设为首页 加入收藏

TOP

标准模板库(STL)List介绍(十七)
2011-06-07 12:31:23 来源: 作者: 【 】 浏览:9185
Tags:标准 模板 STL List 介绍
 
  
使用STL通用算法remove()从list中删除元素  
通用算法remove()使用和list的成员函数不同的方式工作。一般情况下不改变容器的大小。  
  
/*  
|| Using the generic remove algorithm to remove list elements  
*/  
#include <string>  
#include <list>  
#include <algorithm>  
   
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");  
   
     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 list  
cockatoo  
galah  
cockatoo  
rosella  
king parrot  
  
  
List according to new past the end iterator  
galah  
rosella  
king parrot  
  
  
Original list now. Care required!  
galah  
rosella  
king parrot  
rosella  
king parrot  
  
通用remove()算法返回一个指向新的list的结尾的iterator。从开始到这个新的结尾(不含新结尾元素)的范围 包含了remove后剩下所有元素。你可以用list成员函数erase函数来删除从新结尾到老结尾的部分。  
  
  
首页 上一页 14 15 16 17 18 下一页 尾页 17/18/18
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇 C++语言的url encode 和decode 下一篇C++ map的基本操作和使用

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: