1、merge:将两个序列合并成一个新的序列,并对新的序列排序
原型:
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator merge ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,OutputIterator result );
template <class InputIterator1, class InputIterator2,class OutputIterator, class Compare>
OutputIterator merge ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp );
示例:
//mergealgorithmexample
#include<iostream>
#include<algorithm>
#include<vector>
usingnamespacestd;
intmain(){
intfirst[]={5,10,15,20,25};
intsecond[]={50,40,30,20,10};
vector<int>v(10);
vector<int>::iteratorit;
sort(first,first+5);
sort(second,second+5);
merge(first,first+5,second,second+5,v.begin());
cout<<"Theresultingvectorcontains:";
for(it=v.begin();it!=v.end();++it)
cout<<""<<*it;
cout<<endl;
return0;
}
2、inplace_merge:将两个序列合并成一个新的序列,并对新的序列进行归并排序(这两个序列必须要进过排序)
原型:
template <class BidirectionalIterator>
void inplace_merge ( BidirectionalIterator first, BidirectionalIterator middle,
BidirectionalIterator last );
template <class BidirectionalIterator, class Compare>
void inplace_merge ( BidirectionalIterator first, BidirectionalIterator middle,
BidirectionalIterator last, Compare comp );
示例:
//inplace_mergeexample
#include<iostream>
#include<algorithm>
#include<vector>
usingnamespacestd;
intmain(){
intfirst[]={5,10,15,20,25};
intsecond[]={50,40,30,20,10};
vector<int>v(10);
vector<int>::iteratorit;
sort(first,first+5);
sort(second,second+5);
copy(first,first+5,v.begin());
copy(second,second+5,v.begin()+5);
inplace_merge(v.begin(),v.begin()+5,v.end());
cout<<"Theresultingvectorcontains:";
for(it=v.begin();it!=v.end();++it)
cout<<""<<*it;
cout<<endl;
return0;
}
3、includes:测试是一个序列是否在另一个序列中
原型:
template <class InputIterator1, class InputIterator2>
bool includes ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2 );
template <class InputIterator1, class InputIterator2, class Compare>
bool includes ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2, Compare comp );
示例:
//includesalgorithmexample
#include<iostream>
#include<algorithm>
usingnamespacestd;
boolmyfunction(inti,intj){returni<j;}
intmain(){
intcontainer[]={5,10,15,20,25,30,35,40,45,50};
intcontinent[]={40,30,20,10};
sort(container,container+10);
sort(continent,continent+4);
//usingdefaultcomparison:
if(includes(container,container+10,continent,continent+4))
cout<<"containerincludescontinent!"<<endl;
//usingmyfunctionascomp:
if(includes(container,container+10,continent,continent+4,myfunction))
cout<<"containerincludescontinent!"<<endl;
return0;
}
4、set_union:和merge类似,不过新序列中没有重复的元素
原型:
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_union ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,OutputIterator result );
template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
OutputIterator set_union ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp );
示例:
//set_unionexample
#include<iostream>
#include<algorithm>
#include<vector>
usingnamespacestd;
intmain(){
intfirst[]={5,10,15,20,25};
intsecond[]={50,40,30,20,10};
vector<int>v(10);//0000000000
vector<int>::iteratorit;
sort(first,first+5);//510152025
sort(second,second+5);//1020304050
it=set_union(first,first+5,second,second+5,v.begin());
//51015202530405000
cout<<"unionhas"<<int(it-v.begin())<<"elements.\n";
return0;
}
5、set_intersection:两个序列的交集
原型:
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,OutputIterator result );
template <class InputIterator1, class InputIterator2,class OutputIterator, class Compare>
OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp );
示例:
//set_intersectionexample
#include<iostream>
#include<algorithm>
#include<vector>
usingnamespacestd;
intmain(){
intfirst[]={5,10,15,20,25};
intsecond[]={50,40,30,20,10};
vector<int>v(10);//0000000000
vector<int>::iteratorit;
sort(first,first+5);//510152025
sort(second,second+5);//1020304050
it=set_intersection(first,first+5,second,second+5,v.begin());
//102000000000
cout<<"intersectionhas"<<int(it-v.begin())<<"elements.\n";
return0;
}
6、set_difference:序列(first1,last1)不在序列(first2,last2)中的元素
原型:
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,outputIterator result );
template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
OutputIterator set_intersection ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp );
示例:
//set_differenceexample
#include<iostream>
#include<algorithm>
#include<vector>
usingnamespacestd;
intmain(){
intfirst[]={5,10,15,20,25};
intsecond[]={50,40,30,20,10};
vector<int>v(10);//0000000000
vector<int>::iteratorit;
sort(first,first+5);//510152025
sort(second,second+5);//1020304050
it=set_difference(first,first+5,second,second+5,v.begin());
//515250000000
cout<<"differencehas"<<int(it-v.begin())<<"elements.\n";
return0;
}
7、set_symmetric_difference:所有不在序列(first1,last1)和序列(first2,last2)中的元素
原型:
template <class InputIterator1, class InputIterator2, class OutputIterator>
OutputIterator set_symmetric_difference ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,OutputIterator result );
template <class InputIterator1, class InputIterator2, class OutputIterator, class Compare>
OutputIterator set_symmetric_difference ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,OutputIterator result, Compare comp );
示例:
//set_symmetric_differenceexample
#include<iostream>
#include<algorithm>
#include<vector>
usingnamespacestd;
intmain(){
intfirst[]={5,10,15,20,25};
intsecond[]={50,40,30,20,10};
vector<int>v(10);//0000000000
vector<int>::iteratorit;
sort(first,first+5);//510152025
sort(second,second+5);//1020304050
it=set_symmetric_difference(first,first+5,second,second+5,v.begin());
//515253040500000
cout<<"symmetricdifferencehas"<<int(it-v.begin())<<"elements.\n";
return0;
}
分享到:
相关推荐
Effective STL中文版:50条有效使用STL的经验 高清PDF
通过替代STL std :: allocator的固定块来防止堆碎片错误并提高执行速度
STL顺序容器:list的使用。 (Lists将元素按顺序储存在链表中. 与 向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢. )
c++ STL, stl,stl的学习c++ STL, stl,stl的学习c++ STL, stl,stl的学习c++ STL, stl,stl的学习
C++库函数及STL算法(英文版)介绍及用法 每个函数都有简单的源文件说明,有例子
C++ stl算法汇总 STL 各种算法 应用 大全
ACM STL算法入门 导入 STL的概念与组成 Iterator(迭代器) Container(容器) Algorithm(算法) Adaptors(配接器)
--stl_defaultTabTextColor : 字体颜色 --> <!--stl_indicatorColor : (指示器)动画下划线的颜色 --> <!--stl_indicatorCornerRadius : (指示器)动画下划线的圆角度数 --> <!--stl_indicatorThickness : ...
STL所有算法介绍STL所有算法介绍STL所有算法介绍
STL算法一览...............
C++ STL std的string详细讲解
stl容器简介,包括STL概念及具体容器使用方法等
STL常用算法简介 STL常用算法简介 STL常用算法简介 STL常用算法简介 STL常用算法简介
关于stl的一些用法 STL算法作为模板函数提供 STL另一个重要特性是它不是面向对象的。为了具有足够通用性,STL主要依赖于模板而不是封装,继承和虚函数(多态性)——OOP的三个要素。
。。。
。。。
50条有效使用STL的经验
STL 我觉得STL是一个很好的编程工具,你会发现它能带给你很多
答案
光谱延时 (STL) 算法旨在成为一种简单有效的技术,用于在二维图像表示中分析和呈现动物运动的空间和时间信息。 STL 算法在每个时间点用特定于时间的颜色重新编码动物的位置,并将其覆盖在视频的参考帧上,以生成摘要...