| mono (37) | |
|
I am confused. What is exactly an iterator? and what it is use for? I know that there is 5 kinds of iterators: input, output, forward, bidirectional and random, but still I am confused. Anyone could help me to understand. Thank you. | |
|
|
|
| Peter87 (3687) | |
|
Iterators are used to traverse containers like list, vector, maps, arrays, etc. The "kind" of the iterator is decided by what operations it supports. A forward iterator is an iterator that you can go from one element to the next. With a bidirectional iterator you can go both forward and backwards. With a Random access iterator it is possible to go from one element to any other in one step (using an offset). Note that a bidirectional iterator is also a forward iterator, and that a random access iterator is also a bidirectional iterator. By using function templates it is possible to write functions that works for any kind of iterator of a certain kind. An example is std::sort that works with all types of random access iterators, meaning it can sort arrays, vectors, deques, etc. but not things like std::list because it doesn't have random access iterators. | |
|
Last edited on
|
|
| JLBorges (1336) | |
|
A very good introduction to iterators in C++: 'http://www.mochima.com/tutorials/STL.html#Introduction The best that I've encountered on the web. | |
|
|
|