The concept of an iterator is fundamental to understanding the C++ Standard Template Library (STL) because iterators provide a means for accessing data stored in container classes such a vector, map, list, etc.
You can think of an iterator as pointing to an item that is part of a larger container of items. For instance, all containers support a function called begin, which will return an iterator pointing to the beginning of the container (the first element) and function, end, that returns an iterator corresponding to having reached the end of the container. In fact, you can access the element by "dereferencing" the iterator with a *, just as you would dereference a pointer.
To request an iterator appropriate for a particular STL templated class, you use the syntax
std::class_name<template_parameters>::iterator namewhere name is the name of the iterator variable you wish to create and the class_name is the name of the STL container you are using, and the template_parameters are the parameters to the template used to declare objects that will work with this iterator. Note that because the STL classes are part of the std namespace, you will need to either prefix every container class type with "std::", as in the example, or include "using namespace std;" at the top of your program.
std::vector<int> myIntVector; std::vector<int>::iterator myIntVectorIterator;Different operations and containers support different types of iterator behavior. In fact, there are several different classes of iterators, each with slightly different properties. First, iterators are distinguished by whether you can use them for reading or writing data in the container. Some types of iterators allow for both reading and writing behavior, though not necessarily at the same time.
using namespace std; vector<int> myIntVector; // Add some elements to myIntVector myIntVector.push_back(1); myIntVector.push_back(4); myIntVector.push_back(8); for(int y=0; y<myIntVector.size(); y++) { cout<<myIntVector[y]<<" "; //Should output 1 4 8 }The STL approach (use this)
using namespace std; vector<int> myIntVector; vector<int>::iterator myIntVectorIterator; // Add some elements to myIntVector myIntVector.push_back(1); myIntVector.push_back(4); myIntVector.push_back(8); for(myIntVectorIterator = myIntVector.begin(); myIntVectorIterator != myIntVector.end(); myIntVectorIterator++) { cout<<*myIntVectorIterator<<" "; //Should output 1 4 8 }As you might imagine, you can use the decrement operator, --, when working with a bidirectional iterator or a backward operator.
iterator + nwhere n is an integer. The result will be the element corresponding to the nth item after the item pointed to be the current iterator. This can be a problem if you happen to exceed the bounds of your iterator by stepping forward (or backward) by too many elements.
vector<int> myIntVector; vector<int>::iterator myIntVectorIterator; myIntVectorIterator = myIntVector.begin() + 2;You can also use the standard arithmetic shortcuts for addition and subtraction, += and -=, with random access iterators. Moreover, with random access iterators you can use <, >, <=, and >= to compare iterator positions within the container.
vector<int>::iterator myIntVectorIterator; myIntVector.erase(myIntVectorIterator.begin(), myIntVectorIterator.end());which would delete all elements in the vector. If you only wanted to delete the first two elements, you could use
myIntVector.erase(myIntVectorIterator.begin(), myIntVectorIterator.begin()+2);Note that various container class support different types of iterators -- the vector class, which has served as our model for iterators, supports a random access iterator, the most general kind. Another container, the list container (to be discussed later), only supports bidirectional iterators.
class_name<parameters>::iterator