// あるコンテナから指定した条件を満たす要素だけを // コンテナから削除する。 #include <iostream> #include <string> #include <vector> #include <algorithm> #include <functional> using namespace std; // matching pattern type definition typedef enum MATCHING_PATTERN_ { COMPLETE, PARTIAL } MATCHING_PATTERN; // predicate class pred : std::unary_function<string, bool> { public: pred( const char *pcName = "" , MATCHING_PATTERN mp = COMPLETE ) : name_( pcName ), mp_( mp ) {} bool operator()( const string& str ) const { bool b_ret = true; switch ( mp_ ) { case COMPLETE: b_ret = ( str == name_ ); break; case PARTIAL: b_ret = ( str.find( name_ ) != str.npos ); break; default: b_ret = true; break; } return b_ret; } void setName( const char *pcName ) { name_ = pcName; } void setMatchingPattern( MATCHING_PATTERN mp ) { mp_ = mp; } private: string name_; MATCHING_PATTERN mp_; }; int main() { vector<string> strVec; strVec.push_back( "foo" ); strVec.push_back( "bar" ); strVec.push_back( "hogehoge" ); strVec.push_back( "aaa" ); strVec.push_back( "bbb" ); strVec.push_back( "ccc" ); // partial matching pred pd( "b", PARTIAL ); strVec.erase( remove_if( strVec.begin(), strVec.end(), pd ), strVec.end() ); cout << "copied vector size = " << strVec.size() << endl; for ( int i=0; i < strVec.size( ) ; i++ ) { cout << strVec[i] << endl; } return 0; } /* 条件を満たせたば remove するので、 狙ったものがコンテナから削除されることになる。 この例では "b" が含まれる文字列をコンテナから削除している。 */ // end of file