# Looping through a vector of vector

• 11-02-2009
Ducky
Looping through a vector of vector
Should we looping like declaring another vector:

Code:

```#include <vector> #include <iostream> using namespace std; int main() {     vector< vector<int> > ph;     vector<int> p;     p.push_back(1);     p.push_back(2);     p.push_back(3);     vector<int> q;     q.push_back(10);     q.push_back(20);     q.push_back(30);     q.push_back(40);     ph.push_back(p);     ph.push_back(q);     for (vector< vector<int> >::size_type u = 0; u < ph.size(); u++) {         for (vector<int>::size_type v = 0; v < ph[u].size(); v++) {             cout << ph[u][v] << " ";         }         cout << endl;     } }```
or is it ok just simply with an unsigned int:

Code:

```for (unsigned u = 0; u < ph.size(); ++u) {   for (unsigned v = 0; v < ph[u].size(); ++v) {     ...   } }```
• 11-02-2009
Daved
Practically speaking, either is fine. If you want to be super correct, use the first.
• 11-02-2009
brewbuck
Hell, I sometimes use plain old int, but you need to be aware of the size of the vector and know that that's a safe thing to do.

In case you're wondering why I would ever do that, it's because of patterns like this:

Code:

```int bestIdx = -1; for( int i = 0; i < (int)vec.size(); ++i ) {     if( MeetsSomeCondition( vec[ i ] ) && ( bestIdx < 0 || BetterThan( vec[ i ], vec[ bestIdx ] ) )         bestIdx = i; }```
In other words, I want to use -1 as a placeholder for "don't have a result yet"
• 11-02-2009
Ducky
Thank you very much guys! :)

It was working fine with an int but i told myself better be safe than sorry. :)