Professional C__ - Marc Gregoire [237]
The section “Modifying Algorithms” in Chapter 11 lists all available modifying algorithms with a description. This section provides code examples for a number of those algorithms.
iota
C++11 adds an iota() algorithm, defined in the vector iota(vec.begin(), vec.end(), 5); for (auto& i : vec) cout << i << " "; Code snippet from ModifyingAlgorithms\iota.cpp The output is as follows: 5 6 7 8 9 10 11 12 13 14 transform The transform() algorithm is similar to for_each(), in that it applies a callback to each element in a range. The difference is that transform() expects the callback to generate a new element for each call, which it stores in the destination range specified. The source and destination ranges can be the same if you want transform() to replace each element in a range with the result from the call to the callback. For example, you could add 100 to each element in a vector like this: // The populateContainer() function is identical to the one shown earlier // for comparison algorithms, so it is omitted here. vector populateContainer(myVector); cout << "The vector contents are:" << endl; for (auto& i : myVector) cout << i << " "; cout << endl; transform(myVector.begin(), myVector.end(), myVector.begin(), [](int i){return i + 100;}); cout << "The vector contents are:" << endl; for (auto& i : myVector) cout << i << " "; Code snippet from ModifyingAlgorithms\TransformLambda.cpp Another form of transform() calls a binary function on pairs of elements in the range. The following example creates two vectors and uses transform() to calculate the sum of pairs of elements and store the result back in the first vector: // The populateContainer() function is identical to the one shown earlier // for comparison algorithms, so it is omitted here. vector cout << "Vector1:" << endl; populateContainer(vec1); cout << "Vector2:" << endl; vector populateContainer(vec2); if (vec2.size() < vec1.size()) { cout << "Vector2 should be at least the same size as vector1." << endl; return 1; } // Create a lambda to print a vector auto printVec = [](const vector for (auto& i : vec) cout << i << " "; cout << endl; }; cout << "Vector1: "; printVec(vec1); cout << "Vector2: "; printVec(vec2); transform(vec1.begin(), vec1.end(), vec2.begin(), vec1.begin(), [](int a, int b){return a + b;}); cout << "Vector1: "; printVec(vec1); cout << "Vector2: "; printVec(vec2); Code snippet from ModifyingAlgorithms\TransformLambdaBinary.cpp The output could look as follows: Vector1: Enter a number (0 to quit): 1 Enter a number (0 to quit): 2 Enter a number (0 to quit): 0 Vector2: 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): 0 Vector1: 1 2 Vector2: 11 22 33 Vector1: 12 24 Vector2: 11 22 33 transform() and the other modifying algorithms often return an iterator referring to the past-the-end value of the destination range. The examples in this book usually ignore that return value. copy The copy() algorithm allows you to copy elements from one range to another, starting with the first element and proceeding to the last element in the range. The source and destination ranges must be different, but they can overlap. Note that copy() doesn’t insert