Two-dimensional vectors in C++

  5 mins read  

Two-dimensional vectors in C++

We know that there are two-dimensional arrays, but there are also two-dimensional vectors and that’s what we’re going to talk about today’s cpp::daily.

Vector vector is a two-dimensional vector with a variable number of lines, where each line is a vector. Each array index stores an array that can be traversed and accessed using iterators. It is similar to an array of vectors, but with dynamic properties.

In the future we will have content about libraries that make use of advanced concepts of Mathematics Arrays and Determinants that are generally used to map images, so let’s see how we write a two-dimensional vector now.

When you want to create a multidimensional array you use it like this, for example:

int array[3][5] = {  { 11, 2, 9, 27, 89 },
                     { 20, 13, 52 },
                     { 8, 4, 79, 4 }
                  };

So if you want to print the number 52, it would be: std::cout << array[1][2] .

Two-dimensional vectors work in much the same way only the performance is superior. The same code above using vectors would be:

#include <vector>

std::vector<std::vector<int>> vec {  { 11, 2, 9, 27, 89 },
                                      { 20, 13, 52 },
                                      { 8, 4, 79, 4 }
                                  };

This is a two-dimensional vector, so if you want to print the number 52 as well, it would be:

std::cout << vec[1][2] << '\n';

If you want to print all the values of this vector you can create a nested loop, for example:

for ( int i = 0; i < vec.size(); ++i ) {
 for (int j = 0;  j < vec[i].size(); ++j) {
   std::cout << vec[i][j] << ' ';
 }
 std::cout << '\n';
}

The output will be:

11 2 9 27 89
20 13 52
8 4 79 4 

Displaying each position of the elements:

for ( int i = 0; i < vec.size(); ++i ) {
  for (int j = 0;  j < vec[i].size(); ++j) {
    std::cout << "vec[" << i << "][" << j << "]: " << vec[i][j];
    if( j != 3 ){
      std::cout << ", ";
    }
  }
  std::cout << '\n';
}

What if you want to add one more coordinate to this vector? Just use push_back(), example:

Before the elements are printed by the for loop.

vec.push_back( { 22, 33, 44 } );

And if you want to remove the last one added or not, just use pop_back():

vec.pop_back();

Final code:

#include <iostream>
#include <vector>

int main( int argc, char ** argv ){
  std::vector<std::vector<int>> vec{  { 11, 2, 9, 27, 89 },
                                      { 20, 13, 52 },
                                      { 8, 4, 79, 4 }
                                   };

  vec.push_back( { 22, 33, 44 } );
  vec.pop_back();

  for ( int i = 0; i < vec.size(); ++i ) {
    for (int j = 0;  j < vec[i].size(); ++j) {
      std::cout << "vec[" << i << "][" << j << "]: " << vec[i][j];
      if( j != 3 ){
        std::cout << ", ";
      }
    }
    std::cout << '\n';
  }

  return 0;
}

Add our feed: https://en.terminalroot.com.br/feed to:

Feedly