includes原型:
std::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 );
该函数是测试[first1,last1)里面是否包含了
[first2,last2)里面的所有元素。
也就是说,后面那一个区间是否是前一个区间的子集。
[first1,last1)和[first2,last2)里面的元素应该先排好序,并且是使用同一规则排序。
其行为类似如下:
2
3
4
5
6
7
8
9
10
11
|
template <class InputIterator1, class InputIterator2>
bool includes (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2)
{
while (first2!=last2) {
if ( (first1==last1) || (*first2<*first1) ) return false;
if (!(*first1<*first2)) ++first2;
++first1;
}
return true;
}
|
|
一个简单的例子:
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
using namespace std;
void includes2(){
vector<int> vi{1,5,8,19,78,100,56};
array<int,3> ai{19,100,56};
vector<int> v2{5,11,20};
sort(vi.begin(),vi.end());
sort(ai.begin(),ai.end());
sort(v2.begin(),v2.end());
cout<<"at first,vi=";
for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});
cout<<endl;
cout<<"at first,ai=";
for_each(ai.begin(),ai.end(),[](int i){cout<<i<<" ";});
cout<<endl;
cout<<"at first,v2=";
for_each(v2.begin(),v2.end(),[](int i){cout<<i<<" ";});
cout<<endl;
if(includes(vi.begin(),vi.end(),ai.begin(),ai.end()))
cout<<"includes(vi.begin(),vi.end(),ai.begin(),ai.end()) return true!"<<endl;
else
cout<<"includes(vi.begin(),vi.end(),ai.begin(),ai.end()) return false!"<<endl;
if(includes(vi.begin(),vi.end(),v2.begin(),v2.end()))
cout<<"includes(vi.begin(),vi.end(),v2.begin(),v2.end()) return true!"<<endl;
else
cout<<"includes(vi.begin(),vi.end(),v2.begin(),v2.end()) return false!"<<endl;
}
运行截图:
如果不排序,则有会发生错误!
例如下面的例子:
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
using namespace std;
void includes2(){
vector<int> vi{1,5,8,19,78,100,56};
array<int,4> ai{19,100,56,1};
vector<int> v2{5,11,20};
// sort(vi.begin(),vi.end());
// sort(ai.begin(),ai.end());
// sort(v2.begin(),v2.end());
cout<<"at first,vi=";
for_each(vi.begin(),vi.end(),[](int i){cout<<i<<" ";});
cout<<endl;
cout<<"at first,ai=";
for_each(ai.begin(),ai.end(),[](int i){cout<<i<<" ";});
cout<<endl;
cout<<"at first,v2=";
for_each(v2.begin(),v2.end(),[](int i){cout<<i<<" ";});
cout<<endl;
if(includes(vi.begin(),vi.end(),ai.begin(),ai.end()))
cout<<"includes(vi.begin(),vi.end(),ai.begin(),ai.end()) return true!"<<endl;
else
cout<<"includes(vi.begin(),vi.end(),ai.begin(),ai.end()) return false!"<<endl;
if(includes(vi.begin(),vi.end(),v2.begin(),v2.end()))
cout<<"includes(vi.begin(),vi.end(),v2.begin(),v2.end()) return true!"<<endl;
else
cout<<"includes(vi.begin(),vi.end(),v2.begin(),v2.end()) return false!"<<endl;
}
运行截图:
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-16
于GDUT
——————————————————————————————————————————————————————————————————
分享到:
相关推荐
STL所有算法介绍STL所有算法介绍STL所有算法介绍
STL常用算法简介 STL常用算法简介 STL常用算法简介 STL常用算法简介 STL常用算法简介
stl常用算法,模板stl常用算法,
STL常用算法大全
参考自侯捷先生的《STL源码剖析》,C++ STL 的数值算法(Numeric algorithms)是一组对容器元素进行数值计算的模板函数,包括容器元素求和 accumulate 、两序列元素的内积 inner_product 、容器元素的一系列部分元素和...
STL和常用算法简介
algorithm algorithm STL 算法 algorithm_头文件_说明 algorithm algorithm STL 算法 algorithm_头文件_说明 algorithm algorithm STL 算法 algorithm_头文件_说明
ACM STL算法入门 导入 STL的概念与组成 Iterator(迭代器) Container(容器) Algorithm(算法) Adaptors(配接器)
自己根据编程原本与使用C++的经历写的STL的algorithm的实现,不好的地方请指出
STL常用算法 七十个 12
C++ stl算法汇总 STL 各种算法 应用 大全
STL切片算法,一种快速高效解决stl文件切片的程序算法讲解
C++精选代码库。包含常用STL容器模拟实现、algorithm算法头文件函数demo
C++库函数及STL算法(英文版)介绍及用法 每个函数都有简单的源文件说明,有例子
STL算法部分主要由头文件<algorithm>,,组成。要使用 STL中的算法函数必须包含头文件<algorithm>,对于数值算法须包含,中则定义了一些模板类,用来声明函数对象。
该文档收录了STL中的部分常用的算法,非常实用。。。对于ACM编程着很大的帮助。。。
。。。
。。。
stl学习资料说明,stl基本使用说明,stl常用算法说明
关于stl的一些用法 STL算法作为模板函数提供 STL另一个重要特性是它不是面向对象的。为了具有足够通用性,STL主要依赖于模板而不是封装,继承和虚函数(多态性)——OOP的三个要素。