template <class InputIterator, class Function> Function for_each( InputIterator xFirst, InputIterator xLast, Function xFunct );
#include <algorithm>
#include <iostream> #include <vector> #include <algorithm> void Increment(char& crChar) { ++crChar; } int main() { using namespace std; // Create a vector instance vector<char> qV; qV.push_back('X'); qV.push_back('o'); qV.push_back('a'); qV.push_back('X'); qV.push_back('.'); qV.push_back('n'); qV.push_back('e'); qV.push_back('t'); vector<char>::iterator qIter; // Output the vector cout << "Original: "; for (qIter = qV.begin(); qIter != qV.end(); ++qIter) { cout << *qIter; } cout << endl; // Apply the increment to the last four entries for_each(qV.begin() + 4, qV.end(), Increment); // Output the vector after the increment cout << "Incremented: "; for (qIter = qV.begin(); qIter != qV.end(); ++qIter) { cout << *qIter; } cout << endl; // Keep the window open cin.get(); return 0; }
© 20072024 XoaX.net LLC. All rights reserved.