// replace.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>
using std::cout;
using std::endl;
bool greater9( int );
int main()
{
const int SIZE = 10;
int a[ SIZE ] = { 10, 2, 10, 4, 16, 6, 14, 8, 12, 10 };
std::ostream_iterator< int > outputIt( cout, " " );
std::vector< int > v1( a, a + SIZE );
cout<<"Vector v1 before replacing all 10s:/n ";
std::copy( v1.begin(), v1.end(), outputIt );
std::replace( v1.begin(), v1.end(), 10, 100 );
cout<<"/nVector v1 after replacing 10s with 100s:/n ";
std::copy( v1.begin(), v1.end(), outputIt );
std::vector< int > v2( a, a + SIZE );
std::vector< int > c1( SIZE );
cout<<"/n/nVector v2 before replacing all 10s "
<<"and copying:/n ";
std::copy( v2.begin(), v2.end(), outputIt );
std::replace_copy( v2.begin(), v2.end(), c1.begin(), 10, 100 );
cout<<"/nVector c1 after replacing all 10s in v2:/n ";
std::copy( c1.begin(), c1.end(), outputIt );
std::vector< int > v3( a, a + SIZE );
cout<<"/n/nVector v3 before replacing values greater than 9:/n ";
std::copy( v3.begin(), v3.end(), outputIt );
std::replace_if( v3.begin(), v3.end(), greater9, 100 );
cout<<"/nVector v3 after replacing all values greater"
<<"/nthan 9 with 100s:/n";
std::copy( v3.begin(), v3.end(), outputIt );
std::vector< int > v4( a, a + SIZE );
std::vector< int > c2( SIZE );
cout<<"/n/nVector v4 before replacing all values greater "
<<"/nthan 9 and copying:/n";
std::copy( v4.begin(), v4.end(), outputIt );
std::replace_copy_if( v4.begin(), v4.end(), c2.begin(), greater9, 100 );
cout<<"/nVector c2 after replacing all values greaer "
<<"/nthan 9 in v4:/n ";
std::copy( c2.begin(), c2.end(), outputIt );
cout<< endl;
return 0;
}
bool greater9( int x )
{
return x> 9;
}