1. 100多种算法
2.函数对象(function objects )
3.函数适配器(function adapters )
4.三个头文件
#include <algorithm>
#include <numeric>
#include <functional>
预定义函数对象
negate<type>()
plus<type>()
minus<type>()
multiplies<type>()
divides<type>()
modulus<type()
equal_to<type>()
not_equal_to<type>()
less<type>()
greater<type>()
less_equal<type>()
greater_equal<type>()
logical_not<type>()
logical_and<type>()
logical_or<type>()
预定义的函数适配器
bind1st( op, value )
bind2nd( op, value)
not1( op )
not2( op )
mem_fun_ref(op)
mem_fun(op)
ptr_fun(op)
算法分类
1.非修改性算法(nonmodifying algorithm )
for_each() 对每个元素执行某操作
count() 返回元素个数
countif() 返回满足某一准则(条件)的元素数
min_element() 返回最小值元素(以一个迭代器表示)
max_element() 返回最大值元素(以一个迭代器表示)
find() 搜寻等于某值的第一个元素
find_if() 搜寻满足某个准则的第一个元素
search_n() 搜寻具有某特性的第一个元素
search() 搜寻某个子区间第一次出现位置
find_end() 搜寻某个子区间最后一次出现位置
find_first_of() 搜寻等于“某数个值之一”的第一个元素
adjacent_find() 搜徐连续两个相等(或符合特定准则)的元素
equal() 判断两区间是否相等
mismatch() 返回两个序列的各组对应元素中,第一个不相等元素
lexicographical_compare() 判断某一序列在“字段顺序”下是否小于另一序列
2.修改性算法( modifying algorithms )
3.移除性算法(removing algorithms)
4.变序性算法(mutating algorithms)
5.排序算法(sorting algorithms)
6.已序区间算法(sorted range algorithms )
7.数值算法( numeric algorithms )
//nonmodifying algorithms
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
#include <vector>
#include <deque>
#include <list>
#include <functional>
using namespace std;
template<typename T>
void Print(const T& t)
{
for(typename T::const_iterator itr=t.begin(); itr!=t.end(); ++itr)
{
cout<<*itr<<' ';
}cout<<endl;
}
void Nonmodifying( vector<int>& vec )
{
vec.push_back(4);
vec.push_back(4);
cout<<"source: ";
Print(vec);
cout<<"count 4:"<<count(vec.begin(), vec.end(), 4)<<endl;
cout<<"count <4:"<<count_if(vec.begin(), vec.end(), bind2nd(less<int>(),4))<<endl;
cout<<"min:"<<*min_element(vec.begin(), vec.end())<<endl;
cout<<"min less:"<<*min_element(vec.begin(), vec.end(), less<int>())<<endl;
cout<<"max:"<<*max_element(vec.begin(), vec.end())<<endl;
cout<<"find pos:"<<distance(vec.begin(), find(vec.begin(), vec.end(), 3))+1<<endl;
cout<<"find first >4:"<<*find_if(vec.begin(), vec.end(),
bind2nd(greater<int>(), 5))<<endl;
vector<int>::iterator pos;
pos = search_n(vec.begin(), vec.end(), 2, 4);
if( pos != vec.end() ){
cout<<"search_n 4 4 pos:"<<distance(vec.begin(), pos)+1<<endl;
}
cout<<"search_n >5 pos:"<<distance(vec.begin(),
search_n(vec.begin(), vec.end(), 2, 5, greater<int>()))+1<<endl;;
list<int> lst;
lst.push_back(2);
lst.push_back(3);
lst.push_back(1);
pos = search(vec.begin(), vec.end(), lst.begin(), lst.end());
cout<<"search 2,3 pos:"<<distance(vec.begin(), pos)+1<<endl;
pos = find_end(vec.begin(), vec.end(), lst.begin(), lst.end());
cout<<"find_end 2,3 pos:"<<distance(vec.begin(), pos)+1<<endl;
pos = adjacent_find(vec.begin(), vec.end());
cout<<"adjacent_find pos:"<<distance(vec.begin(), pos)+1<<endl;
pos = adjacent_find(vec.begin(), vec.end(), greater<int>());
cout<<"adjacent_find pos:"<<distance(vec.begin(), pos)+1<<endl;
if( equal(vec.begin()+1, vec.begin()+3, lst.begin())){
cout<<"equal"<<endl;
}else{
cout<<"not equal"<<endl;
}
pair<vector<int>::iterator, list<int>::iterator> values;
values = mismatch( vec.begin(), vec.end(), lst.begin(), less_equal<int>());
if( vec.end() == values.first ){
cout<<"not mismatch"<<endl;
}else{
cout<<"mismatch first:"<<*values.first<<" second:"<<*values.second<<endl;
}
if(lexicographical_compare(vec.begin(), vec.end(), lst.begin(), lst.end())){
cout<<"vec<lst"<<endl;
}else{
cout<<"vec>lst"<<endl;
}
}
int main( int argc, char** argv )
{
vector<int> vec;
for(int i=1; i<=9; ++i)
{
vec.push_back(i);
}
Nonmodifying(vec);
return 0;
}