STL算法

来源:互联网 发布:gd单片机官网 编辑:程序博客网 时间:2024/05/18 00:09

STL包含三部分:

一、容器:

1、顺序容器:vector   deque   list

2、关联容器:set  multiset   map   multimap

3、适配器:stack   queue   qriority_queue

二、迭代器:

1、输入迭代器

2、输出迭代器

3、向前迭代器

4、双向迭代器

5、随机迭代器

三、算法,共有80多种,主要分四类:

1、非修改性算法例如访问

2、修改性算法例如插入删除反转

3、堆算法主要是堆创建删除排序

4、数值算法

前三种包含在头文件<algorithm>,后一种包含在<numeric>头文件里。

下面介绍几种常见的算法:

1、copy

template<initerator in, outiterator out>

out copy(in beg,in end,out p)

将源容器(包括数组下同)从迭代器(包括指针下同)从beg-end-1部分复制到目标容器(包括输出流)p开始的位置,注意目标容器大小必须大于等于要复制的元素数目:

#include<numeric>#include<algorithm>using namespace std;int main(){int a[]={1,2,3,4,5,6};vector<int> vv(5);list<int>li(5);copy(a+2,a+4,vv.begin());copy(a,a+5,li.begin());cout<<"the inf of vv:"<<endl;for(int i=0;i<vv.size();i++)cout<<vv[i]<<" ";list<int>::iterator p;cout<<"\n the inf of li"<<endl;for(p=li.begin();p!=li.end();p++)cout<<*p<<" ";vv.insert(vv.begin(),747);cout<<"\n after insert vv"<<endl;for(int i=0;i<vv.size();i++)cout<<vv[i]<<" ";copy(vv.begin(),vv.begin()+4,li.begin());cout<<"\n after copy li:"<<endl;ostream_iterator<int> out(cout," ");copy(li.begin(),li.end(),out);//for(p=li.begin();p!=li.end();p++)//cout<<*p<<" ";system("pause");return 0;}
2、fill

template<typename in,typename T>

void fill(in beg,in end,const T&x)----将指定的x填入容器beg-end-1


void fill_n(in beg,size n,const T&x)----将指定的x填入容器beg-beg+n-1

int main(){int a[]={1,2,3,4,5,6};list<int>li(a,a+6);ostream_iterator<int> out(cout," ");cout<<"the inf of li"<<endl;copy(li.begin(),li.end(),out);cout<<"\n the inf of arr:"<<endl;copy(a,a+6,out);fill(a+2,a+4,88);cout<<"\n after fill the arr:"<<endl;copy(a,a+6,out);fill_n(li.begin(),2,99);cout<<"\n after fill the li:"<<endl;copy(li.begin(),li.end(),out);system("pause");return 0;}

3、generate

用某个函数返回值填入容器

template<typename in,typename fun>

void generate(in beg,in end,fun f)

void generate_n(in beg,size n,fun f)

int sum(){static int a=20;return a++;}int main(){int a[]={1,2,3,4,5,6};list<int>li(6);ostream_iterator<int> out(cout," ");copy(a,a+6,li.begin());cout<<"the inf of arr:"<<endl;copy(a,a+6,out);cout<<"\n the inf of li:"<<endl;copy(li.begin(),li.end(),out);generate(a+2,a+4,sum);cout<<"\n the inf of arr:"<<endl;copy(a,a+6,out);generate_n(li.begin(),2,sum);cout<<"\n the inf of li:"<<endl;copy(li.begin(),li.end(),out);system("pause");return 0;}
4、remove

template<typename in,typename T>

in remove(in beg,in end,T const&x)-----删除元素序列中与x一样的元素

template<typename in,typename boolfun>

in remove_if(in beg,in end,boolfun funname)-----删除序列中所有使得函数funname为真的元素

template<typename in,typename out,typename T>

out remove_copy(in beg,int end,out p,const T&x)----忽略与x一样的元素将其他元素复制到p开始的容器

template<typename in,typename out,typename boolfun>

out remove_copy_if(in beg,in end,out p,boolfun funname)----忽略使得函数为真的元素,将其他元素复制

bool great(int x){return x>3;}int main(){int a[]={1,7,3,4,3,6,1,2};list<int> li(a,a+8);ostream_iterator<int> out(cout," ");cout<<"the inf arr:"<<endl;copy(a,a+8,out);cout<<"\n the inf li:"<<endl;copy(li.begin(),li.end(),out);remove(a,a+8,3);cout<<"\n after remove the inf arr:"<<endl;copy(a,a+8,out);remove_if(li.begin(),li.end(),great);cout<<"\n after remove_if the inf li:"<<endl;copy(li.begin(),li.end(),out);int b[8]={9};cout<<"\n the inf  b:"<<endl;copy(b,b+8,out);remove_copy(a,a+8,b,3);cout<<"\n after remove_copy the inf  b:"<<endl;copy(b,b+8,out);remove_copy_if(a,a+8,b,great);cout<<"\n after remove_copy_if the inf  b:"<<endl;copy(b,b+8,out);system("pause");return 0;}

5、replace

template<typename in,typename T>

void replace(in beg,in end, constT &old,const T&new)-----将容器beg-end-1之间的与old相等的替换成new

template<typename in,typename boolfun,typename T>

void replace_if(in beg,in end,boolfun funname,const T&new)-----将容器beg-end-1之间的满足函数为真的替换成new

template<typename in,typename out,typename T>

out replace_copy(in beg,in end,out p,const T&old,const T&new)------将容器beg-end-1之间的与old相等的先替换成new,再把这一段复制到p开始的容器

template<typename in,typename out,typename boolfun,typename T>

out replace_copy_if(in beg,in end,out p,boolfun funname,const T&new)----将容器beg-end-1之间的满足函数为真的先替换成new,再把这一段复制到p开始的容器

bool great(int x){return x>3;}int main(){int a[]={1,7,3,4,3,6,1,2};list<int> li(8);list<int>li1(a,a+8);copy(a,a+8,li.begin());ostream_iterator<int> out(cout," ");cout<<"the inf arr:"<<endl;copy(a,a+8,out);cout<<"\n the inf li:"<<endl;copy(li.begin(),li.end(),out);replace(a,a+8,3,747);replace_if(li.begin(),li.end(),great,747);cout<<"\n after replace arr:"<<endl;copy(a,a+8,out);cout<<"\n after replace_if li:"<<endl;copy(li.begin(),li.end(),out);int b[8]={9};int a1[]={1,7,3,4,3,6,1,2};list<int> li2(a1,a1+8);replace_copy(a1+2,a1+5,b,3,88);replace_copy_if(li1.begin(),li1.end(),li2.begin(),great,88);cout<<"\n after place_copy b:"<<endl;copy(b,b+8,out);cout<<"\n after replace_copy_if li2:"<<endl;copy(li2.begin(),li2.end(),out);system("pause");return 0;}
6、find

template<typename in,typename T>

in find(in beg,in end,const T&x)-----搜索容器beg-end-1之间等于x的位置

template<typename in,typename boolfun>

in find_if(in beg,in end,boolfun funnname)-----搜索容器beg-end-1之间满足函数为真的位置

两个函数都返回指向第一个合适的元素的位置

template<typename in,typename out>

in find_end(in beg,in end,out beg,out end)-----搜索in中beg-end-1之间与out里beg-end-1匹配的子序列

template<typename in,typename out,typename boolfun>

in find_end(in beg,in end,out in,out end,boolfun name)-----搜索in中beg-end-1之间与out里beg-end-1满足函数的子序列
两个函数均返回最后一个匹配位置

find_first_of搜索子序列中某个元素在第二个序列第一次出现的位置:

template<typename in,typename out>

in find_first_of(in beg,in end,out beg,out,end)

template<typename in,typename out,typename boolfun>

in find_first_of(in beg,in end,out beg,out end,boolfun name)

bool greatthan(int x,int y){return x>y;}int main(){int a[]={1,7,3,4,3,6,1,2};vector<int>vv(a,a+8);ostream_iterator<int>out(cout," ");cout<<"the inf arr:"<<endl;copy(a,a+6,out);cout<<"\n the inf vv:"<<endl;copy(vv.begin(),vv.end(),out);int k;cout<<"enter thr key"<<endl;cin>>k;int *p=find(a,a+8,k);if(p!=(a+8))cout<<"\n the posi:"<<p-a;else cout<<"\n no value in arr"<<endl;vector<int>::iterator p1=find_if(vv.begin(),vv.end(),great);if(p1!=vv.end())cout<<"\n the posi:"<<p1-vv.begin();else cout<<"no value in vv"<<endl;int a1[]={1,7,3,4,3,6,1,2};vector<int>vv1(a,a+8);int a2[]={3,6,1};int *pp=find_end(a1,a1+8,a2,a2+3);p1=find_end(vv1.begin(),vv1.end(),a2,a2+3);if(pp!=a1+8)cout<<"\n the pos:"<<pp-a1;else cout<<"\n value no";if(p1!=vv1.end())cout<<"\n pos:"<<p1-vv1.begin();else cout<<"no value"<<endl;int a3[]={1,7,3,4,3,6,1,2};int b[]={9,96,21,3,2,3,1};vector<int>vv2(a,a+8);pp=find_first_of(a3,a3+8,b+2,b+4);p1=find_first_of(vv2.begin(),vv2.end(),b+2,b+4,greatthan);if(pp!=a3+8)cout<<"\n pos:"<<pp-a3;else cout<<"value no"<<endl;if(p1!=vv2.end())cout<<"\n pos:"<<p1-vv2.begin();else cout<<"no value"<<endl;system("pause");return 0;}

7、search

函数search与find_end类似,都是搜索一个子序列,后者搜索最后匹配的子序列,而search搜索第一个匹配的位置:

template<typename in,typename out>

in search(in beg,in end,out beg, out end)----元素匹配的标准是相等性检查

template<typename in,typename out,typename boolfun>

in search(in beg,in end,out beg,out end,boolfun name )----元素匹配的标准是函数为真

函数search_n是搜索序列一个值连续出现的情况:

template<typename in,typename size,typename T>

in search_n(in beg,in end,size count,const T&x)

template<typename in,typename size,typename boolfun>

in search_n(in beg,in end,size count,boolfun name)

int main(){int a[]={1,7,3,4,3,3,1,2};int b[]={9,96,4,3,2,3,1};vector<int>vv(a,a+8);ostream_iterator<int>out(cout," ");cout<<"the inf a:"<<endl;copy(a,a+8,out);cout<<"\n the inf vv:"<<endl;copy(vv.begin(),vv.end(),out);int*p=search(a,a+8,b+2,b+4);if(p!=a+8)cout<<"\n thr po:"<<p-a<<endl;else cout<<"\n no value"<<endl;vector<int>::iterator pp=search_n(vv.begin(),vv.end(),2,3);if(pp!=vv.end())cout<<"\n po:"<<pp-vv.begin()<<endl;else cout<<"\n no value"<<endl;system("pause");return 0;}
8、sort&&binary_search

函数sort需要使用随机访问迭代器,可以用来排序数组,vector,deque:

template<typename randin>

void sort(randin beg,randin end)----默认升序排序

template<typename randin,typename rala>

void sort(randin beg,randin end,rala op)----按照指定关系排序

函数binary_search需要在一个已经排序了序列搜索一个值,故先需要sort排序再使用

template<typename in,typename T>

bool binary_search(in beg,in end,const T&x)-----按照默认的升序关系并进行搜索

template<typename in,typename T,typename strictweakodering>

bool binary_search(in beg,in end,const T&x,strictweakordering op)

int main(){int a[]={1,7,3,4,3,3,1,2};vector<int>vv(a,a+8);ostream_iterator<int>out(cout," ");cout<<"the inf a:"<<endl;copy(a,a+8,out);cout<<"\n the inf vv:"<<endl;copy(vv.begin(),vv.end(),out);sort(a,a+8);sort(vv.begin(),vv.end(),greater<int>());cout<<"\n after sort by less a:"<<endl;copy(a,a+8,out);cout<<"\n after sort by greater vv:"<<endl;copy(vv.begin(),vv.end(),out);bool f=binary_search(a,a+8,4);if(f)cout<<"\n the 4 existing in a"<<endl;else cout<<"\n no in a"<<endl;f=binary_search(vv.begin(),vv.end(),4,greater<int>());if(f)cout<<"\n the 4 existing in vv"<<endl;else cout<<"\n no in vv"<<endl;system("pause");//binary_search必须已经排序了return 0;}

9、adjacent_find&&merge

函数adjacent_find搜索序列中满足条件的(要么是连续相等要么是连续使得函数为真)的相邻元素:

template<typename in>

in adjacent_find(in beg,in end)

template<typename in,typename boolfun>

in adjacent_find(in beg,in end,boolfun name)

返回的是指向第一个满足条件的子序列的首元素的迭代器

函数merge将两个有序序列合并为新序列,故先需要sort:

template<typename in1.typename in2,typename out>

out merge(in beg1,in1 end,in2 beg,in2 end,out p)

template<typename in1,typename in2,typename out,typename rela>

out merge(in1 beg,in1 end,in2 beg,in2 end,out p,rela op)

函数inplace_merge将序列的两部分合并,还是在已有序列:

template<typename in>

in inplace_merge(in beg,in mid,in end)

template<typename in,typename rela>

in inplace_merge(in beg,in mid,in end,rela op)

将两个有序子序列beg-mid-1和mid-end-1合并,并保存到原序列,原址合并

int main(){int a[]={1,7,3,4,3,3,1,2};vector<int>li(8);copy(a,a+8,li.begin());ostream_iterator<int>out(cout," ");cout<<"the arr:"<<endl;copy(a,a+8,out);cout<<"the li:"<<endl;copy(li.begin(),li.end(),out);vector<int>::iterator  p=adjacent_find(li.begin(),li.end());if(p!=li.end())cout<<"\n the first adjacent:"<<(p-li.begin())<<endl;else cout<<"\n no adjacent!"<<endl;sort(a,a+3);sort(a+3,a+8);cout<<"\n after sort the first arr:"<<endl;copy(a,a+3,out);cout<<"\n the second arr:"<<endl;copy(a+3,a+8,out);merge(a,a+3,a+3,a+8,li.begin());cout<<"\n after merge the li:"<<endl;copy(li.begin(),li.end(),out);inplace_merge(a,a+3,a+8);cout<<"\n after inplace_merge:"<<endl;copy(a,a+8,out);system("pause");return 0;}
10、reverse

函数reverse将序列元素反转:

template<typename in>

void reverse(in beg,in end)
函数reverse_copy将序列元素逆序后复制到另一个序列,原序列不变:

template<typename in,typename out>

out reverse_copy(in beg,in end,out p)

int main(){int a[]={1,7,3,4,3,3,1,2};list<int>li(8);ostream_iterator<int>out(cout," ");cout<<"a:"<<endl;copy(a,a+8,out);reverse(a,a+8);cout<<"\n after reverse a:"<<endl;copy(a,a+8,out);reverse_copy(a,a+8,li.begin());cout<<"\n after reverse_copy a:"<<endl;copy(a,a+8,out);cout<<"\n after reverse_copy li:"<<endl;copy(li.begin(),li.end(),out);system("pause");return 0;}
11、rotate

函数rotate将序列元素旋转:

template<typename in>

void rotate(in beg,in new,in end)

将序列beg-end-1之间序列元素以new处开始旋转

函数rotate_copy作旋转后复制到新容器,原序列不变:

template<typename in,typename out>

out rotate_copy(in beg,in new,in end,out p)

int main(){int a[]={1,2,3,4,5,6,7,8};list<int> li(8);ostream_iterator<int>out(cout," ");cout<<"the inf a:"<<endl;copy(a,a+8,out);rotate(a,a+3,a+8);//以a+3为首cout<<"\n after rotate a:"<<endl;copy(a,a+8,out);rotate_copy(a,a+1,a+8,li.begin());//以a+1为首cout<<"\n after rotate_copy a:"<<endl;copy(a,a+8,out);cout<<"\n after rotate_copy li:"<<endl;copy(li.begin(),li.end(),out);system("pause");return 0;}
12、swap

函数swap专门交换元素:

template<typename T>

void swap(T &x,T&y)

函数iter_swap专门交换迭代器或者指针间接交换元素:

template<typename in1,typename in2>

void iter_swap(in1 p1,in2 p2)

函数swap_ranges专门交换序列

template<typename in1,in 2>

in1 swap_ranges(in1 beg,in1 end,in2 beg)

从in2 beg开始同序列beg-end-1交换

int main(){int a[]={1,2,3,4,5,6,7,8};ostream_iterator<int>out(cout," ");cout<<"the inf a:"<<endl;copy(a,a+8,out);swap(a[0],a[1]);cout<<"\a after swap ele a:"<<endl;copy(a,a+8,out);iter_swap(a+2,a+3);cout<<"\n after swap itr a:"<<endl;copy(a,a+8,out);swap_ranges(a+1,a+4,a+4);cout<<"\n after swap range a:"<<endl;copy(a,a+8,out);system("pause");return 0;}
13、count

函数cout统计给定值在序列中出现次数:

template<typename in,typename T>

int count(in beg,in end,const T&x)

函数count_if统计满足函数的在序列出现次数:

template<typename in.typename boolfun>

int count_if(in beg,in end,boolfun name)

int main(){int a[]={1,2,3,4,5,6,7,8};ostream_iterator<int>out(cout," ");cout<<"the inf a:"<<endl;copy(a,a+8,out);cout<<"\n the num of 6:"<<count(a,a+8,6)<<endl;cout<<"\n the num >3:"<<count_if(a,a+8,great)<<endl;system("pause");return 0;}
14、max&&min

函数max_element,函数min_element求序列中元素最值:

template<typename in>

in max_element(in beg,in end)

template<typename in>

in min_element(in beg,in end)

int main(){int a[]={1,2,3,4,5,6,7,8};ostream_iterator<int>out(cout," ");cout<<"the inf a:"<<endl;copy(a,a+8,out);cout<<"\n the max of a:"<<*max_element(a,a+8)<<endl;cout<<"\n the min of a:"<<*min_element(a,a+8)<<endl;system("pause");return 0;}
15、random_shuffle

函数random_shuffle对原序列随机重排序:

temlate<typename in>

void random_shuffle(in beg,in end)

int main(){int a[]={1,2,3,4,5,6,7,8};ostream_iterator<int>out(cout," ");cout<<"the old:"<<endl;copy(a,a+8,out);random_shuffle(a,a+8);cout<<"\n the new:"<<endl;copy(a,a+8,out);random_shuffle(a,a+8);cout<<"\n the new:"<<endl;copy(a,a+8,out);system("pause");return 0;}
16、for_each&&transform

函数for_each主要使用一个函数对序列每一个元素进行操作:

template<typename in,typename fun>

void for_each(in beg,in end,fun name)

函数transform将一个函数使用于序列每一个元素,将处理结果放在另一个容器:
template<typename in,typename out,typename fun>

out transform(in beg,in end,out p,fun name)

void dis(int&x){cout<<"value:"<<x<<" ";}int sum(int&x){return x*x;}int main(){int a[]={1,2,3,4,5,6,7,8};cout<<"the a:"<<endl;for_each(a,a+8,dis);vector<int>vv(8);transform(a,a+8,vv.begin(),sum);for_each(vv.begin(),vv.end(),dis);system("pause");return 0;}
17、集合函数

函数includes检测第一个序列是否包含第二个序列:

template<typename in1,typename in2>

bool includes(in1 beg,in1 end,in2 beg,in2 end)

函数set_union求两个序列的并集:

template<typename in1,typename in2,typename out>

out set_union(in1 beg,in1 end,in2 beg,in2 end,out p)

函数set_difference求两个序列差集:

template<typename in1,typename in2,typename out>

out set_difference(in1 beg,in1 end,in2 beg,in2 end,out p)

函数set_intersection求两个序列交集:

template<typename in1,typename in2,out p>

out set_intersection(in1 beg,in1 end,in2 beg,in2 end,out p)

函数set_symmetric_differencr求两个序列某一个,但不同时出现在另一个的集合:

template<typename in1,typename in2,out p>

p set_symmetric_difference(in1 beg,in1 end,in2 beg,in2 end,out p)

int main(){int a[]={1,2,3,4,5,6,7,8};int b[]={1,3,6,9,12};ostream_iterator<int>out(cout," ");cout<<"the a:"<<endl;copy(a,a+8,out);cout<<"\n the b:"<<endl;copy(b,b+5,out);bool f=includes(a,a+8,b,b+3);if(f)cout<<"a includes b"<<endl;else cout<<"not include"<<endl;vector<int>vv(15);vector<int>::iterator p=set_union(a,a+8,b,b+5,vv.begin());cout<<"\n after union:"<<endl;copy(vv.begin(),p,out);p=set_difference(a,a+8,b,b+5,vv.begin());cout<<"\n after difference:"<<endl;copy(vv.begin(),p,out);p=set_intersection(a,a+8,b,b+5,vv.begin());cout<<"\n after intersection:"<<endl;copy(vv.begin(),p,out);p=set_symmetric_difference(a,a+8,b,b+5,vv.begin());cout<<"\n after sysmmetric_difference:"<<endl;copy(vv.begin(),p,out);system("pause");return 0;}
18、数值计算

函数accumulate

template<typename in,typename T>

T accumulate<in beg,in end,T x>

返回所有元素和x的和

template<typename in,typename T,typename arith>

T accumulate<in beg,in end,T x,arith op>

返回所有元素和x进行某种运算的结果

函数adjacent_difference

template<typename in,typename out>

out adjacent_difference(in beg,in end,out p,)

返回创建的序列,序列第一个元素是输入序列的第一个元素,之后某一个元素是原序列当前元素与前一个元素之差

template<typename in,typename out,typename ari>

out adjacent_difference(in beg,in end,out p,,ari op)

返回创建的序列,序列第一个元素是输入序列的第一个元素,之后某一个元素是原序列当前元素与前一个元素按照某种运算的结果

函数inner_product

template<typename in1,typename in2,typename T>

T inner_product(in1 beg,in1 end,in2 beg, T x)

返回序列in1 beg-in1 end-1与序列in2 beg开始的两个序列之间内积和x之和

template<typename in1,typename in2,typename T,typename ari>

T inner_product(in1 beg,in1 end,in2 beg, T x,ari op1,ari op2)

与第一个类似,但是加法被op1替换,乘法被op2替换。

int main(){int a[]={1,2,3,4,5};vector<int> vv(5);ostream_iterator<int>out(cout," ");cout<<"the a:"<<endl;copy(a,a+5,out);cout<<"the sum of a:"<<endl;cout<<accumulate(a,a+5,0)<<endl;cout<<"the product of a:"<<endl;cout<<accumulate(a,a+5,1,multiplies<int>());vector<int>::iterator p=adjacent_difference(a,a+5,vv.begin());cout<<"the adjacent_difference of vv:"<<endl;copy(vv.begin(),p,out);cout<<"\n the inner product of a*a:"<<endl;cout<<inner_product(a,a+5,a,0)<<endl;p=partial_sum(a,a+5,vv.begin());cout<<"\n after partial_sum vv is:"<<endl;copy(vv.begin(),p,out);system("pause");return 0;}

0 0
原创粉丝点击