使用STL通用算法remove()从list中删除元素
#include<iostream>#include<string>#include<list>#include<algorit...
#include <iostream>#include <string>#include <list>#include <algorithm>using namespace std; PrintIt(string& AString) { cout << AString << endl; } int main (void) { list<string> Birds; list<string>::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 list" << endl; for_each(Birds.begin(), Birds.end(), PrintIt); NewEnd = remove(Birds.begin(), Birds.end(), "cockatoo");//这里的NewEnd起什么作用 cout << endl << "List according to new past the end iterator" << endl; for_each(Birds.begin(), NewEnd, PrintIt); cout << endl << "Original list now. Care required!" << endl; for_each(Birds.begin(), Birds.end(), PrintIt);}输出结果将为:Original listcockatoogalahcockatoorosellaking parrot List according to new past the end iteratorgalahrosellaking parrot Original list now. Care required!galahrosellaking parrotrosellaking parrot//这个结果怎么出来滴呀!
展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询