2

I use a vector< int > and I want to store all the vectors into another. So I have chosen list<vector<int> >

Later I want to display all the elements hold in each vector in the list. But I'm wondering how to display them now.

If I'm using only vector or list means I can use iterator and display my ints. But I don't know how to do this. Can anybody help?

Kiril Kirov
  • 35,473
  • 22
  • 102
  • 177
user322
  • 103
  • 8

2 Answers2

2

Nested iterations, This will print each vector's data in a line:

list<vector<int>>  data;

// ...

for (auto &v : data)
{
    for (auto &i : v)
    {
      cout << i << " ";
    }
    cout << endl;
}

A bit older:

for (list<vector<int> >::const_iterator v = data.begin(); v != data.end(); v++)
{
    for (vector<int>::const_iterator i = v->begin(); i != v->end(); i++)
    {
        cout << *i << " ";
    }
    cout << endl;
}
masoud
  • 51,434
  • 14
  • 119
  • 190
  • hey thanks for your suggestion. it might be helpful . i'm just tryiin now to know how both methods work. But i'm wondering how the auto in 1st method is working. – user322 Mar 19 '13 at 13:15
  • 1
    [`auto`](http://www.cprogramming.com/c++11/c++11-auto-decltype-return-value-after-function.html) and [`ranged-based loop`](http://www.cprogramming.com/c++11/c++11-ranged-for-loop.html) are C++11 features. Read about them and enjoy using them. – masoud Mar 19 '13 at 13:35
  • cool thank u very much. Now i tried to display with 2nd method and its able to display only one vector.. might be there's no data or what i have to see.. anyways thanks a lot. – user322 Mar 19 '13 at 13:44
  • looks like i have to add some plug-ins if want to use c++11 features i think. any suggestions on that... – user322 Mar 19 '13 at 13:45
  • No, you need just enable it in your compiler, which compiler do you have? – masoud Mar 19 '13 at 13:47
1

Take a look at this question. It contains a generic pretty-printer for C++ with support for STL containers.

So you'll just #include it and your

    list<vector<int>> data;
    cout << data;

will work like a bliss.

Community
  • 1
  • 1
ulidtko
  • 12,505
  • 10
  • 49
  • 82