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。*/*/
#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。*/*/
相关文章推荐
- STL + c++ + 模板 + 重要思维 + 基础算法+ 经典算法 + 经典实例 + 编程总结+ 心得+ 入门必会 + 知识点汇总。+string +dfs +bfs等重要算法
- [STL]C++中placement new操作符(经典)
- 一些C/C++开源项目网址 经典开源C/C++项目 100个开源C/C++项目中的bugs
- 算法竞赛入门经典_5 c++与STL入门
- 刘汝佳 算法竞赛入门经典 第5章 C++与STL入门
- STL + c++ + 模板 + 重要思维 + 基础算法+ 经典算法 + 经典实例 + 编程总结+ 心得+ 入门必会 + 知识点汇总。
- [经典面试题][百度]c++实现STL中的string类
- 【算法竞赛入门经典第五章:C++与STL入门
- C++_STL经典书单
- C++STL学习经典
- C++ STL学习经典
- 多年c++ stl的经典总结
- C++ STL轻松导学(经典收藏)
- C++经典书籍解读
- C++ STL容器参考手册 (总册)
- C++中实现.NET风格的委托(经典收藏)
- 【C++ STL应用与实现】26: 如何使用std::for_each以及基于范围的for循环 (since C++11)
- C++比较经典的书籍
- 【C++ STL】细数C++ STL 的那些事---vector (动态数组)
- STL 在 C++ 中的应用