Source of replace1a.cpp


  1: //replace1a.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() algorithm to replace\nall copies of a given "
 12:         "value from a vector of integers with another value.";
 13:     cout << "\nPress Enter to continue ... ";  cin.ignore(80, '\n');

 15:     int a[] = {1, 2, 2, 3, 4, 5, 2, 3};
 16:     vector<int> v(a, a+8);

 18:     cout << "\nHere are the values in the vector:\n";
 19:     for (vector<int>::size_type i=0; i<v.size(); i++)
 20:         cout << v.at(i) << " ";
 21:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');

 23:     cout << "\nNow we replace all copies of the value 2 with 222.";
 24:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 25:     replace(v.begin(), v.end(), 2, 222);

 27:     cout << "\nHere are the revised contents of the vector:\n";
 28:     for (vector<int>::size_type i=0; i<v.size(); i++)
 29:         cout << v.at(i) << " ";
 30:     cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n');
 31: }