Filtering Values Outside a Given Range using the remove_copy_if function found in the <algorithm> - C++ STL Algorithm

C++ examples for STL Algorithm:remove_copy_if

Description

Filtering Values Outside a Given Range using the remove_copy_if function found in the <algorithm>

Demo Code

#include <algorithm>
#include <vector>
#include <iostream>
#include <iterator>

using namespace std;

struct MyRange/*from   w w  w.j  av  a 2 s.  com*/
{
  MyRange(int min, int max): min_(min), max_(max)
  {}
  
  bool operator()(int x) {
    return (x < min_) || (x > max_);
  }
  
  int min_;
  int max_;
};

int main()
{
  vector<int> v;
  v.push_back(6);
  v.push_back(12);
  v.push_back(18);
  v.push_back(24);
  v.push_back(30);
  remove_copy_if(v.begin(), v.end(), ostream_iterator<int>(cout, "\n"), MyRange(10,25));
}

Result


Related Tutorials