lost_with_coding lost_with_coding - 3 years ago 194
C++ Question

C++/Size of a 2d vector

How do I find the size of a 2 dimensional vector? So far I have the following code which doesn't compile.

#include <iostream>
#include <vector>

using namespace std;

int main()

vector < vector <int> > v2d;

for (int x = 0; x < 3; x++)
for (int y = 0; y < 5; y++)
v2d.push_back(vector <int> ());


return 0;

Answer Source

To get the size of v2d, simply use v2d.size(). For size of each vector inside v2d, use v2d[k].size().

Note: for getting the whole size of v2d, sum up the size of each individual vector, as each vector has its own size.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download