an example of using STL alg replace_if with function, function object, and lambda


👍 g++ -std=c++11 replace_if.cpp
👍 ./a.out
3 1 4 1 5 9 2 5 
7 7 4 7 5 9 7 5 
5 5 4 5 5 9 5 5 
4 4 4 4 5 9 4 5

👍 cat replace_if.cpp 
#include <iostream>
#include <vector>
using namespace std;

template<typename I>
void prt(I i, I end) {
  for(; i != end; ++i)
    cout << *i << ' ';
  cout << endl;
}

bool f1(int n) {return n < 4;}

class f2 {
public:
  f2() {}
  bool operator()(int n)
    {return n < 4;}
};

int main() {
  const vector c{3,1,4,1,5,9,2,5};
  vector v = c;
  prt(v.begin(), v.end());

  replace_if(
    v.begin(),
    v.end(),
    [](int n)->bool {return n < 4;},
    7);
  prt(v.begin(), v.end());

  v = c;
  replace_if(
    v.begin(),
    v.end(),
    f1,
    5);
  prt(v.begin(), v.end());

  v = c;
  replace_if(
    v.begin(),
    v.end(),
    f2(),
    4);
  prt(v.begin(), v.end());
}