I create a 2d vector, however when I read the size from this 2d vector pointer, it gets wrong size. I have no idea about, can anyone explain this?
#include <stdio.h>
#include <vector>
void read_size(std::vector<std::vector<float>> *vec)
{
printf("read size %d, %d\n", vec->size(), vec[0].size());
}
int main()
{
std::vector<std::vector<float>> vec;
vec.resize(100);
vec[0].resize(50);
printf("main %d, %d\n", vec.size(), vec[0].size());
read_size(&vec);
return 0;
}
The output is
main 100, 50
read size 100, 100
I think both size should be 100, 50
vec->size()
is equiavlent to (*vec).size()
.
vec[0]
is equivalent to *(vec + 0)
, which is equivalent to *vec
.
Therefore, vec->size()
and vec[0].size()
are the same things.
You should first dereference the pointer vec
in the function read_size
to access to its elements.
This can be done like (*vec)[0].size()
, vec[0][0].size()
, or vec->at(0).size()
.