/*
Demonstration of the replace_if algorithm.
This program also shows how an ostream_iterator can be used with
the copy algorithm to copy the contents of a vector to cout.
NDE, 2014-01-07
*/
#include <vector>
#include <iterator>
#include <iostream>
#include <algorithm>
using namespace std;
bool is_negative(int value)
{
return value < 0;
}
int main()
{
// Populate a vector with some integers
vector<int> v;
for (int i = -5; i <= 5; ++i) {
v.push_back(i);
}
// Display vector contents
ostream_iterator<int> out(cout, " ");
cout << "Before: ";
copy(v.begin(), v.end(), out);
cout << endl;
// Replace negative values with zero
replace_if(v.begin(), v.end(), is_negative, 0);
// Display vector contents again
cout << "After: ";
copy(v.begin(), v.end(), out);
cout << endl;
return 0;
}