私は2つのセットを持ってtest
おり、例えばそこに存在test1
する要素を削除する必要がありますtest
test1
test
1,2,3,4,5 を含み、3,5,6,7 を含む場合test1
: 関数を実行する必要があるtest
ため、1,2,4 のみが残ります。
私はset_intersectionを発見しました - それは物事を行うための最良の方法ですか?
編集:お詫び。両方test
でtest1
あるset<int>
私は2つのセットを持ってtest
おり、例えばそこに存在test1
する要素を削除する必要がありますtest
test1
test
1,2,3,4,5 を含み、3,5,6,7 を含む場合test1
: 関数を実行する必要があるtest
ため、1,2,4 のみが残ります。
私はset_intersectionを発見しました - それは物事を行うための最良の方法ですか?
編集:お詫び。両方test
でtest1
あるset<int>
set
非定数イテレータはありません。uselist
および remove_if。
#include <iostream>
#include <list>
#include <algorithm>
int
main(int argc, char* argv[]) {
std::list<int> test = {1,2,3,4,5};
std::list<int> test1 = {3,5,6,7};
std::list<int>::iterator ri = std::remove_if(test.begin(), test.end(), [&](int x) -> bool {
return std::find(test1.begin(), test1.end(), x) != test1.end();
});
test.erase(ri, test.end());
std::for_each(test.begin(), test.end(), [&](decltype(test)::value_type x) {
std::cout << x << std::endl;
});
return 0;
}