设为首页 加入收藏

TOP

标准模板库(STL)List介绍(八)
2011-06-07 12:31:23 来源: 作者: 【 】 浏览:9183
Tags:标准 模板 STL List 介绍
 
  
使用STL通用算法find()在list中查找对象  
  
  我们如何在list中查找东西呢?STL的通用算法find()和find_if()可以做这些。 就象for_each(), count(), count_if() 一样,这些算法也使用iterator范围,这个范围指出一个list或任意 其他容器中的一部分来处理。通常首iterator指着开始的位置,次iterator指着停止处理的地方。 由次iterator指出的元素不被处理。  
这是find()如何工作:  
  
/*  
|| How to find things in an STL list  
*/  
#include <string>  
#include <list>  
#include <algorithm>  
  
int main (void) {  
     list<string> Fruit;  
     list<string>::iterator FruitIterator;  
  
     Fruit.push_back("Apple");  
     Fruit.push_back("Pineapple");  
     Fruit.push_back("Star Apple");  
  
     FruitIterator = find (Fruit.begin(), Fruit.end(), "Pineapple");  
    
     if (FruitIterator == Fruit.end()) {  
         cout << "Fruit not found in list" << endl;  
     }  
     else {  
         cout << *FruitIterator << endl;  
     }  
}  
  
输出是:  
  
Pineapple  
  
如果没有找到指出的对象,就会返回Fruit.end()的值,要是找到了就返回一个指着找到的对象的iterator  
  
  
首页 上一页 5 6 7 8 9 10 11 下一页 尾页 8/18/18
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇 C++语言的url encode 和decode 下一篇C++ map的基本操作和使用

评论

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