C++-----------STL经典网址

http://www.sgi.com/tech/stl/stl_index.html



#include<iostream>
#include<list>
using namespace std;
list<int> * generate(int n=0)
{
        list<int>* iter =new list<int>( n );
        return iter;
}
int main()
{
        list<int>* intList=generate(10);
        int i=1;
        for(list<int>::iterator iter=intList->begin();iter!=intList->end();iter++)
        {
                *iter=i++;
        }
        for(list<int>::iterator iter=intList->begin();iter!=intList->end();iter++)
        {
                printf("%5d",*iter);
        }
        cout<<endl;
        getchar();
        return 0;
}

---------------------------find------find_if ------------------------ example----------------------

// disable warning C4786: symbol greater than 255 character,
// okay to ignore
///#pragma warning(disable: 4786)(WIN32);


#include <algorithm>
#include <iostream>
#include <list>
#include <assert.h>
using namespace std;


// returns true if n is an odd number
int IsOdd( int n)
{
    return n % 2 ;
}


int main()
{
    const int ARRAY_SIZE = 8 ;
    int IntArray[ARRAY_SIZE] = { 1, 2, 3, 4, 4, 5, 6, 7 } ;
    int *location ;   // stores the position of the first
                      // element that is an odd number
    int i ;


        // print content of IntArray
    cout << "IntArray { " ;
    for(i = 0; i < ARRAY_SIZE; i++)
        cout << IntArray[i] << ", " ;
    cout << " }" << endl ;


cout<<"--------------------test find-------------"<<endl;
     location =find(IntArray,IntArray+ARRAY_SIZE,7);///参数:开始数据的地址(一个指针),结束数据的地址,在这个范围内要查找的具体数据. 返回的是第一个找到数据的地址(一个指针),故这里用*int location表示. 如没有找到返回NULL。
     cout<<"location is find: "<<*location<<endl;
     
list<int> L;
L.push_back(3);
L.push_back(1);
L.push_back(7);
list<int>::iterator result = find(L.begin(), L.end(), 7);///同理,iterator迭代器,是list<int>构建的游标类型,result也是一个指针类型的游标.
assert(result == L.end() || *result == 7);
cout<<"---------list<int> result is:"<<*result<<endl;
 
     cout<<"--------------------test find_if-------------"<<endl;
    // Find the first element in the range [first, last -1 ]
    // that is an odd number
    location = find_if(IntArray, IntArray + ARRAY_SIZE, IsOdd);///开始结束数据的指针,IsOdd符合条件的一个方法(返回类型是bool),执行时返回第一个true时的数据,如没有找到则返回最后一个数据.


    IntArray[0] =2;
    IntArray[1] =4;
    IntArray[2] =6;
    IntArray[3] =8;
    IntArray[4] =10;
    IntArray[5] =12;
    //IntArray[6] =11;  ///test.
    IntArray[6] =14;
    IntArray[7] =16;
    location = find_if(IntArray, IntArray + ARRAY_SIZE-1, IsOdd) ;
    cout << "First odd element " << *location<< " is at location " << location - IntArray << endl;
             return 0;
 }
    
    /*
find_if定义在<algorithm>头文件中。


我们首先看下在STL中find_if是怎样实现的,然后就能写出针对各种类型的find_if函数.
//定义在STL实现代码stl_algo.h中


template <class InputIterator, class Predicate>


InputIterator find_if(InputIterator first, InputIterator last,Predicate pred)


......{


       while (first != last && !pred(*first)) ++first;


       return first;


}


可见STL是把find_if定义为一个函数模板,该函数模板接收三个参数,前两个类型InputItearator是输入的迭代器,在两个迭代器之间进行查找。Predicate是用于比较的断言函数。在find-if实现中,断言函数pred将迭代器当前所指对象默认当做它的参数,判断并查找。


pred是条件谓词,可以是函数,也可以是类。
 类要重载运算符()带一个类型参数。
 当满足pred(*inputiter)==true时,返回迭代器位置,
 否则返回last。*/*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值