1: //replace_copy1a.cpp
3: #include <iostream>
4: #include <vector>
5: #include <algorithm>
6: using namespace std;
8: int main()
9: {
10: cout << "\nThis program illustrates the use of the STL "
11: "replace_copy() algorithm to copy\nall values from one vector "
12: "of integers to another and simultaneously replace,\nin the "
13: "output, all instances of a given value with another value.";
14: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
16: int a1[] = {1, 2, 2, 3, 4, 5, 2, 3};
17: vector<int> v1(a1, a1+8);
19: cout << "\nHere are the values in the vector v1:\n";
20: for (vector<int>::size_type i=0; i<v1.size(); i++)
21: cout << v1.at(i) << " ";
22: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
24: int a2[] = {101, 102, 103, 104, 105, 106,
25: 107, 108, 109, 110, 111, 112};
26: vector<int> v2(a2, a2+12);
28: cout << "\nHere are the values in the vector v2:\n";
29: for (vector<int>::size_type i=0; i<v2.size(); i++)
30: cout << v2.at(i) << " ";
31: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
33: cout << "\nNow we copy all values from v1 to v2, starting at the "
34: "3rd value of v2,\nand simultaneously replace each 2 with 222.";
35: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
37: vector<int>::iterator p = replace_copy(v1.begin(), v1.end(),
38: v2.begin()+2, 2, 222);
40: cout << "\nHere are the revised values in v2:\n";
41: for (vector<int>::size_type i=0; i<v2.size(); i++)
42: cout << v2.at(i) << " ";
43: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
45: cout << "\nThe iterator returned by the algorithm is pointing at "
46: "the value " << *p << ".";
47: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
48: }