1

私は2つのセットを持ってtestおり、例えばそこに存在test1する要素を削除する必要がありますtesttest1

test1,2,3,4,5 を含み、3,5,6,7 を含む場合test1: 関数を実行する必要があるtestため、1,2,4 のみが残ります。

私はset_intersectionを発見しました - それは物事を行うための最良の方法ですか?

編集:お詫び。両方testtest1あるset<int>

4

2 に答える 2

0

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;
}
于 2013-06-19T03:16:23.277 に答える