vector的remove( )函数
#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的元素内容变为上述结果呢?