vector中の特定の条件を満たす要素だけを抽出
まだC++03な私にはcopy_ifなんてないので、remove_copy_ifを使って代用。
すごく…めんどいです…
#include<iostream> #include<vector> #include<algorithm> #include <functional> int main() { //テストデータ std::vector<int> x,y; x.push_back(1); x.push_back(3); x.push_back(4); x.push_back(5); x.push_back(3); //条件用Functor struct IsNotThree : public std::unary_function<int, bool> { bool operator()(int x) const{return x!=3;} }; //xの要素の3以外をyにコピー std::remove_copy_if(x.begin(), x.end(), std::back_inserter(y), std::not1(IsNotThree())); //表示 struct Show{void operator()(int x){std::cout << x << std::endl;}}; std::for_each(y.begin(), y.end(), Show()); return 0; }
実行結果
1 4 5
参考