C++ examples for STL Algorithm:replace
Replacing values from a sequence using algorithms replace
#include <iostream> #include <algorithm> #include <vector> #include <iterator> // ostream_iterator using namespace std; int main() //from w ww . ja v a 2 s. c o m { const int SIZE = 10; int a[ SIZE ] = { 10, 2, 10, 4, 16, 6, 14, 8, 12, 10 }; ostream_iterator< int > output( cout, " " ); vector< int > v1( a, a + SIZE ); // copy of a cout << "Vector v1 before replacing all 10s:\n "; copy( v1.begin(), v1.end(), output ); // replace all 10s in v1 with 100 replace( v1.begin(), v1.end(), 10, 100 ); cout << "\nVector v1 after replacing 10s with 100s:\n "; copy( v1.begin(), v1.end(), output ); }