Vector of Vectors

How to access the vector of vectors in cpp. And the loop is running infinitely.
How to make it run only once for the given condition.
Or is there a way to access the vector of vectors without using for loop?

1
2
3
4
5
6
7
  if(condition == 1){
  for (int i = 0; i < 3; ++i)
      { 
        for (auto it = m[i].begin();it != m[i].end(); ++it)
          {
        //Do stuffs only one time //here the loop is running infinitely.
           }}}


Thank you
How to access the vector of vectors in cpp.

That's extremely vague, so it's hard to know what you're really asking.

A vector of vectors is still just a vector. You access it in exactly the same way as any other vector, using the same interface.

Each element of that vector is itself also a vector. You can access those elements in exactly the same way as any other vector.

If you're finding the syntax confusing, see the advice I gave you in your other thread for using typedefs to help simplify the code.

Without a more specific question, it's hard to give a more specific answer.

And the loop is running infinitely.

Presumably, that's because the condition it != m[i].end() is never false.

This is where you should use your debugger to step through the code to find out why that's happening.

How to make it run only once for the given condition.

What does that mean?

Or is there a way to access the vector of vectors without using for loop?

All the standard library features for operating on a vector will work on a vector of vectors. For example, stuff in <algorithm> like std::tranform will work.

Is this continuation of the http://www.cplusplus.com/forum/beginner/273416/ ?

Lets assume that you have vector foo, whose elements are vectors (whose elements are floats):
1
2
3
using FloatVector = std::vector<float>;

std::vector<FloatVector> foo;

Each element in foo is a FloatVector. There are many ways to iterate through each element in a vector. For example:
1
2
3
4
for ( FloatVector& fv : foo )
{
  // do something with fv
}

or
1
2
3
4
for ( size_t e = 0; e < foo.size(); ++e )
{
  // do something with foo[e]
}

or
1
2
3
4
for ( auto it = foo.begin(); it != foo.end(); ++it )
{
  // do something with *it
}

Since an element of foo is a vector, you can iterate through it. For example:
1
2
3
4
5
6
7
for ( FloatVector& fv : foo )
{
  for ( float& f : fv )
  {
    // do something with f
  }
}

or
1
2
3
4
5
6
7
for ( size_t row = 0; row < foo.size(); ++row )
{
  for ( size_t col = 0; col < foo[row].size(); ++col )
  {
    // do something with foo[row][col]
  }
}

1
2
3
4
for (auto it = m[i].begin();it != m[i].end(); ++it)
          {
        //Do stuffs only one time //here the loop is running infinitely.
           }


What 'stuff' is being done? Does this 'stuff' invalidate iterators?
How to access the vector of vectors in cpp

A 'vector of vectors' is also known as a 2D vector.

Well, how a 2D vector is accessed depends on if you are trying to access individual elements, or looping through all the elements.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include <vector>
#include <numeric>

int main()
{
   std::cout << "Creating a 2-dimensional vector...\nEnter # of rows: ";
   int num_rows { };
   std::cin >> num_rows;

   std::cout << "Enter # of columns: ";
   int num_cols { };
   std::cin >> num_cols;

   std::cout << "\n";

   // create a 2 dimensional int vector with known dimensions,
   // filled with default value of zero
   std::vector<std::vector<int>> a2DVector(num_rows, std::vector<int>(num_cols));

   // initialize the vector with some values other than zero
   int start  { 101 };
   int offset { 100 };

   // step through each row and fill the row vector with some values
   for (auto& itr : a2DVector)
   {
      std::iota(itr.begin(), itr.end(), start);
      start += offset;
   }

   // let's display the filled 2D vector
   std::cout << "Displaying the filled 2D vector:\n";
   for (const auto& row_itr : a2DVector)
   {
      for (const auto& col_itr : row_itr)
      {
         std::cout << col_itr << ' ';
      }
      std::cout << '\n';
   }
   std::cout << '\n';

   // let's display a single element
   std::cout << "Displaying a single element:\n";
   std::cout << a2DVector[0][1] << '\n';
}
Last edited on
Topic archived. No new replies allowed.