泛型算法系列19:remove_if()&&remove_copy_if()

// remove_if.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include /* ハ莎・ original element sequence: 0 1 1 2 3 5 8 13 21 34 sequence after applying remove_if < 10: 13 21 34 sequence after applying remove_copy_if even: 1 1 3 5 13 21 */ using namespace std; template inline _OI my_remove_copy_if(_II _F, _II _L, _OI _X, _Pr _P) { for (; _F != _L; ++_F) if (!_P(*_F)) *_X++ = *_F; return (_X); } template inline _FI my_remove_if(_FI _F, _FI _L, _Pr _P) { _F = find_if(_F, _L, _P); if (_F == _L) return (_F); else { _FI _Fb = _F; return (my_remove_copy_if(++_F, _L, _Fb, _P)); } } class EvenValue { public: bool operator()( int value ) { return value % 2 ? false : true; } }; int main(int argc, char* argv[]) { int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }; vector< int >::iterator iter; vector< int > vec( ia, ia+10 ); ostream_iterator< int > ofile( cout, " " ); cout << "original element sequence:/n"; copy( vec.begin(), vec.end(), ofile ); cout << '/n'; iter = remove_if( vec.begin(), vec.end(), bind2nd(less(),10) ); vec.erase( iter, vec.end() ); cout << "sequence after applying remove_if < 10:/n"; copy( vec.begin(), vec.end(), ofile ); cout << '/n'; vector< int > vec_res( 10 ); iter = remove_copy_if( ia, ia+10, vec_res.begin(), EvenValue() ); cout << "sequence after applying remove_copy_if even:/n"; copy( vec_res.begin(), iter, ofile ); cout << '/n'; return 0; }

你可能感兴趣的:(C++,STL)