CPP Blog

C++博客 首页 新随笔 联系 聚合 管理
  10 Posts :: 0 Stories :: 0 Comments :: 0 Trackbacks
How to delete element from Vector:

There is tricky thing for deleting in vector loop.

The erase method returns the next element after the one you just erased. So you can use that to continue in your loop.

vector c;
iterator i = c.begin();
     while(i != c.end()){
          if (i == something)
          {
                //i = i.erase();
                 i = c.erase(i);
          } else {
               i++;
          }
}

OR:

vector c;
for(vector<type>::iterator i=c.begin(); i != c.end(); )
{
     if (some condition)
     {
           //i = i.erase();
             i = c.erase(i);
     }
     else
       ++i;
}




posted on 2010-02-18 06:02 SimonHan 阅读(739) 评论(0)  编辑 收藏 引用 所属分类: C++

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