mardi 19 février 2019

How different is a 2D vector from a 2D array in terms of storing and accessing elements C ++

I have been trying to understand how to access elements inside a vector which does not have a fixed dimension like an array. The following example contains a vector which has a random size. I want to understand the working mechanism that makes the accessing of each element possible.

#include <iostream>
#include <vector>
using namespace std;
int main()
{ 
    vector <vector <int>> vector_2d
    {
        {1, 2, 3},
        {10, 20, 30, 40},
        {100, 200, 300, 400, 500},
    };
    for (auto vec: vector_2d) {
        for (auto val: vec) {
            cout << val << " ";
        };
        cout << endl;
    };
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire