Jump to content
  • Advertisement
Sign in to follow this  
gforce100

Help with Iterators and Algorithms

This topic is 4376 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Can anybody help in decribing what are iterators and algorithms, i dont quite understand what they are and what they are useful for. any help welcomed and links, thanks

Share this post


Link to post
Share on other sites
Advertisement
An iterator is a way of accessing elements of an object without caring what the object is. For instance, you can use an iterator on a std::vector, std::map and std::list in exactly the same way, without caring what the object is. The main advantage is that you can change your object (container) and not have to go and re-write a bunch of code for it.

Algorithms (Presuming you mean STL algorithms) use iterators so there doesn't have to be a copy of the code for a vector, a copy for a list, a copy for a map, etc.
Examples of algorithms include sorting functions, finding functions, etc.

Example:

typedef std::vector<int> Container;

Container c;
for(int i=0; i<100; ++i)
{
c.insert(c.end(), rand());
}

std::sort(c.begin(), c.end());


In the above code, you should be able to change the definition of "Container" to whatever you like and the code should behave exactly the same. Why would you want to do this? Well, if you're doing more complex stuff, a map might be better suited than a vector (Maps are good for lookup tables, vectors are good at random access, etc).

Share this post


Link to post
Share on other sites
I will give it a high level shot.

An iterator is just that, an iterator ;) The stl random iterators model the concept of a pointer.

i.e.

vector<type>::iterator it = myVector.begin();
for( ; it != myVector.end(); ++it )
//do something

is the same as

type array[CONST_SIZE];
type* it = array;

for( ; it != array + CONST_SIZE; ++it )
//do something

as you can see the vector just models an array. But the iterator models the concept of the pointer which can be used like so in the std::sort


// vector version
sort( myVecto.begin(), myVector.end(), SomePredicate );

// array version, still using std::sort
sort( array, array+CONST_SIZE, SomePredicate );


The real power of doing this, is that any container that supports an iterator model of a pointer can be used with any stl algorithm.
Does this help any? I would suggest getting the book Generic Programming and the STL by Matthew H. Austen from Addsion Wesley

Share this post


Link to post
Share on other sites
thanks for the help,

so are iterators for accessing things from a vector etc. and changing them,
and using both iterators and algorithms can save time and space.
thanks

Share this post


Link to post
Share on other sites
Basically yes... you would use iterators to access elements in the containers. The C++ array type as a "built in iterator" if you will.. Then you can use the stl algorithms to massage your containers.. there's copy, sort, transform, search... just to name a few...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!