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