1
remove_if
とvector<T>::erase
の仕組みを理解しようとしています。私は以下のコード(奇数の要素を削除しようとしている)を持っている:アルゴリズムでベクトル内の特定の要素を消去する
v2.clear();
v2 = { 10, 20, 21, 30, 31, 33, 44, 66, 67 }; //vector<int>
cout << "v2 is now: " << endl;
printCollection(v2);
cout << "Trying to remove odds from v2: " << endl;
auto what = remove_if(begin(v2), end(v2), [](int elem) {return elem % 2 != 0;});
v2.erase(begin(v2), what);
printCollection(v2);
、ここでは出力されます:
v2 is now:
10 20 21 30 31 33 44 66 67
Trying to remove odds from v2:
33 44 66 67
何が起こっていますか?