Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::remove_if

 

std::remove_if is an STL algorithm to removed elements from a container.

 

std::remove_if does the following:

 

 

#include <algorithm>
#include <cassert>
#include <functional>
#include <vector>

int main()
{
  //Create a std::vector with some values in it
  std::vector<int> v;
  v.push_back(0);
  v.push_back(1);
  v.push_back(2);

  //Check
  assert(v.size() == 3);
  assert(v[0]==0);
  assert(v[1]==1);
  assert(v[2]==2);

  //Remove the '1'
  const std::vector<int>::iterator new_end
   = std::remove_if(v.begin(),v.end(),std::bind2nd(std::equal_to<int>(),1));

  //Resize the std::vector
  v = std::vector<int>(v.begin(),new_end);

  //Check
  assert(v.size() == 2);
  assert(v[0]==0);
  assert(v[1]==2);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict