更易型算法(Manipulating Algorithms)
一、移除元素(Removing)元素
- remove(iter_begin, iter_end, value) 删除某个区间的元素,但是不会改变该群集的元素数量
- coll.erase(remove(coll.begin(), coll.end(), value), coll.end()) 删除区间里的元素,同时会改变元素数量
注:注意两者的区别
1 #include <iostream> 2 #include <list> 3 #include <algorithm> 4 #include <iterator> 5 6 using namespace std; 7 8 int main() 9 { 10 list<int> coll; 11 for(int i=1; i < 6; ++i) { 12 coll.push_back(i); 13 coll.push_back(i); 14 } 15 16 copy(coll.begin(), coll.end(), 17 ostream_iterator<int>(cout, " ")); 18 cout << endl; 19 20 coll.erase(remove(coll.begin(), coll.end(), 3), 21 coll.end()); 22 copy(coll.begin(), coll.end(), 23 ostream_iterator<int>(cout, " ")); 24 cout << endl; 25 }