您的位置:首页 > 编程语言 > C语言/C++

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

2013-07-07 18:34 411 查看
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。*/*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: