Page 1 of 1

Vector Iterators vector::iterator Rate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=53183&amp;s=bcea3a09f41c0d39e2a316cdd1cfbc1e&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 gabehabe

• GabehabeSwamp

Reputation: 1399
• Posts: 10,965
• Joined: 06-February 08

Posted 27 May 2008 - 03:46 PM

What do I need to know before covering this tutorial?
All you need is a basic knowledge of vectors.

OK, just a brief coverage of the basic beginning:
```#include <iostream> // just to output and show what's going on
#include <vector>   // include vectors and iterators

using namespace std;

int main (){
```

I'm going to start by declaring two vectors, just so that you can see how an iterator can be used more than once.
```    vector <int> myVec; // declare a vector
// fill myVec with some values
for (int i = 0; i < 10; i++)
myVec.push_back(i);

vector <int> myVec2; // declare a second vector
// fill myVec2 with some values
for (int i = 10; i < 20; i++)
myVec2.push_back(i);
```

Now comes the iterator. This is pretty simple really:
```    vector <int>::iterator It; // declare a general iterator
```
Basically, we need an iterator for an int, since this is what our vectors contain. It is just the name for our iterator (this can be anything)

Now all we need to do is iterate through our vector in a for loop, like so (explanations are in the comments)
```    // It = beginning of myVec (first value)
// It != end of myVec (last value)
// ++It increase It to point to the next value of the vector
for (It = myVec.begin(); It != myVec.end(); ++It)
cout << *It << '\t'; // output the current value that It is *pointing to

// print a new line
cout << endl;

// It = beginning of myVec2 (first value)
// It != end of myVec2 (last value)
// ++It increase It to point to the next value of the vector
for (It = myVec2.begin(); It != myVec2.end(); ++It)
cout << *It << '\t'; // output the current value that It is *pointing to
```

Finally, all we need to do is close off our program.
```    cout << endl;
cin.get();
return 0;
}
```

Here is the completed code, for those of you who want it:
```#include <iostream> // just to output and show what's going on
#include <vector>   // include vectors and iterators

using namespace std;

int main (){
vector <int> myVec; // declare a vector
// fill myVec with some values
for (int i = 0; i < 10; i++)
myVec.push_back(i);

vector <int> myVec2; // declare a second vector
// fill myVec2 with some values
for (int i = 10; i < 20; i++)
myVec2.push_back(i);

vector <int>::iterator It;

// It = beginning of myVec (first value)
// It != end of myVec (last value)
// ++It increase It to point to the next value of the vector
for (It = myVec.begin(); It != myVec.end(); ++It)
cout << *It << '\t'; // output the current value that It is *pointing to

// print a new line
cout << endl;

// It = beginning of myVec2 (first value)
// It != end of myVec2 (last value)
// ++It increase It to point to the next value of the vector
for (It = myVec2.begin(); It != myVec2.end(); ++It)
cout << *It << '\t'; // output the current value that It is *pointing to

cout << endl;
cin.get();
return 0;
}
```

A quick note, before we finish:
An iterator can be reused, as seen in the code above. The only time that you will need more than one iterator, is when you need to iterate through a vector of a different data type. A few examples:
vector <int>::iterator It; will create an iterator for a vector of ints
vector <char>::iterator It; will create an iterator for a vector of chars
vector <float>::iterator It; will create an iterator for a vector of floats
And so on...

Happy coding!

Is This A Good Question/Topic? 2

Replies To: Vector Iterators

#2 codetonic

Reputation: 2
• Posts: 46
• Joined: 13-May 09

Posted 03 June 2009 - 03:27 PM

Thanks. I've learned all I know about iterators from you.
I'm expanding on your DeckOfCards example for a poker simulator.

Using a vector and iterator for the deck is pure elegance.

Could you compare iterators with references and value_types?

#3 Guest_guest*

Reputation:

Posted 15 August 2010 - 01:27 PM

```// It != end of myVec (last value)

```

I think you meant to say ...

```// It != pointing to one past end of myVec ('It' to one past last value)

```

... and the same for the 2nd vector

#4 RetardedGenius

• ﻿>>──(Knee)──►

Reputation: 126
• Posts: 555
• Joined: 30-October 10

Posted 23 May 2011 - 09:08 AM

Excellent tutorial, I was surprised by just how simple this concept is (other people had made it sound a lot more complicated, I experienced the same phenomenon with Pointers lol.)