Josuttis の「The C++ Standard Library, 2nd ed.」を読んでいました。セクション 6.7.1 で、著者は、以下に示すコードが予期しない結果をもたらすと説明しています。std::remove()
どのように機能するのか、なぜこの奇妙な結果が得られるのかはまだわかりません。std::erase()
(実際に要素を削除するにはを使用する必要があることは理解していましたが、実際には& `std::remove() をlist::erase()
組み合わせて使用するよりも使用した方がよいでしょう)。std::remove()
list<int> coll;
// insert elements from 6 to 1 and 1 to 6
for (int i=1; i<=6; ++i) {
coll.push_front(i);
coll.push_back(i);
}
// print
copy (coll.cbegin(), coll.cend(), // source
ostream_iterator<int>(cout," ")); // destination
cout << endl;
// remove all elements with value 3
remove (coll.begin(), coll.end(), // range
3); // value
// print (same as above)
結果は
pre: 6 5 4 3 2 1 1 2 3 4 5 6
post: 6 5 4 2 1 1 2 4 5 6 5 6 (???)