is_permutation原型:
std::is_permutation
equality (1)
template <class ForwardIterator1, class ForwardIterator2>
bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2);
|
predicate (2)
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>
bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, BinaryPredicate pred);
|
该函数是用来判断两个序列是否为同一元素集的不同排列!
该函数使用operator==或者是pred来判断两个元素是否是相等的。
其行为类似:
template <class InputIterator1, class InputIterator2>
bool is_permutation (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2)
{
std::tie (first1,first2) = std::mismatch (first1,last1,first2);
if (first1==last1) return true;
InputIterator2 last2 = first2; std::advance (last2,std::distance(first1,last1));
for (InputIterator1 it1=first1; it1!=last1; ++it1) {
if (std::find(first1,it1,*it1)==it1) {
auto n = std::count (first2,last2,*it1);
if (n==0 || std::count (it1,last1,*it1)!=n) return false;
}
}
return true;
}
坑的是windows下面的codeblock居然不支持这个函数,我汗!
一个简单的测试例子:
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
using namespace std;
int main(){
vector<int> v1{1,2,3,4,5,6};
vector<int> v2{1,5,6,4,3,2};
vector<int> v3{1,3,2,5,6,4,1};//add a elements 1
vector<double> v4{1,2,4,3,5,6};
vector<int> v5{1,0,2,3,4,5,6};
array<int,6> ai{6,5,3,4,2,1};
cout<<"v1=";
for(int &i:v1)
cout<<i<<" ";
cout<<endl;
cout<<"v2=";
for(int &i:v2)
cout<<i<<" ";
cout<<endl;
cout<<"v3=";
for(int &i:v3)
cout<<i<<" ";
cout<<endl;
cout<<"v4=";
for(double &i:v4)
cout<<i<<" ";
cout<<endl;
cout<<"v5=";
for(int &i:v5)
cout<<i<<" ";
cout<<endl;
cout<<"ai=";
for(int &i:ai)
cout<<i<<" ";
cout<<endl;
if(is_permutation(v1.begin(),v1.end(),v2.begin()))
cout<<"Yes ,v1 and v2 is permutation!"<<endl;
else
cout<<"No ,v1 and v2 is not permutation!"<<endl;
if(is_permutation(v1.begin(),v1.end(),v3.begin()))
cout<<"Yes ,v1 and v3 is permutation!"<<endl;
else
cout<<"No ,v1 and v3 is not permutation!"<<endl;
if(is_permutation(v1.begin(),v1.end(),v4.begin()))
cout<<"Yes ,v1 and v4 is permutation!"<<endl;
else
cout<<"No ,v1 and v4 is not permutation!"<<endl;
if(is_permutation(v1.begin(),v1.end(),v5.begin()))
cout<<"Yes ,v1 and v5 is permutation!"<<endl;
else
cout<<"No ,v1 and v5 is not permutation!"<<endl;
if(is_permutation(v1.begin(),v1.end(),ai.begin()))
cout<<"Yes ,v1 and ai is permutation!"<<endl;
else
cout<<"No ,v1 and ai is not permutation!"<<endl;
}
运行的结果:
可以看到,虽然v3多了一个元素1,但是v1和v3还是属于同一元素集的不同排列!
v4的数据类型为double,也是一样!
——————————————————————————————————————————————————————————————————
//写的错误或者不好的地方请多多指导,可以在下面留言或者点击左上方邮件地址给我发邮件,指出我的错误以及不足,以便我修改,更好的分享给大家,谢谢。
转载请注明出处:http://blog.csdn.net/qq844352155
author:天下无双
Email:coderguang@gmail.com
2014-9-17
于GDUT
——————————————————————————————————————————————————————————————————
分享到:
相关推荐
algorithm algorithm STL 算法 algorithm_头文件_说明 algorithm algorithm STL 算法 algorithm_头文件_说明 algorithm algorithm STL 算法 algorithm_头文件_说明
用于三维光学形貌扫描完成后,生成的stl文件的读取,并形成俯视投影云图
stlshow_stl分层_STL分层_stlmatlab_STL切片_stl分层.zip
C++读取STL文件,输出所有三角形的顶点坐标
STL课件_C++_课件stl_c++课件_源码.zip
stlshow_stl分层_STL分层_stlmatlab_STL切片_stl分层_源码.rar.rar
STL算法(for_each/transform)
一:next_permutation(start,end,//cmp) 使用默认排序方法:按照字典序从小到大 int arr[3]={1,2,3}; do{ for(int num:arr){ cout<<num<< ; } cout<<endl; }while(next_permutation...
09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板09_07_IO_STL标准模板...
stl的入门ppt 很好用哦,分为导入 STL的概念与组成 Iterator(迭代器) Container(容器) Algorithm(算法) Adaptors(配接器)
STL学习教程,
STL 最新源代码
SGI STL是由SGI公司开发的STL库.这份STL可以用在VC上.
vc显示stl,读取顶点坐标,并显示在独立窗口中
C++ STL 参考手册Cpp_STL_ReferenceManual ,对学习STL 有帮助
在MATLAB中完成STL数据的渲染,用PATCH函数完成加颜色和亮度等功能
读取、显示STL格式的3D文件,可对二进制的STL文件进行读取、显示。显示模式可以选择有网格、无网格等。
最新的STL源码,最新的STL源码,最新的STL源码
看STL文件的小软件,可以自由的实现旋转,等功能,现在只是一小部分,以后会发后面的
十多个STL的经典例子,相当不错,初学STL必备