
移除[First, Last]区间中每一个与Val相等的元素

移除[First, Last]区间中每一个令_Pred(elem)结果为true的元素
都返回被改动的序列的新逻辑终点
这些算法会把原本置于后面的未移除元素向前移动,覆盖被移除元素
由于元素会被改动,这些算法不能用于associative或unordered容器
list提供了一个效果相同但性能更好的成员函数remove().
复杂度:线性,执行比较动作或调用_Pred()numElems次
使用例子:
template<typename T>
inline void INSERT_ELEMENTS(T& coll, int first, int last)
{
for (int i = first; i <= last; ++i)
{
coll.insert(coll.end(), i);
}
}
template<typename T>
inline void PRINT_ELEMENTS(const T & coll, const string& optcstr = "")
{
cout << optcstr;
for (auto elem : coll)
{
cout << elem << ' ';
}
cout << endl;
}
int main()
{
list<int>a;
INSERT_ELEMENTS(a, 2, 6);
INSERT_ELEMENTS(a, 4, 9);
INSERT_ELEMENTS(a, 1, 7);
PRINT_ELEMENTS(a);
auto pos = remove(a.begin(), a.end(), 5);
PRINT_ELEMENTS(a, "size not changed: ");
a.erase(pos, a.end());
PRINT_ELEMENTS(a, "size changed: ");
a.erase(remove_if(a.begin(), a.end(), [](int elem) {return elem < 4; }), a.end());
PRINT_ELEMENTS(a, "<4 removed: ");
}
