C++ Iterator

C++ Iterators

Iterators are used to access and iterate through elements of data structures (vectors, sets, etc.), by “pointing” to them.

It is called an “iterator” because “iterating” is the technical term for looping.

To iterate through a vector, look at the following example:

Example

// Create a vector called cars that will store strings
vector<string> cars = {"Volvo", "BMW", "Ford", "Mazda"};

// Create a vector iterator called it
vector<string>::iterator it;

// Loop through the vector with the iterator
for (it = cars.begin(); it != cars.end(); ++it) {
  cout << *it << "\n";
}

Continue reading C++ Iterator