Online Book Reader

Home Category

Professional C__ - Marc Gregoire [194]

By Root 1547 0
the program is:

equal!

vectorOne is not less than vectorTwo

vector Iterators

The section on “Iterators” at the beginning of this chapter explained the basics of container iterators. The discussion can get a bit abstract, so it’s helpful to jump in and look at a code example. Here is the test score normalization program from earlier with the for loop previously using size() replaced by a for loop using an iterator:

vector doubleVector;

// Initialize max to smallest number

double max = numeric_limits::lowest();

for (size_t i = 0; true; i++) {

double temp;

cout << "Enter score " << i + 1 << " (-1 to stop): ";

cin >> temp;

if (temp == -1) {

break;

}

doubleVector.push_back(temp);

if (temp > max) {

max = temp;

}

}

max /= 100.0;

for (vector::iterator iter = doubleVector.begin();

iter != doubleVector.end(); ++iter) {

*iter /= max;

cout << *iter << " ";

}

Code snippet from TestScores\TestScoresIterator.cpp

You see for loops like the new one in this example quite a bit in STL code. First, take a look at the for loop initialization statement:

vector::iterator iter = doubleVector.begin();

Recall that every container defines a type named iterator to represent iterators for that type of container. begin() returns an iterator of that type referring to the first element in the container. Thus, the initialization statement obtains in the variable iter an iterator referring to the first element of doubleVector. Next, look at the for loop comparison:

iter != doubleVector.end();

This statement simply checks if the iterator is past the end of the sequence of elements in the vector. When it reaches that point, the loop terminates. The increment statement, ++iter, increments the iterator to refer to the next element in the vector.

Use preincrement instead of postincrement when possible because preincrement is at least as efficient, and usually more efficient. iter++ must return a new iterator object, while ++iter can simply return a reference to iter. See Chapter 18 for details on implementing operator++, and Chapter 17 for details on writing your own iterators.

The for loop body contains these two lines:

*iter /= max;

cout << *iter << " ";

As you can see, your code can both access and modify the elements over which it iterates. The first line uses * to dereference iter to obtain the element to which it refers, and assigns to that element. The second line dereferences iter again, but this time only to stream the element to cout.

With C++11, the syntax of the preceding for loop using iterators can be simplified by using the auto keyword introduced in Chapter 1. This is shown in the following code fragment:

for (auto iter = doubleVector.begin();

iter != doubleVector.end(); ++iter) {

*iter /= max;

cout << *iter << " ";

}

Code snippet from TestScores\TestScoresIterator.cpp

In this example, the compiler will automatically derive the type of the variable iter based on the right-hand side of the initializer, which in this case is the result of the call to begin().

Using the range-based for loop of C++11, the loop can be simplified even further. The range-based for loop is introduced in Chapter 1. The following code does exactly the same as the previous implementations of the loop:

for (auto& d : doubleVector) {

d /= max;

cout << d << " ";

}

Code snippet from TestScores\TestScoresIterator.cpp

This looks much more elegant than the other versions of the loop.

Accessing Fields of Object Elements

If the elements of your container are objects, you can use the -> operator on iterators to call methods or access members of those objects. For example, the following small program creates a vector of 10 strings, then iterates over all of them appending a new string to the old one:

vector stringVector(10, "hello");

for (auto it = stringVector.begin(); it != stringVector.end(); ++it) {

it->append(" there");

}

Code snippet from VectorIterators\AccessingFields.cpp

Or, using the range-based for loop, it can be written as follows:

vector stringVector(10,

Return Main Page Previous Page Next Page

®Online Book Reader