Standard C++ Library Class Reference
Complexity
Exactly last - first comparisons or applications of the corresponding predicate are done.
Example
//
// replace.cpp
//
 #include <algorithm>
 #include <vector>
 #include <iterator>
 #include <iostream.h>
 template<class Arg>
 struct all_true : public unary_function<Arg, bool>
 {
 bool operator()(const Arg&){ return 1; }
 };
 int main()
 {
 //Initialize a vector with an array of integers
 int arr[10] = {1,2,3,4,5,6,7,8,9,10};
 vector<int> v(arr, arr+10);
 //Print out original vector
 cout << "The original list: " << endl << " ";
 copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
 cout << endl << endl;
 //Replace the number 7 with 11
 replace(v.begin(), v.end(), 7, 11);
 // Print out vector with 7 replaced, 
 // s.b. 1 2 3 4 5 6 11 8 9 10
 cout << "List after replace " << endl << " ";
 copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
 cout << endl << endl;
 //Replace 1 2 3 with 13 13 13
 replace_if(v.begin(), v.begin()+3, all_true<int>(), 13);
 // Print out the remaining vector, 
 // s.b. 13 13 13 4 5 6 11 8 9 10
 cout << "List after replace_if " << endl << " ";
 copy(v.begin(),v.end(),ostream_iterator<int>(cout," "));
 cout << endl << endl;
 return 0;
 }
Output :
The original list:










