STL algorithm算法is_permutation(27)
is_permutation原型:
std::is_permutation
| equality (1) |
template <class ForwardIterator1, class ForwardIterator2> |
|---|---|
| predicate (2) |
template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate> |
该函数是用来推断两个序列是否为同一元素集的不同排列。
该函数使用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)的更多相关文章
- STL algorithm算法merge(34)
merge原型: std::merge default (1) template <class InputIterator1, class InputIterator2, class Outpu ...
- STL algorithm算法mismatch(37)
mismatch原型: std::mismatch equality (1) template <class InputIterator1, class InputIterator2> p ...
- STL algorithm算法lower_bound和upper_bound(31)
lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...
- STL algorithm算法minmax,minmax_element(36)
minmax原型: std::minmax C++11 C++14 default (1) template <class T> pair <const T&,const T ...
- 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& ...
- 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& ...
- STL algorithm算法mov,move_backward(38)
move原型: std::move template <class InputIterator, class OutputIterator> OutputIterator move (In ...
- STL algorithm算法make_heap和sort_heap(32)
make_heap原型: std::make_heap default (1) template <class RandomAccessIterator> void make_heap ( ...
- STL algorithm算法lexicographical_compare(30)
lexicographical_compare原型: std::lexicographical_compare default (1) template <class InputIterator ...
随机推荐
- ThinkPHP - 图片水印
图片添加水印 可以通过使用Image类的水印方法给图片添加水印支持,例如:water 给图片添加水印 用法 water($source, $water, $savename=null, $alph ...
- 【JavaScript】双引号问题
拼装字符串是遇到双引号冲突问题. 最后用"代替了平时的转码手段.
- 浙江大学2015年校赛F题 ZOJ 3865 Superbot BFS 搜索
不知道为什么比赛的时候一直想着用DFS 来写 一直想剪枝结果还是TLE = = 这题数据量不大,又是问最优解,那么一般来说是用 BFS 来写 int commandi[4] = {1, 2, 3, 4 ...
- USACO Money Systems Dp 01背包
一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V ...
- POJ 1379 Run Away 【基础模拟退火】
题意:找出一点,距离所有所有点的最短距离最大 二维平面内模拟退火即可,同样这题用最小圆覆盖也是可以的. Source Code: //#pragma comment(linker, "/ST ...
- 练习 jquery+Ajax+Json 绑定数据 分类: asp.net 练习 jquery+Ajax+Json 绑定数据 分类: asp.net
练习 jquery+Ajax+Json 绑定数据
- Windows API获取系统配置文件的配置参数
在Windows平台下获取系统配置文件(如:System.ini)的配置参数. 系统配置文件System.ini的内容如下: [SYSTEM] ServiceIP = 10.128.11.99:600 ...
- linux 内核代码精简
#为了提高性能,文件系统一般都是以 relatime形式挂载进来的,见:/etc/fstab #更新一下mtime,这样,编译过程中用到的文件的atime都会被更新 find . -exec touc ...
- 动态规划之一ones
n给一个整数n,要你找一个值为n的表达式,这个表达式只有1 + * ( ) 够成.并且1不能连续,比如11+1就不合法. n输入n,(1<=n<=10000) n输出最少需要多少个1才能构 ...
- vc根据域名获取IP地址 gethostbyname()函数
以下是VC Socket初始化时用到的两个函数 一.WSAStartup函数 int WSAStartup ( ...