條款九,在刪除選項中仔細選擇
*在一個標準STL容器中去掉值為1963的對象,若是一個連續內存容器,最好的方法是erase-remove
c.erase ( remove( c.begin(), c.end(), 1963 ), c.end() );//當C時vector,string ,deque時,這是一處特定值得元素的最佳方法
*當C是標準關聯容器的時候(map, set)使用任何叫做remove的東西都是完全錯誤的
而應該直接采用c.erase(1963)對數的高效時間
*但如果操作是從C中除去每個有特定值的物體
bool bandValue(int x)
對于序列容器(vector, string,deque,list)我們要做的只是把每個remove替換為remove_if然后就OK了
c.erase( remove_if(c.begin(), c.end(), badValue), c.end())//vector,string,deque
c.remove_if( badValue ) //list
對于標準關聯容器,
AssocContainner<int> c;
for(AssoContainer<int>::iterator i = c.begin();
i!= c.end(); )
{
if(badValue(*i)) c.erase(i++);
else ++i;
}