Select Numbers less than 10 : copy_if « STL Algorithms Modifying sequence operations « C++






Select Numbers less than 10

  
#include <algorithm>
#include <functional>
#include <vector>
#include <iostream>

using namespace std;

template <class T>
void print(T& c){
   for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
      std::cout << *i << endl;
   }
}


template<class InputIterator, class OutputIterator, class Predicate >
OutputIterator copy_if( InputIterator start, InputIterator stop,OutputIterator out, Predicate select ){
   while( start != stop ){
      if( select( *start ) )
         *out++ = *start;
      ++start;
   }
   return out;
}

int main( )
{
   const int numbers = 7;
   const int num[numbers] = { 5, 0, 3, 2, 1, 4, 7 };
   vector<int> v1( num, num+numbers );
   print( v1 );

   vector<int> v2;
   copy_if( v1.begin(), v1.end(), back_inserter( v2 ),bind2nd( less<int>(), 10 ) );
   print( v2 );
}
  
    
  








Related examples in the same category