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:
cpp
#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.
cpp
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:
cpp
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)
cpp
// 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.
cpp
cout << endl;
cin.get();
return 0;
}
Here is the completed code, for those of you who want it:
cpp
#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!
