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

——————————————————————————————————————————————————————————————————


STL algorithm算法is_permutation(27)的更多相关文章

  1. STL algorithm算法merge(34)

    merge原型: std::merge default (1) template <class InputIterator1, class InputIterator2, class Outpu ...

  2. STL algorithm算法mismatch(37)

    mismatch原型: std::mismatch equality (1) template <class InputIterator1, class InputIterator2> p ...

  3. STL algorithm算法lower_bound和upper_bound(31)

    lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...

  4. STL algorithm算法minmax,minmax_element(36)

    minmax原型: std::minmax C++11 C++14 default (1) template <class T> pair <const T&,const T ...

  5. STL algorithm算法min,min_element(35)

    min样板: std::min C++98 C++11 C++14 default (1) template <class T> const T& min (const T& ...

  6. STL algorithm算法max,max_elements(33)

    max原型: std::max C++98 C++11 C++14 default (1) template <class T> const T& max (const T& ...

  7. STL algorithm算法mov,move_backward(38)

    move原型: std::move template <class InputIterator, class OutputIterator> OutputIterator move (In ...

  8. STL algorithm算法make_heap和sort_heap(32)

    make_heap原型: std::make_heap default (1) template <class RandomAccessIterator> void make_heap ( ...

  9. STL algorithm算法lexicographical_compare(30)

    lexicographical_compare原型: std::lexicographical_compare default (1) template <class InputIterator ...

随机推荐

  1. BZOJ 1911: [Apio2010]特别行动队( dp + 斜率优化 )

    sum为战斗力的前缀和 dp(x) = max( dp(p)+A*(sumx-sump)2+B*(sumx-sump)+C )(0≤p<x) 然后斜率优化...懒得写下去了... ------- ...

  2. 使用Atlas实现MySQL读写分离+MySQL-(Master-Slave)配置

    参考博文: MySQL-(Master-Slave)配置  本人按照博友北在北方的配置已成功  我使用的是 mysql5.6.27版本. 使用Atlas实现MySQL读写分离 数据切分——Atlas读 ...

  3. Python之路Day10

    本节主要内容:memcache&redis.RabbitMQ.twisted框架 1. memcache&redis 1.1 memcache Memcached 是一个高性能的分布式 ...

  4. QDebug &operator<<出错(根据QString来找,是不得要领的,而是应该根据QString所在的对象来思考)

    程序运行后,总是崩溃在这个地方:inline QDebug &operator<<(const QString & t) 我应该用什么办法找出是哪个QString出了问题呢 ...

  5. docker学习笔记15:Dockerfile 指令 USER介绍

    USER指令用于指定容器执行程序的用户身份,默认是 root用户. 在docker run 中可以通过 -u 选项来覆盖USER指令的设置. 举例:docker run -i -t -u mysql ...

  6. cglib源码学习交流

    背景 前段时间在工作中,包括一些代码阅读过程中,spring aop经常性的会看到cglib中的相关内容,包括BeanCopier,BulkBean,Enancher等内容,以前虽大致知道一些内容,原 ...

  7. Pencil OJ 02 安装

    Mongo 官方的安装方法 官方教程已经很好啦,这里就不罗嗦了. 源码编译 待补.我是从这里看到的. 遇到的问题 启动时的警告信息 2015-03-06T21:01:15.526-0800 I CON ...

  8. 构件图(Component Diagram)—UML图(八)

    构件图是显示代码自身结构的实现级别的图表.构件图由诸如源码文件.二进制代码文件.可运行文件或动态链接库 (DLL) 这种构件构成,并通过依赖关系相连接 以下这张图介绍了构件图的基本内容: 以下这张图是 ...

  9. [产值分析]生产部KPI考核之产值分析

    接到新任务:设计统计电子和磁电公司生产部产值分析报表. 眼下状况: 1.电子公司:取最新单位价格*入库数量 2.磁电公司:取最低价格*入库数量(实际取价的时候又没有取到最低价) 假设计算出来的结果和財 ...

  10. Column store index 列数据如何匹配成行数据?

    SQL Server 2012引入了列存储索引,对每列的数据进行分组和存储,然后联接所有列以完成整个索引.这不同于传统索引,传统索引对每行的数据进行分组和存储,然后联接所有行以完成整个索引. 在访问基 ...