STL中的排序算法及其他主要包括:
1,排序 2,第n个元素 3,二分检索 4,归并 5,序结构上的集合操作 6,堆操作 7,最大和最小 8,词典比较 9,排列生成器 10,数值算法
1,排序:
sort(),stable_sort(),partial_sort(),partial_sort_copy()
template<class RanIt>
void sort(RanIt first , RanIt last;
template<class RanIt ,class Pred>
void sort(RanIt first, RanIt last, Pred pr);
template<class RanIt>
void stable_sort(RanIt first,RanIt last)
template<class RanIt,class Pred>
void stable_sort(RanIt first,RanIt last,Pred pr);
template<class RanIt>
void partial_sort(RanIt first,RanIt middle,RanIt last);
template<class RanIt,class Pred>
void partial_sort(RanIt first,RanIt middle,RanIt last,Pred pr);
template<class Init,class RanIt>
RanIt partial_sort_copy(Init first1 , Init last1, RanIt first2,RanIt last2);
template<class Init,class RanIt,class Pred>
RanIt partial_sort_copy(Init first1,Init last1,RanIt first1 , RanIt last2,Pred pr);
示例:
1,>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int a[] = {1,8,6,10,4};
vector<int> v(a,a+5);
sort(v.begin(),v.end());
cout << "sort as increment : " << endl;
copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
return 0;
}
2,>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
bool myless(int &m,int &n)
{
return m > n;
}
int main()
{
int a[] = {1,8,6,10,4};
vector<int> v(a,a+5);
sort(v.begin(),v.end(),myless);
cout << "sort as decrement : " << endl;
copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
return 0;
}
3,>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
class Student
{
public:
int NO;
string name;
int grade;
Student(int NO,string name,int grade)
{
this -> NO = NO;
this -> name = name;
this -> grade = grade;
}
bool operator< (const Student &s) const
{
return grade < s.grade;
}
};
ostream & operator << (ostream &os,const Student & s)
{
os << s.NO << "\t" << s.name << "\t" << s.grade;
return os;
}
int main()
{
Student s1(101,"zhangsan",90);
Student s2(102,"lisi",80);
Student s3(103,"wangwu",85);
Student s4(103,"zhaoliu",65);
vector<Student> v;
v.push_back(s1);
v.push_back(s2);
v.push_back(s3);
v.push_back(s4);
sort(v.begin(),v.end());
cout << "sort as increment : " << endl;
cout << "NO\tname\tgrade" << endl;
copy(v.begin(),v.end(),ostream_iterator<Student>(cout,"\n"));
return 0 ;
};
4,>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
int a[] = {10,1,3,9,7,6,2,4,5,8};
vector<int> v(a,a+10);
cout << "Org vector data: ";
copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
partial_sort(v.begin(),v.begin()+4,v.end());
cout << "After partial_sort : ";
copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
return 0;
}
5,>
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
using namespace std;
class Student
{
public:
int NO;
string name;
int grade;
Student() {}
Student(int NO,string name,int grade)
{
this -> NO = NO;
this -> name = name;
this -> grade = grade;
}
bool operator> (const Student &s) const
{
return grade > s.grade;
}
};
int main()
{
vector<Student> v;
Student s1(101,"aaa",90);
Student s2(102,"bbb",80);
Student s3(103,"ccc",60);
Student s4(104,"ddd",96);
Student s5(105,"eee",95);
Student s6(106,"fff",78);
Student s7(107,"ggg",94);
Student s8(108,"hhh",92);
v.push_back(s1);
v.push_back(s2);
v.push_back(s3);
v.push_back(s4);
v.push_back(s5);
v.push_back(s6);
v.push_back(s7);
v.push_back(s8);
vector<Student> vResult(3);
partial_sort_copy(v.begin(),v.end(),vResult.begin(),vResult.end(),greater<Student>());
for(int i = 0 ;i < vResult.size(); i++)
{
Student & s = vResult.at(i);
cout << s.NO << "\t" << s.name << "\t" << s.grade << endl;
}
return 0 ;
};
6,>
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
int a[] = {10,1,3,9,7,6,2,4,5,8};
list<int> l(a,a+10);
// l.sort(l.begin(),l.end()); 错误调用,因为list特殊,重载了sort!
l.sort(greater<int>());
copy(l.begin(),l.end(),ostream_iterator<int>(cout,"\t"));
return 0;
}
2,第n个元素
nth_element()
template<class RanIt>
void nth_element(RanIt first,RanIt nth,RanIt last);
template<class RanIt,class Pred>
void nth_element(RanIt first,RanIt nth,RanIt last,Pred pr);
示例:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
using namespace std;
class Student
{
public:
int NO;
string name;
int grade;
Student(int NO,string name,int grade)
{
this -> NO = NO;
this -> name = name;
this -> grade = grade;
}
bool operator > (const Student & s) const
{
return grade > s.grade;
}
};
int main()
{
vector<Student> v;
Student s1(101,"aaa",90);
Student s2(102,"bbb",80);
Student s3(103,"ccc",60);
Student s4(104,"ddd",96);
Student s5(105,"eee",95);
Student s6(106,"fff",78);
Student s7(107,"ggg",94);
Student s8(108,"hhh",92);
v.push_back(s1);
v.push_back(s2);
v.push_back(s3);
v.push_back(s4);
v.push_back(s5);
v.push_back(s6);
v.push_back(s7);
v.push_back(s8);
nth_element(v.begin(),v.begin()+2,v.end(),greater<Student>());
vector<Student>::iterator it = v.begin()+2;
cout << "the 3th good student info : " << endl;
cout << "ID\tname\tgrade" << endl;
cout << (*it).NO << "\t" << (*it).name << "\t" << (*it).grade << endl;
return 0;
}
3,二分检索
lower_bound() , upper_bound() , equal_range() , binary_search()
template<class FwdIt, class T>
FwdIt lower_bound(FwdIt first,FwdIt last,const T &val);
template<class FwdIt,class T,class Pred>
FwdIt lower_bound(FwdIt first,FwdIt last,const T &val,Pred pr);
template<class FwdIt,class T>
FwdIt upper_bound(FwdIt first, FwdIt last,const T &val);
template<class FwdIt,class T,class Pred>
FwdIt upper_bound(FwdIt first,FwdIt last,const T &val,Pred pr);
template<class FwdIt,class T>
pair<FwdIt,FwdIt> equal_range(FwdIt first,FwdIt last,const T &val);
template<class FwdIt,class T,class Pred>
pair<FwdIt,FwdIt>equal_range(FwdIt fist,FwdIt last,const T &val,Pred pr);
template<class FwdIt,class T>
bool binary_search(FwdIt first ,FwdIt last, const T &val);
template<class FwdIt,class T,class Pred>
bool binary_search(FwdIt first,FwdIt last,const T &val,Pred pr);
示例:
#include <iostream>
#include <algorithm>
#include <list>
using namespace std;
int main()
{
int a[] = {1,2,2,3,3,3,4,4,4,4};
list<int> l1(a,a+10);
cout << "org data a[] = ";
copy(l1.begin(),l1.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
bool bExist = binary_search(l1.begin(),l1.end(),5);
cout << "have element 5 ? " << (bExist==0?"false" : "true") << endl;
list<int>::iterator first2 = lower_bound(l1.begin(),l1.end(),2);
if(first2 != l1.end())
{
cout << "the first element 2 appear : " << distance(l1.begin(),first2) << endl;
}
list<int>::iterator last3 = upper_bound(l1.begin(),l1.end(),3);
if(last3 != l1.end())
{
cout << "the last element 3 appear : " << distance(l1.begin(),--last3) << endl;;
}
pair<list<int>::iterator,list<int>::iterator> p = equal_range(l1.begin(),l1.end(),4);
if(p.first != l1.end())
{
int nSize = distance(p.first,p.second);
cout << "Total have element 4 is : " << nSize <<endl;
}
return 0 ;
}
4,归并
merge() , inplace_merge()
template<class Init1,class Init2,class OutIt>
OutIt merge(Init1 first,Init1 last1,Init2 first2,Init2 last2,OutIt x);
template<class Init1,class Init2,class OutIt,class Pred>
OutIt merge(Init1 first1,Init1 last1,Init2 first2,Init2 last2,OutIt x,Pred pr);
template<class BidIt>
void inplace_merge(BidIt first,BidIt middle,BidIt last)
template<class BidIt,class Pred>
void inplace_merge(BidIt first,BidIt middle,BidIt last, Pred pr);
示例:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[] = {1,3,5,7};
int b[] = {2,4,6,8};
int result[8];
cout << "Org a[] = " << endl;
copy(a,a+4,ostream_iterator<int>(cout,"\t"));
cout << endl;
cout << "Org b[] = " << endl;
copy(b,b+4,ostream_iterator<int>(cout,"\t"));
cout << endl;
merge(a,a+4,b,b+4,result);
cout << "After a[],b[] merge: " << endl;
copy(result,result+8,ostream_iterator<int>(cout,"\t"));
cout << endl << endl;
int c[8] = {1,3,4,8,2,5,6,7};
cout << "Org c[] = " << endl;
copy(c,c+8,ostream_iterator<int>(cout,"\t"));
cout << endl;
inplace_merge(c,c+4,c+8);
cout << "After c[] inplace_merge : " << endl;
copy(c,c+8,ostream_iterator<int>(cout,"\t"));
cout << endl;
return 0;
}
5,有序结构上的集合操作:
includes(), set_union(), set_intersection() , set_difference() , set_symmetric_difference()
template<class Init1,class Init2>
bool includes(Init1 first1,Init1 last1,Init2 first2,Init2 last2);
template<class Init1,class Init2,class Pred>
bool includes(Init1 first1,Init1 last1,Init2 first2,Init2 last2,Pred pr);
template<class Init1,class Init2,class OutIt>
OutIt set_union(Init1 first1,Init1 last1,Init2 first2,Init2 last2,OutIt x);
tempalte<class Init2,class Init2 ,class OutIt,class Pred>
OutIt set_union(Init1 first1,Init1 last1,Init2 first2,Init2 last2,OutIt x,Pred pr);
template<class Init1,class Init2,class OutIt>
OutIt set_intersection(Init1 first1,Init1 last1,Init2 first2,Init2 last2,OutIt x)
template<class Init1,class Init2,class OutIt,class Pred>
OutIt set_intersection(Init1 first,Init1 last1,Init2 first2,Init2 last2,OutIt x,Pred pr);
template<class Init1,class Init2,class OutIt>
OutIt set_difference(Init1 first1,Init1 last1,Init2 first2,Init2 last2,OutIt x);
template<class Init1,class Init2,class OutIt,class Pred>
OutIt set_difference(Init1 first1,Init1 last1,Init2 first2,Init2 last2,OutIt x,Pred pr);
template<class Init1,class Init2,class OutIt>
OutIt set_symmetric_difference(Init1 first1,Init1 last1,Init2 first2,Init2 last2,OutIt x);
template<class Init1,class Init2,class OutIt,class Pred>
OutIt set_symmetric_dirrerence(Init1 first1,Init1 last1,Init2 first1,Init2 last2,OutIt x,Pred pr);
示例:
1,>
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
int a[] = {1,2,3,4,5};
int b[] = {1,2,3,6};
list<int> l1(a,a+5);
list<int> l2(b,b+4);
cout << "org l1 = ";
copy(l1.begin(),l1.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
cout << "org l2 = ";
copy(l2.begin(),l2.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
bool bRet = includes(l1.begin(),l1.end(),l2.begin(),l2.end());
cout << "l1 include l2 ? " << (bRet == 1? "yes" : "no") << endl;
list<int>l3;
set_union(l1.begin(),l1.end(),l2.begin(),l2.end(),back_inserter(l3));
cout << "l1 ²¢ l2 : " << endl;
copy(l3.begin(),l3.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
list<int> l4;
set_intersection(l1.begin(),l1.end(),l2.begin(),l2.end(),back_inserter(l4));
cout << "l1 ½» l2 : " << endl;
copy(l4.begin(),l4.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
list<int> l5;
set_difference(l1.begin(),l1.end(),l2.begin(),l2.end(),back_inserter(l5));
cout << "l1 ²î l2 : " << endl;
copy(l5.begin(),l5.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
list<int> l6;
set_symmetric_difference(l1.begin(),l1.end(),l2.begin(),l2.end(),back_inserter(l6));
cout << "l1 ¶Ô³Æ²î l2 : " << endl;
copy(l6.begin(),l6.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
list<int> l7;
merge(l1.begin(),l1.end(),l2.begin(),l2.end(),back_inserter(l7));
cout << "l1 merge l2 : " << endl;
copy(l7.begin(),l7.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
return 0;
}
2,>
#include <iostream>
#include <algorithm>
using namespace std;
struct Student
{
int NO;
char name[20];
bool operator < (Student &s)
{
return NO < s.NO;
}
};
int main()
{
bool bRet = true;
Student s1[] = {{1001,"zhang"},{1003,"li"},{1004,"wang"},{1002,"zhao"}};
Student s2[] = {{1001,"zhang"},{1004,"wang"},{1002,"zhao"}};
sort(s1,s1+4);
sort(s2,s2+3);
bRet = includes(s1,s1+4,s2,s2+3);
cout << (bRet == true ? "s1 include s2" : "s1 not includes s2") << endl;
return 0;
}
3,>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
struct Student
{
int NO;
string name;
int chinese;
int math;
public:
bool operator< (const Student &s)
{
if(NO == s.NO)
{
math = s.math;
}
return NO < s.NO;
}
};
int main()
{
vector<Student> v1;
vector<Student> v2;
ifstream in1("1.txt");
ifstream in2("2.txt");
while(!in1.eof())
{
Student s;
in1 >> s.NO >> s.name >> s.chinese;
v1.push_back(s);
}
while(!in1.eof())
{
Student s;
in2 >> s.NO >> s.name >> s.math;
v2.push_back(s);
}
in1.close();
in2.close();
set_union(v1.begin(),v1.end(),v2.begin(),v2.end(),v1.begin());
for(int i = 0 ; i < v1.size();i ++)
{
Student &s = v1.at(i);
cout << s.NO << "\t" << s.name << "\t" << s.chinese << s.math << endl;
}
return 0;
}
6,堆操作
make_heap() , pop_heap() , push_heap() , sort_heap()
template<class RanIt>
void make_heap(RanIt first,RanIt last);
template<class RanIt ,class Pred>
void make_heap(RanIt first,RanIt last,Pred pr);
template<class RanIt>
void pop_heap(RanIt first,RanIt last);
template<class RanIt>
void pop_heap(RanIt first,RanIt last,Pred pr);
template<class RanIt>
void push_heap(RanIt first,RanIt last)
template<class RanIt,clas Pred>
void push_heap(RanIt first,RanIt last,Pred pr)
template<class RanIt>
void sort_heap(RanIt first,RanIt last);
template<class RanIt,class Pred>
void sort_heap(RanIt first,RanIt last,Pred pr);
示例程序:
1,> 建最大堆,建最小堆,求堆中最大值和次大值
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
int main()
{
int a[] = {1,4,2,10,6,5,9,7,8,3};
cout << "org a[] = ";
copy(a,a+10,ostream_iterator<int>(cout,"\t"));
cout << endl;
vector<int> v1(a,a+10);
make_heap(v1.begin(),v1.end(),greater<int>());
cout << "Min heap:" << endl;
copy(v1.begin(),v1.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
vector<int> v2(a,a+10);
make_heap(v2.begin(),v2.end(),less<int>());
cout << "Max heap:" << endl;
copy(v2.begin(),v2.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
pop_heap(v2.begin(),v2.end());
cout << "Max value in heap : ";
cout << *(v2.end()-1) << endl;
pop_heap(v2.begin(),v2.end()-1);
cout << "The second Max value in heap :";
cout << *(v2.end()-2) << endl;
return 0 ;
}
2,> 封装堆
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
template<class T,class Pred = less<T> >
class MyHeap
{
Pred pr;
vector<T> v;
int ValidSize;
public:
MyHeap(T t[],int nSize):v(t,t+nSize)
{
ValidSize = nSize;
make_heap(v.begin(),v.begin()+ValidSize,pr);
}
void Insert(const T &t)
{
v.push_back(t);
ValidSize ++;
push_heap(v.begin(),v.begin()+ValidSize,pr);
}
bool Remove(T &result)
{
if(ValidSize == 0)
return false;
pop_heap(v.begin(),v.begin()+ValidSize,pr);
result = *(v.begin()+ValidSize-1);
ValidSize --;
return true;
}
void Sort()
{
if(ValidSize > 0)
sort_heap(v.begin(),v.end()+Validsize);
}
bool IsEmpty()
{
return ValidSize == 0;
}
void Display()
{
copy(v.begin(),v.begin()+ValidSize,ostream_iterator<T>(cout,"\t"));
}
};
int main()
{
int a[] = {1,4,2,10,6};
MyHeap<int,greater<int> >m(a,5);
cout << "org heap:" << endl;
m.Display();
cout << endl;
m.Insert(100);
cout << "After insert heap : " << endl;
m.Display();
cout << endl;
int result;
m.Remove(result);
cout << "Min value in heap : " << result << endl;
m.Remove(result);
cout << "The second Min value in heap : " << result << endl;
return 0 ;
}
7,最大和最小:
min() , max() , min_element() , max_element()
template<class T>
const T & min(const T &x ,const T &y);
template<class T,class Pred>
const T & min(const T &x,const T &y,Pred pr);
template<class T>
const T & max(const T &x,const T &y);
template<class T,Pred pr>
const T & max(const T &x,const T &y,Pred pr);
template<class FwdIt>
FwdIt min_element(FwdIt first,FwdIt last);
template<class FwdIt,class Pred>
FwdIt min_element(FwdIt first, FwdIt last,Pred pr);
template<class FwdIt>
FwdIt max_element(FwdIt first,FwdIt last);
template<class FwdIt,class Pred>
FwdIt max_element(FwdIt first ,FwdIt last, Pred pr);
示例:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a = _MIN(10,20);
int b = _MAX(10,20);
cout << "min(10,20) : " << a << endl;
cout << "max(10,20) : " << b << endl;
int c[] = {1,10,5,7,9};
cout << "org c[] = ";
copy(c,c+5,ostream_iterator<int>(cout,"\t"));
cout << endl;
int * it_min = min_element(c,c+5);
int * it_max = max_element(c,c+5);
cout << "the min value in c[]: " << *it_min << endl;
cout << "the max value in c[]: " << *it_max << endl;
return 0;
}
8,词典比较
lexicographical_compare()
template<class InIt1,class InIt2>
bool lexicographical_compare(InIt1 first1,InIt1 last1,InIt2 first2, InIt2 last2);
template<class Init1,class Init2,class Pred>
bool lexicographical_compare(Init1 first1,Init1 last1,Init2 first2,Init2 last2 , Pred pr);
示例:
1,>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[] = {1,2,3};
int b[] = {1,2,2};
bool bRet = lexicographical_compare(a,a+3,b,b+3);
cout << (bRet == 1 ? "a[] < b[]" : "a[] > b[]") << endl;
return 0;
}
2,>
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool mycompare(vector<int> &v1,vector<int> &v2)
{
return lexicographical_compare(v1.begin(),v1.end(),v2.begin(),v2.end());
}
int main()
{
int a[] = {1,2,3};
int b[] = {1,2,2};
int c[] = {1,2,3,1};
vector<int> v[3] = {vector<int>(a,a+3),vector<int>(b,b+3),vector<int>(c,c+4)};
cout << "org 3 vector : " << endl;
for(int i = 0 ;i < 3 ;i ++)
{
copy(v[i].begin(),v[i].end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
}
sort(v,v+3,mycompare);
cout << "After sort(as lexicographical_compare) : " << endl;
for(i = 0 ; i < 3 ;i ++)
{
copy(v[i].begin(),v[i].end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
}
return 0;
}
9,排列生成器:
next_permutation() , prev_permutation()
template<class BidIt>
bool next_permutation(BidIt first,BidIt last);
template<class BidIt,class Pred>
bool next_permutation(BidIt first,BidIt last,Pred pr);
template<class BidIt>
bool prev_permutation(BidIt first,BidIt last);
tempalte<class BidIt,class Pred>
bool prev_permutation(BidIt first,BidIt last,Pred pr);
示例:
1,>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int A[] = {2,3,4,5,6,1};
const int N = sizeof(A) / sizeof(int);
cout << "Initialize: " ;
copy(A,A+N,ostream_iterator<int>(cout," "));
cout << endl;
prev_permutation(A,A+N);
cout << "After prev_permutation : ";
copy(A,A+N,ostream_iterator<int>(cout," "));
cout << endl;
next_permutation(A,A+N);
cout << "After next_permutation : ";
copy(A,A+N,ostream_iterator<int>(cout," "));
cout << endl;
return 0;
}
2,> 全排列:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[]={0,0,1,1,3,2};
sort(a,a+6);
if(a[0] == 0)
{
int *it = upper_bound(a,a+6,0);
swap(a[0],*it);
}
do
{
copy(a,a+6,ostream_iterator<int>(cout,"\t"));
cout << endl;
}while(next_permutation(a,a+6));
return 0;
}
2,>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int a[]={0,0,1,1,3,2};
sort(a,a+6);
if(a[0] == 0)
{
int *it = upper_bound(a,a+6,0);
swap(a[0],*it);
}
do
{
copy(a,a+6,ostream_iterator<int>(cout,"\t"));
cout << endl;
}while(next_permutation(a,a+6));
return 0;
}
10,数值算法
accumulate() , inner_product() partial_sum() , adjacent_difference()
template<class Init,class T>
T accumulate(Init first,Init last,T init);
template<class Init,class T,class Pred>
T accumulate(Init first,Init last,T init,Pred pr);
template<class Init1,class Init2,class T>
T inner_product(Init1 first1,Init1 last1,Init2 first2,T val);
template<class Init1,class Init2,class T,class Pred1,class Pred2>
T inner_product(Init1 first1,Init1 last1,Init2 first2 ,T val,Pred1 pr1,Pred2 pr2);
template<class Init,class OutIt>
OutIt partial_sum(Init first,Init last,OutIt result);
template<class Init,class OutIt,class Pred>
OutIt partial_sum(Init first,Init last,OutIt result,Pred pr);
template<class Init,class OutIt>
OutIt adjacent_difference(Init first,Init last,OutIt result);
template<class Init,class OutIt,class Pred>
OutIt adjacent_difference(Init first,Init last,OutIt result);
示例:
1,>
#include <iostream>
#include <numeric>
#include <functional>
#include <vector>
#include <iterator>
using namespace std;
int main()
{
vector<int> v(10);
for(int i = 0 ;i < 10 ; i ++)
{
v[i] = i +1 ;
}
vector<int> mid(10);
partial_sum(v.begin(),v.end(),mid.begin(),multiplies<int>());
int sum = accumulate(mid.begin(),mid.end(),0);
cout << "1+(1 X 2) + (1 X 2 X 3) + ... the first ten sum: " << sum << endl;
return 0;
}
2,>
#include <iostream>
#include <numeric>
#include <vector>
using namespace std;
int main()
{
vector<float> v(10);
for(int i = 1;i <= 10 ;i++)
{
v.push_back((float)i/(i+1));
cout << (float)i/(i+1) << "\t";
}
float sum = inner_product(v.begin(),v.end(),v.begin(),0.0f);
cout << endl << sum << endl;
return 0;
}
11,自定义STL风格:
1>函数参数一般是迭代指针,不要以数组形式传递
2>要从迭代指针参数获得迭代指针类型,这一点很重要
示例:
如果自定义泛型算法中要使用临时变量,一定要使用_Val_type函数,一定要用转换函数
template<class Ty> inline
Ty * _Val_type(const _Ty *)
_Ty * 一般来说表示一个迭代器指针下面例子中 _Val_type(first) 就会得到 _Ty * 等于int *,则Ty等于int
#include <algorithm>
#include <iostream>
#include <vector>
#include <list>
using namespace std;
template<class Init1,class Init2>
void my_swap(Init1 first,Init1 last,Init2 first2)
{
_my_swap(first,last,first2,_Val_type(first));
}
template<class init1,class init2,class T> inline
void _my_swap(init1 first,init1 last,init2 first2, T *)
{
int n = 0 ;
for(; first != last;first ++,first2 ++,n ++)
{
// if(n % 2 == 0)
{
T tmp = *first;
* first = * first2;
*first2 = tmp;
}
}
}
int main()
{
int a[] = {1,3,5,7,9};
int b[] = {2,4,6,8,10};
vector<int> v(a,a+5);
list<int> l(b,b+5);
cout << "org v = ";
copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
cout << "org l = ";
copy(l.begin(),l.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
my_swap(v.begin(),v.end(),l.begin());
cout << "After swap v =";
copy(v.begin(),v.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
cout << "After swap l =";
copy(l.begin(),l.end(),ostream_iterator<int>(cout,"\t"));
cout << endl;
return 0;
}