0

I have push_back(ed) Lines in 6 "places"/for 6 elements lika below in a 2D vector.

So each element of the 6 consist of different many "MainLines".
So when I am trying to get these out in the OutPut, I know I will use any kind of ::iterator and knowing begin() and end() of each element but I am not sure how to get that line correct. I have started this line out below for example 2DVector[1].

std::vector<std::vector<string> > 2DVector(5, std::vector<string>());
2DVector[i].push_back(MainLine.c_str());

ofstream OutPut("C:\\File1");

for( std::vector<string>::iterator it = 2DVector[1].begin(); it !=2DVector[1].end(); it++)
{
    OutPut << 2DVector[1][*it];
}
2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by John A
0

Use the same logic that you would use if you were outputting a 2D array. Nest two loops, and have two separate indices (in this case, iterators), which keep track of your position in the vector. Something like this:

std::vector< std::vector<int> > my_vector;

// ...

std::vector< std::vector<int> >::iterator iter_x;
std::vector<int>::iterator iter_y;

for (iter_x = my_vector.begin(); iter_x != my_vector.end(); iter_x++)
{
  for (iter_y = iter_x->begin(); iter_y != iter_x->end(); iter_y++)
  {
    // do whatever you want here
    // *iter_y points to the actual data -- in this case, the integer
  }
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.