vector的remove( )函数

leon850515 2007-01-16 08:55:02
#include<vector>
#include<algorithm>

using namespace std;

void PrintIt( char * &AString )
{
cout << AString << endl;
}

int main( )
{
vector< char * > Birds;
vector< char * >::iterator NewEnd;

Birds.push_back( "cockatoo" );
Birds.push_back( "galah" );
Birds.push_back( "cockatoo" );
Birds.push_back( "rosella" );
Birds.push_back( "king parrot" );
cout << "Original vector" << endl;
for_each( Birds.begin( ), Birds.end( ), PrintIt );

NewEnd = remove( Birds.begin( ), Birds.end( ), "cockatoo" );
cout << endl << "after removing cockatoo vector" << endl;
for_each( Birds.begin( ), Birds.end( ), PrintIt );

return 0;
}

运行环境:Visual Studio 2005

运行结果:
Original vector
cockatoo
galah
cockatoo
rosella
king parrot

after removing cockatoo vector
galah
rosella
king parrot
rosella
king parrot

为什么执行remove操作后,vector的元素内容变为上述结果呢?
...全文
3723 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
sqhily2008 2012-09-12
  • 打赏
  • 举报
回复
coll.erase(remove(coll.begin(),   coll.end(),   val),   coll.end());   
richbirdandy 2008-08-22
  • 打赏
  • 举报
回复
lz大概已经转行了 呵呵
chlaws 2008-08-22
  • 打赏
  • 举报
回复 1
LZ啊 你对remove 算法不了解啊
你要删除指定元素 那么就采用vector.erase(remove) 这种方式

remove 算法 并不是表面语义的移除的意思,它是采用了后继覆盖的方式把前边找到的指定的值进行覆盖掉
关于详细的信息有空看下Effective STL
jieao111 2008-08-22
  • 打赏
  • 举报
回复
乖乖,这个帖子发出的时候,
jay的Fans 2008-08-22
  • 打赏
  • 举报
回复
LZ应该看看vector容器的删除操作是怎么实现的
weird213 2008-08-22
  • 打赏
  • 举报
回复
实现楼主功能的程序应该是:
#include <vector>
#include <algorithm>
#include <iostream>
#include <conio.h>
using namespace std;

void PrintIt( char * &AString )
{
cout << AString << endl;
}

int main( )
{
vector < char * > Birds;
//vector < char * > ::iterator NewEnd;
Birds.push_back( "cockatoo " );
Birds.push_back( "galah " );
Birds.push_back( "cockatoo " );
Birds.push_back( "rosella " );
Birds.push_back( "king parrot " );
cout << "Original vector " << endl;
for_each( Birds.begin( ), Birds.end( ), PrintIt );

Birds.erase (remove(Birds.begin (),Birds.end(),"cockatoo "),Birds.end ());
//NewEnd = remove( Birds.begin( ), Birds.end( ), "cockatoo " );
cout << endl << "after removing cockatoo vector " << endl;
for_each( Birds.begin( ), Birds.end( ), PrintIt );
getch();
return 0;
}
todototry 2007-01-17
  • 打赏
  • 举报
回复
remove只是把待删除的元素移位到了数据序列的尾部,而并未真正的删除元素,
remove函数返回待删除序列的首个元素的迭代器
比如说,123123,remove(1),那么序列变成了232311,返回一个迭代器,只想第一个1的位置,这是配合erase()函数,把这个迭代器作参数,可以把后,面两个1删除,数据序列变成了2323
达到目的
个人意见,高手多多指教
pottichu 2007-01-17
  • 打赏
  • 举报
回复
如果你真的想删除东西的话就在类似remove的算法后接上erase

http://www.stlchina.org/documents/EffectiveSTL/files/item_32.html
taodm 2007-01-17
  • 打赏
  • 举报
回复
看effective stl item 32
DonaldKnuth 2007-01-16
  • 打赏
  • 举报
回复
哦,上面写错了一点,应该是
coll.erase(remove(coll.begin(), coll.end(), val), coll.end());
对你的程序而言这样写就可以了:
#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

void PrintIt( char * &AString )
{
cout << AString << endl;
}

int main( )
{
vector< char * > Birds;
vector< char * >::iterator NewEnd;

Birds.push_back( "cockatoo" );
Birds.push_back( "galah" );
Birds.push_back( "cockatoo" );
Birds.push_back( "rosella" );
Birds.push_back( "king parrot" );
cout << "Original vector" << endl;
for_each( Birds.begin( ), Birds.end( ), PrintIt );

Birds.erase(remove( Birds.begin( ), Birds.end( ), "cockatoo" ), Birds.end( ));
cout << endl << "after removing cockatoo vector" << endl;
for_each( Birds.begin( ), Birds.end( ), PrintIt );

return 0;
}
DonaldKnuth 2007-01-16
  • 打赏
  • 举报
回复
一般是erase 和remove 配合使用的,格式如下:
vector<int> coll;
...........
coll.erase(remove(coll.begin(), coll.end(), val)); //val为要删除的对象
晨星 2007-01-16
  • 打赏
  • 举报
回复
还有,你标题中的“vector的remove()函数”提法不当,呵呵。
你用的是范化的标准算法:std::remove,vector的只有vector::erase。
晨星 2007-01-16
  • 打赏
  • 举报
回复
执行remove之后,你可以手工删除从NewEnd开始到Birds.end()之前的所有元素,而且效率不会低。
另外,STL的标准算法中,除了使用迭带器适配器(比如back_inserter等)之外,几乎没有改变容器实际大小的,remove也不例外。
晨星 2007-01-16
  • 打赏
  • 举报
回复
这好像不违反标准库的规范。
std::remove只保证你返回的那个NewEnd之前不再有被“remove”的元素。
mochen5460 2007-01-16
  • 打赏
  • 举报
回复
是这样的啊,remove后,后面的前移

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧