Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 105,772 C++ Programmers for FREE! Ask your question and get quick answers from experts. There are 1,434 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Vector Iterators

 
Reply to this topicStart new topic

> Vector Iterators, vector::iterator

gabehabe
Group Icon



post 27 May, 2008 - 03:46 PM
Post #1


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! smile.gif
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 8/21/08 03:49PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month