Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++/Size of a 2d vector

Tags:

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> ());             v2d[x].push_back(y);         }     }      cout<<v2d[0].size()<<endl;     cout<<v2d[0][0].size()<<endl;      return 0; } 
like image 487
pandoragami Avatar asked Dec 26 '10 17:12

pandoragami


People also ask

How do you determine the size of a 2D vector?

vector_name. size() gives you the numbers of column in a 2D vector and vector_name[0]. size() gives you the numbers of rows in a 2D vector in C++.

Can a vector be 2 dimensional C++?

A 2-Dimensional vector is used in C++ programming to store and access data based on rows and columns. Different ways to create a 2-Dimensional vector have been shown in this tutorial by using simple examples.

How do you find the dimensions of a vector matrix?

Size of a matrix = number of rows × number of columns. It can be read as the size of a matrix and is equal to number of rows “by” number of columns.


1 Answers

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.

like image 120
Shamim Hafiz - MSFT Avatar answered Oct 04 '22 04:10

Shamim Hafiz - MSFT