Thread: iterators into vector of vector??

1. iterators into vector of vector??

if I have a vector and I wanted an iterator, I would do something like the following:

Code:
```vector<int> myVector;
vector<int>::iterator it = myVector.begin();```
But if I have a vector of vectors like follows:

Code:
`vector<vector<int> > myVector2;`
How can I get an iterator for this?
Thanks
Mark

2. Have you tried this?
vector < vector < int > > :: iterator it = myVector.begin();

here's one quick and sweet example:
Code:
```#include <iostream>
#include <vector>

using namespace std;

int main ( )
{
int i, j;
vector < vector < int > > matrix;
vector < vector < int > > :: iterator it;
vector <int> :: iterator it2;
matrix.resize(5);
for (i = 0; i < 5; i++)
matrix[i].resize(5);

for (i = 0; i < 5; i++)
for (j = 0; j < 5; j++)
matrix[i][j] = i*j;

for (it = matrix.begin(); it != matrix.end(); ++it, cout<<endl)
for (it2 = it->begin();it2 != it ->end(); ++it2)
cout <<*it2<<" ";
return 0;
}```

3. The point being that an iterator is a compound type. In order for an object of this type to be defined, it is necessary to include the iterator keyword and the type of the iterator itself. Hence being a compound type.

The type of an iterator, is always the type of the container to which the iterator points:

vector<int>
An iterator to this container is of the type vector<int>

vector< vector<int> >
An iterator to this cointainer is of the type vector < vector<int> >. You can traverse the "mother" container.
An iterator to the underlying cointainer is of type vector<int>. You can traverse the "child" container.

Mother
and child here being loosely used

4. Perhaps std::map would be more suited to the task. A vector of vectors is quite confusing.

5. Probably is, but maybe he's just curious...

6. Maybe he just wants a matrix

7. Originally Posted by Mario F.
The point being that an iterator is a compound type. In order for an object of this type to be defined, it is necessary to include the iterator keyword and the type of the iterator itself. Hence being a compound type.
Iterator is not a keyword, just a convention. And compound type means something else.

The type of an iterator, is always the type of the container to which the iterator points:
The type of an iterator is the type of the iterator. It's value type is the same as that of the container it is meant for.

vector<int>
An iterator to this container is of the type vector<int>
No, it's of the type vector<int>::iterator. This type is opaque: it could be something as simple as int*, it could also be an instantiation of the __gnu_cxx::__normal_iterator template.
The iterator's value type is int.

vector< vector<int> >
An iterator to this cointainer is of the type vector < vector<int> >. You can traverse the "mother" container.
An iterator to the underlying cointainer is of type vector<int>. You can traverse the "child" container.
Same thing. The iterator's value type in this case is vector<int>, i.e. dereferencing the iterator yields a vector, which you could in turn traverse.

8. Originally Posted by CornedBee
Iterator is not a keyword, just a convention. And compound type means something else.
A compound type is loosely meant as a type defined in terms of another type (Lipmann and others) . A far-reach compound type is a class. An iterator is a compound type.

However, I do agree it's strecthing the meaning.

Originally Posted by CornedBee
The type of an iterator is the type of the iterator. It's value type is the same as that of the container it is meant for.
I meant the abstract notion of the iterator. Not the iterator object itself.

The rest of my post follows on this notion.