1、STL数学算法
#include<iostream> #include<vector> #include<numeric> #include<iterator> #include<algorithm> using namespace std; int a[10]={1,2,3,4,5,6,7,8,9,10}; int b[10]={1,2,3,2,4,5,6,2,8,9}; bool Greater(int x){return x>6;} void square1(int x){cout<<x*x<<" ";} int square2(int x){return x*x;} int main() { ostream_iterator<int> output(cout," "); vector<int> v(a,a+10); random_shuffle(v.begin(),v.end());//将vector v中从v.begin()到v.end()中的所有元素随机排序,但不包括v.end() //replace_if(v.begin(),v.end(),Greater,20);//将从v3.begin()到v3.end()中满足greater函数d的所有元素改为20 copy(v.begin(),v.end(),output);//注意输出~~ cout<<endl; vector<int> v1(b,b+10); int result=count(v1.begin(),v1.end(),2);//返回vector v1中从v1.begin()到v1.end()中为的2的所有元素之和,但不包括v1.end() copy(v1.begin(),v1.end(),output); cout<<endl; //result=count_if(v1.begin(),v1.end(),Greater);//返回满足Greater函数中返回true的元素个数 cout<<*(min_element(v1.begin(),v1.end()))<<endl;//返回vector v中从v.begin()到v.end()中的最小元素 ,但不包括v.end() cout<<*(max_element(v1.begin(),v1.end()))<<endl;//返回最大元素~~~ cout<<accumulate(v1.begin(),v1.end(),0)<<endl;//返回vector v中从v.begin()到v.end()中所有元素之和,但不包括v.end() for_each(v.begin(),v.end(),square1);//对从v.begin()到v.end()的每个元素都采用这个常用函数 cout<<endl; vector<int> v2(10); transform(v.begin(),v.end(),v2.begin(),square2); copy(v2.begin(),v2.end(),output); cout<<endl; return 0; }
2、swap,iter_swap,swap_ranges
#include<iostream> #include<iterator> #include<algorithm> using namespace std; int a[10]={1,2,3,4,5,6,7,8,9,10}; int main() { ostream_iterator<int> output(cout," "); copy(a,a+10,output); cout<<endl; swap(a[0],a[1]);//不解释~~ iter_swap(&a[0],&a[1]);//交换两个值,且会交换迭代器所指的元素值 swap_ranges(a,a+5,a+5);//交换从a开始到a+5(不包括a+5)的元素与从a+5开始的元素 copy(a,a+10,output); cout<<endl; return 0; }
3、copy_backward,merge
#include<iostream> #include<vector> #include<iterator> #include<algorithm> using namespace std; int a[5]={2,4,6,7,9},b[5]={1,3,4,5,7}; int main() { ostream_iterator<int> output(cout," "); vector<int> v(a,a+5); vector<int> v1(b,b+5); vector<int> v2(v.size()); copy_backward(v.begin(),v.end(),v2.end());//将vector v复制给v2 vector<int> v3(v.size()+v1.size()); merge(v.begin(),v.end(),v1.begin(),v1.end(),v3.begin());//组合两个升序序列,变成第三个升序序列 copy(v3.begin(),v3.end(),output); cout<<endl; return 0; }
4、lower_bound,upper_bound,equal_range
#include<iostream> #include<iterator> #include<algorithm> #include<vector> using namespace std; int a[10]={1,1,2,4,4,4,5,5,5,6}; vector<int> v(a,a+10); int main() { ostream_iterator<int> output(cout," "); vector<int>::iterator lower; lower=lower_bound(v.begin(),v.end(),4);//确定排序数值序列中插入第三个参数而保持升序的第一个位置 cout<<lower-v.begin()<<endl; vector<int>::iterator upper; upper=upper_bound(v.begin(),v.end(),4);//确定排序数值序列中插入第三个参数而保持升序的最后一个位置 cout<<upper-v.begin()<<endl; pair<vector<int>::iterator,vector<int>::iterator> Pa;//返回一个正向迭代器,包含lower_bound和upper_bound的组合结果 Pa=equal_range(v.begin(),v.end(),4); cout<<Pa.first-v.begin()<<" "<<Pa.second-v.begin()<<endl; return 0; }
例题1:NYOJ 214(单调递增子序列),题目要求输入的数据量比较的大,普通的动态规划会超时,调用STL库中的lower_bound的话会使程序变得很简单,下面为这道题的标准程序,我没有想到这种方法,贴一下~~动态规划做法请点这里。
#include<cstdio> #include<algorithm> using namespace std; const int MAX=100100; int num[MAX],top=0; int main() { int n; while(~scanf("%d",&n)) { scanf("%d",&num[0]); top=1; for(int i=1;i!=n;i++) { scanf("%d",&num[i]); int * p=lower_bound(num,num+top,num[i]); if(p-num==top) ++top; *p=num[i]; } printf("%d\n",top); } return 0; }
5、inplace_merge,unique_copy和reverse_copy
#include<iostream> #include<iterator> #include<algorithm> #include<vector> using namespace std; int a[10]={1,2,4,5,6,1,2,4,5,6}; vector<int> v(a,a+10); int main() { ostream_iterator<int> output(cout," "); inplace_merge(v.begin(),v.begin()+5,v.end());//合并同一容器中的排序元素序列 copy(v.begin(),v.end(),output); cout<<endl; vector<int> v1; unique_copy(v.begin(),v.end(),back_inserter(v1));//复制从v.begin()到v.end()中的唯一元素到v1中 copy(v1.begin(),v1.end(),output); cout<<endl; vector<int> v2; reverse_copy(v.begin(),v.end(),back_inserter(v2));//逆向复制v1.begin()到v1.end()到v2中 copy(v2.begin(),v2.end(),output); cout<<endl; return 0; }
6、container:iterator这种迭代器以"读/写"模式遍历元素,container::const_iterator是以"只读"模式遍历元素
7、Stream Iterators(流迭代器)
#include<iostream> #include<iterator> #include<vector> #include<string> #include<algorithm> using namespace std; vector<string> v; int main() { copy(istream_iterator<string>(cin),//会产生一个可从"标准输入流cin"读取数据的stream iterator,这些元素会透过一般的operator>>被读取进来,换句话说这个算法是:逐词读取 istream_iterator<string>(),//产生一个代表"流结束符号"的迭代器,意义是:你不能再从中读取任何字符,相当于c中的EOF,可按ctrl+c停止 back_inserter(v));//作用是安插在容器最尾端,front_inserter(v):插在最前端 sort(v.begin(),v.end()); unique_copy(v.begin(),v.end(),ostream_iterator<string>(cout,"\n")); return 0; }
8、使用remove删除相同元素时的正确做法:
#include<iostream> #include<iterator> #include<list> #include<algorithm> using namespace std; list<int> L; int main() { ostream_iterator<int> output(cout," "); for(int i=1;i<=5;i++) { L.push_front(i); L.push_back(i); } remove(L.begin(),L.end(),2); copy(L.begin(),L.end(),output);//会输出:5 4 3 1 1 3 4 5 4 5,由于end()还是返回的当初的那个终点,有些元素虽然被删除了,2被其后的元素覆盖了,至于在后面尾端的那些未被覆盖的元素还是原封不动留在那,但是后面的那些元素已经不属于这个链表了~~ cout<<endl; return 0; }
改进做法:
#include<iostream> #include<iterator> #include<list> #include<algorithm> using namespace std; list<int> L; int main() { ostream_iterator<int> output(cout," "); for(int i=1;i<=5;i++) { L.push_front(i); L.push_back(i); } list<int>::iterator pos=remove(L.begin(),L.end(),2);//"被修改之群集"进过元素的移除操作后逻辑上的新终点,可以当做新的终点使用 cout<<distance(pos,L.end())<<endl;//返回两个迭代器之间的距离 L.erase(pos,L.end());//删除后面的几个元素 copy(L.begin(),L.end(),output); cout<<endl; return 0; }
9、将元素传入map中的方法
/*****方法1:使用:value_type*****/ map<string,float> m; ... m.insert(map<string::float>::value_type("xuzengqiang",88.0)); /*****方法2:使用pair<>*****/ m.insert(pair<string,float>)("xuzengqiang",88.0); /*****方法3:使用make_pair()*****/ m.insert(make_pair("xuzengqiang",88.0));
10、STL中的sort排序
stable_sort 对给定区间所有元素进行稳定排序 //例如:stable_sort(v.begin(),v.end(),mysort);其中mysort为一个排序函数,自定义
partial_sort 对给定区间所有元素部分排序
partial_sort_copy 对给定区间复制并排序
nth_element 找出给定区间的某个位置对应的元素
is_sorted 判断一个区间是否已经排好序
partition 使得符合某个条件的元素放在前面
stable_partition 相对稳定的使得符合某个条件的元素放在前面
其中:例如:sort(v.begin(),v.end(),greater<int>());
equal_to 相等
not_equal_to 不相等
less 小于
greater 大于
less_equal 小于等于
greater_equal 大于等于
区别:带有stable的函数可保证相等元素的原本相对次序在排序后保持不变!
partial_sort()任何时候的复杂度都为:n*log(n),且如果你只需前n个元素排序,则它会在完成任务后立即停止:partial_sort(v.begin(),v,begin()+n,v.end());~~~当然都可以在后面加入排序函数
其余的就不说了,以后见到了再提提~~~~