Where there is a dream ,there is hope

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  64 Posts :: 0 Stories :: 8 Comments :: 0 Trackbacks

常用链接

留言簿(1)

我参与的团队

搜索

  •  

最新评论

阅读排行榜

评论排行榜

条款九,在删除选项中仔细选择
*在一个标准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;
}
posted on 2011-10-11 16:24 IT菜鸟 阅读(172) 评论(0)  编辑 收藏 引用 所属分类: EFFECTIVE-STL学习笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理