Column size and row size of a 2D vector in C++

You have a vector of integer vectors
myVector[0].size() returns you the amount of elements in the first int vector in the 2d vector.

The structure of such vector looks like this:

myVector[
  Vector[0, 4, 2, 5],
  Vector[1, 4, 2]
];

When you call for myVector[1].size() it would return 3 and [0] would return 4.

For the amount of rows (int vectors) in the 2d vector, you can just use myVector.size()

You can run this to see it in actions

#include <iostream>
#include <vector>

int main(){
    std::vector<std::vector<int>>MyVector;
    std::vector<int>temp;

    temp.push_back(1);
    temp.push_back(2);
    temp.push_back(3);
    MyVector.push_back(temp);

    std::cout << "Rows in the 2d vector: " << MyVector.size() <<
    std::endl << "Collumns in the 1st row: " << MyVector[0].size() <<
    std::endl;

    system("pause");
    return 0;
}

This is the output:

Rows in the 2d vector: 1
Collumns in the 1st row: 3

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)