Number of vectors in a vector of vectors?

Oct 20, 2012 at 12:42pm
I have a 2d vector that is not necessarily square. I want to know how many rows and columns it has (obviously, it isn't really 2d, but I'm thinking of it as an array).

The number of elements of each vector within this vector can be found with vec[i].size(), but what about the number of vector elements within the original vector? Can I use vec.size()? Or will that return the total number of elements in the 2d vector?

Thanks!
Oct 20, 2012 at 1:00pm
Try it, see what happens. It should be pretty easy.
Oct 20, 2012 at 1:29pm
Yes you can use vec.size() to determine how many "rows" the vector has.

For example you can count the total number of elements the following way (assuming that you have std::vecror<vector<int>>)

1
2
std::vector<vector<int>>::size_type total = 0;
for ( const auto &v : vec ) total += v.size();
Oct 20, 2012 at 1:40pm
I was hoping the OP could figure that out himself.

I think one can learn more by testing their own theories, rather than just getting the answer from someone.

I am not trying to offend anyone, just my Opinion :+) Cheers
Oct 20, 2012 at 11:10pm
Went with what I said in the OP and everything worked out, thanks all the same.
Topic archived. No new replies allowed.