游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1254|回复: 1

vector remove

[复制链接]

17

主题

34

帖子

34

积分

注册会员

Rank: 2

积分
34
发表于 2007-7-20 21:34:00 | 显示全部楼层 |阅读模式
我的测试代码为:

  1. vector<int> testVector;
  2.        
  3.         testVector.push_back(1);
  4.         testVector.push_back(2);
  5.         testVector.push_back(3);
  6.         testVector.push_back(1);
  7.         testVector.push_back(4);

  8.         remove(testVector.begin(), testVector.end(), 1);

  9.         for(iter = testVector.begin(); iter < testVector.end(); iter++)
  10.         {
  11.                 cout << *iter << "  ";
  12.         }
复制代码

输出的事2 3 4 1 4
why?

103

主题

1432

帖子

1458

积分

金牌会员

Rank: 6Rank: 6

积分
1458
QQ
发表于 2007-7-21 23:12:00 | 显示全部楼层

Re:vector remove

remove 算法并不删除元素。而是将保留的元素前移,返回终点的迭代器。
应该和成员函数erase 连用:

testVector.erase(remove(testVector.begin(),testVector.end(),1);
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-25 18:12

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表