I am using an array of vectors. I have to count number of rows in the array. I am using the technique used for a 2D array. But it does not give desired output.
int main()
{
int i,p=7;
vector<float> *mat=new vector<float>[p];
int k=5;
for(i=0;i<p;i++){
mat[i].assign(k,0);
}
cout<<sizeof(mat)/sizeof(mat[0])<<endl;
return 0;
}
Why is this resulting in 0 ? Shouldn't the output be 7? How to calculate number of rows ?
sizeof(mat)give you the size of the pointer, not the size of whatever memory it point so. Secondly,sizeof(mat[0])give you the size of thestd::vectorobject itself, not the number of elements in the vector (that would be e.g.mat[0].length()). And why don't you use a vector of vectors (i.e.vector<vector<float>>) instead (or ifkis a compile-time constant, a vector of arrays, as invector<array<float, 5>>)?sizeofis a computed at compile time, you can't domat[0].length()