Public Group

# Help with Iterators and Algorithms

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

## 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 on other sites
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 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 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 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...

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633282
• Total Posts
3011206
• ### Who's Online (See full list)

There are no registered users currently online

×