Online Book Reader

Home Category

Professional C__ - Marc Gregoire [290]

By Root 1226 0
rend(). The method rbegin() returns a reverse_iterator starting at the last element of the container, and rend() returns a reverse_iterator starting at the first element of the container. Applying operator++ to a reverse_iterator calls operator--on the underlying container iterator, and vice versa. For example, iterating over a collection from the beginning to the end can be done as follows:

for (auto iter = collection.begin(); iter != collection.end(); ++iter) {}

Iterating over the elements in the collection from the end to the beginning can be done using a reverse_iterator by calling rbegin() and rend(). Note that you still call ++iter:

for (auto iter = collection.rbegin(); iter != collection.rend(); ++iter) {}

The reverse_iterator is useful mostly with algorithms in the STL that have no equivalents that work in reverse order. For example, the basic find() algorithm searches for the first element in a sequence. If you want to find the last element in the sequence, you can use a reverse_iterator instead. Note that when you call an algorithm such as find() with a reverse_iterator, it returns a reverse_iterator as well. You can always obtain the underlying iterator from a reverse_iterator by calling the base() method on the reverse_iterator. However, due to the implementation details of reverse_iterator, the iterator returned from base() always refers to one element past the element referred to by the reverse_iterator on which it’s called.

Here is an example of find() with a reverse_iterator:

// The implementation of populateContainer() is identical to that shown in

// Chapter 13, so it is omitted here.

vector myVector;

populateContainer(myVector);

int num;

cout << "Enter a number to find: ";

cin >> num;

vector::iterator it1;

vector::reverse_iterator it2;

it1 = find(myVector.begin(), myVector.end(), num);

it2 = find(myVector.rbegin(), myVector.rend(), num);

if (it1 != myVector.end()) {

cout << "Found " << num << " at position " << it1 - myVector.begin()

<< " going forward." << endl;

cout << "Found " << num << " at position "

<< it2.base() - 1 - myVector.begin() << " going backward." << endl;

} else {

cout << "Failed to find " << num << endl;

}

Code snippet from IteratorAdapters\ReverseIterators.cpp

One line in this program needs further explanation. The code to print out the position found by the reverse iterator looks like this:

cout << "Found " << num << " at position "

<< it2.base() - 1 - myVector.begin() << " going backward." << endl;

As noted earlier, base() returns an iterator referring to one past the element referred to by the reverse_iterator. In order to get to the same element, you must subtract one.

A possible output of this program is as follows:

Enter a number (0 to quit): 11

Enter a number (0 to quit): 22

Enter a number (0 to quit): 33

Enter a number (0 to quit): 22

Enter a number (0 to quit): 11

Enter a number (0 to quit): 0

Enter a number to find: 22

Found 22 at position 1 going forward.

Found 22 at position 3 going backward.

The previous example defined the iterators as follows:

vector::iterator it1;

vector::reverse_iterator it2;

it1 = find(myVector.begin(), myVector.end(), num);

it2 = find(myVector.rbegin(), myVector.rend(), num);

This is done to show you the exact types of it1 and it2. Of course, using the auto keyword, these four lines can be compacted to the following two lines:

auto it1 = find(myVector.begin(), myVector.end(), num);

auto it2 = find(myVector.rbegin(), myVector.rend(), num);

Stream Iterators

The STL provides adapters that allow you to treat input and output streams as input and output iterators. Using these iterators you can adapt input and output streams so that they can serve as sources and destinations, respectively, in the various STL algorithms. The ostream_iterator class is an output stream iterator. It is a template class that takes the element type as a type parameter. Its constructor takes an output stream and a string to write to the stream following each element. The ostream_iterator class writes elements

Return Main Page Previous Page Next Page

®Online Book Reader