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)里面的元素应该先排好序,并且是使用同一规则排序。
其行为类似如下:
|
|
一个简单的例子:
#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:[email protected]
2014-9-16
于GDUT
——————————————————————————————————————————————————————————————————
时间: 2024-10-28 19:29:31