Page 1 of 1

Vector Iterators vector::iterator Rate Topic: -----

#1 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1383
  • View blog
  • Posts: 10,962
  • 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? 1
  • +

Replies To: Vector Iterators

#2 codetonic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#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
Was This Post Helpful? 0

#4 RetardedGenius  Icon User is offline

  • >>──(Knee)──►
  • member icon

Reputation: 126
  • View blog
  • 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.) :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1