#include <iostream>
#include <string>
#include <vector>
#include <iterator>
#include <algorithm>
#include <list>
#include <set>
#include <cstdlib>
#include <functional>
#include <numeric>
using namespace std;
template<typename T>
void printVector(vector<T> v)
{
vector<T>::iterator it;
for(it=v.begin();it!=v.end();++it) //使用迭代器输出
cout<<*it<<" ";
cout<<endl;
}
//排序与查找
int main()
{
int A[]={1,2,3};
const int N=sizeof(A)/sizeof(int);
cout<<"The sum is: "<<accumulate(A,A+N,1)<<endl;
cout<<"The sum is: "<<accumulate(A,A+N,1,multiplies<int>())<<endl;
///sort()排序字符串
vector<string> fruits;
fruits.push_back("apple");
fruits.push_back("banana");
fruits.push_back("pear");
fruits.push_back("graphe");
fruits.push_back("cherry");
fruits.push_back("orange");
fruits.push_back("apple");
sort(fruits.begin(),fruits.end(),greater<string>());
copy(fruits.begin(),fruits.end(),ostream_iterator<string>(cout," "));
cout<<endl;
stable_sort(fruits.begin(),fruits.end(),greater<string>());
copy(fruits.begin(),fruits.end(),ostream_iterator<string>(cout," "));
cout<<endl;
//binary_search() 二分查找//
int B[]={1,2,3,3,3,5,8};
const int M=sizeof(B)/sizeof(int);
for(int i=1;i<=10;i++)
{
cout<<"Searching for "<<i<<": "
<<(binary_search(B,B+M,i)? "present" : "not present")
<<endl;
}
lower_bound(),upper_bound()
int point[10] = {1,3,7,7,9};
int tmp = upper_bound(point, point + 5, 7) - point;//按从小到大,7最多能插入数组point的哪个位置
printf("%d/n",tmp);
tmp = lower_bound(point, point + 5, 7) - point;按从小到大,7最少能插入数组point的哪个位置
printf("%d/n",tmp);
merge()/
int A1[]={1,3,5,7};
int A2[]={3,4,6,8};
const int N1=sizeof(A1)/sizeof(int);
const int N2=sizeof(A2)/sizeof(int);
merge(A1,A1+N1,A2,A2+N2,ostream_iterator<int>(cout," "));
cout<<endl;
copy(A1,A1+N1,ostream_iterator<int>(cout," "));
cout<<endl;
inplace_merge()合并一个中前后两段分别有序的序列,即[first,middle)与[middle,last)分别有序,合并这两个子序列,并把结果保存在[first,last)中。
int A3[]={1,1,2,3,1,2,5,7,9};
inplace_merge(A3,A3+4,A3+9);
copy(A3,A3+9,ostream_iterator<int>(cout," "));
cout<<endl;
/set_union()///
int B1[]={1,3,5,7,9,11};
int B2[]={1,1,2,3,5,8,13};
char B3[]={'a','b','B','B','f','H'};
int B4[]={'A','B','b','C','D','F','F','h','h'};
const int L1=sizeof(B1)/sizeof(int);
const int L2=sizeof(B2)/sizeof(int);
const int L3=sizeof(B3)/sizeof(int);
const int L4=sizeof(B4)/sizeof(int);
set_union(B1,B1+L1,B2,B2+L2,ostream_iterator<int>(cout," "));
cout<<endl;
set_union(B3,B3+L3,B4,B4+L4,ostream_iterator<char>(cout," "));
cout<<endl;
return 0;
}