next_permutation、prev_permutation以及is_permutation使用

  • next_permutation
  • prev_permutation
  • is_permutation

next_permutation、prev_permutation以及is_permutation均为全排列相关的函数,调用时需要加入头文件#include <algorithm>

next_permutation

next_permutation的函数原型:

//default
template <class BidirectionalIterator>bool next_permutation (BidirectionalIterator first, BidirectionalIterator last);//custom
template <class BidirectionalIterator, class Compare>bool next_permutation (BidirectionalIterator first, BidirectionalIterator last, Compare comp);

next就是下一个的意思,其中文直译过来就是下一个全排列,因此在使用next_permutation函数的时候切记要对全排列的对象进行升序排列,不然结果只会出现比当前排列大的排列,依据ASCII计算。
next_permutation(a, b)表示只对[a, b)区间的数据进行全排列,其余位置不变化

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
#define SORTint main()
{vector<int> V = {3, 2, 1 };
#ifdef SORTsort(V.begin(), V.end());//sort
#endifdo{for (int i = 0; i < V.size(); i++){cout << V[i] << " ";}cout << endl;} while (next_permutation(V.begin(), V.end()));return 0;
}
//result
/*
#define SORT
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
*//*
没有进行sort排序
3 2 1
*/

prev_permutation

prev_permutation和next_permutation功能一样,唯一的区别是next_permutation下一个全排列,而prev_permutation是上一个全排列,所以运用prev_permutation的时候必须降序排列,不然只会出现比结果小的排列

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;//#define SORT
#define RSORTint main()
{vector<int> V = {3, 4, 2, 1};
#ifdef SORTsort(V.begin(), V.end());
#elsesort(V.rbegin(), V.rend());
#endifdo{for (int i = 0; i < V.size(); i++){cout << V[i] << " ";}cout << endl;} while (prev_permutation(V.begin() + 1, V.end() - 1));return 0;
}
//result
/*
#define RSORT
4 3 2 1
4 2 3 1
*//*
#define RSORT
1 2 3 4
*/

is_permutation

template <class ForwardIterator1, class ForwardIterator2>bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2);template <class ForwardIterator1, class ForwardIterator2, class BinaryPredicate>bool is_permutation (ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2, BinaryPredicate pred);

is_permutation用来判断两个全排列是否相等,哪怕顺序不同,只要元素一样就返回true

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{vector<int> V1 = { 3, 4, 2, 1 };vector<int> V2 = { 1, 3, 2, 4 };vector<int> V3 = { 1, 3, 2, 4, 1};vector<float> V4 = { 1.0, 3.0, 2.0, 4.0 };vector<float> V5 = { 1.0000001, 3.0, 2.0, 4.0 };if (is_permutation(V1.begin(), V1.end(), V2.begin())){cout << "V1 and V2 is equal" << endl;}if (is_permutation(V1.begin(), V1.end(), V3.begin())){cout << "V1 and V3 is equal" << endl;}if (is_permutation(V1.begin(), V1.end(), V4.begin())){cout << "V1 and V4 is equal" << endl;}if (is_permutation(V1.begin(), V1.end(), V5.begin())){cout << "V1 and V5 is equal" << endl;}return 0;
}//result
/*
V1 and V2 is equal
V1 and V3 is equal
V1 and V4 is equal
*/

is_permutation单纯的比较值的大小,但是需要注意的是序列1重复的必须在序列2中展现,也就是count sequence1[i] <= count sequence2[i]。

next_permutation、prev_permutation以及is_permutation使用相关推荐

  1. STL经典算法集锦之排列(next_permutation/prev_permutation

    STL经典算法集锦之排列(next_permutation/prev_permutation) 来自:CSDN博客推荐文章 | 时间:2012-05-07 14:54:09 原文链接: http:// ...

  2. 【C++】next_permutation / prev_permutation函数

    关于next_permutation函数 next_permutation和prev_permutation函数都是C++STL中的全排列函数. 函数原型: #include < algorit ...

  3. C++标准库---排列元素next_permutation()prev_permutation()

    bool next_permutation(beg,end) bool prev_permutation(beg,end) next_permutation()会改变区间[beg,end)内的元素次序 ...

  4. C++ STL next_permutation() prev_permutation(a,a+n)用法。

    int a[3] = {1,2,3}; a可能形成的集合为{1,2,3},{1,3,2},{2,1,3},{2,3,1},{3,1,2},{3,2,1}. {2,1,3}的prev是{1,3,2}, ...

  5. c++中的全排列函数next_permutation()

    全排列函数next_permutation() prev_permutation函数(按降序排序) 计算序列全排列的函数:next_permutation(start,end),此函数求的是当前排列的 ...

  6. STL_算法(17)_排列组合 next_permutation() perv_permutation()

    next_permutation() prev_permutation() #include<iostream> #include<algorithm> #include< ...

  7. c++高级编程学习笔记4

    C++运算符重载 运算符重载概述 根据第 1 章的描述,C++中的运算符是一些类似于+.<.*和<<的符号.这些运算符可应用于内建类型,例如 int 和 double,从而实现算术操 ...

  8. 数据结构与算法(基于<algorithm>)

    algorithm算法库 一.排序算法(sort.stable_sort.partial_sort.nth_element) 1.代码示例 2.运行结果 二.查找算法(find.find_if.cou ...

  9. 北邮复试 | 北邮机试往年题汇总 | 计算机院 | 网研院 | 网安院 | 软院

    目录 网研院 北邮考研复试 | 机试 | 2014年北邮网研院机试(下午) 计算机院 北邮考研复试 | 机试 | 2014年北邮计算机院机试(下午) 北邮考研复试 | 机试 | 2014年北邮计算机院 ...

最新文章

  1. Canal Adapter二次开发,实现MySQL实时同步到Redis
  2. 计组-数据通路的功能和基本结构
  3. Objective-C语法之static关键字的那些事儿(六)
  4. Bzoj5093: 图的价值
  5. linux下的静态库与动态库
  6. 判断回文递归算法实现
  7. 7.1图的定义和术语
  8. openCVPracticalExercise学习笔记03
  9. Lecture 3:动态规划
  10. 计算机组成原理(白中英) 第一章课后题部分答案(知识点汇总及扩展)
  11. webserver接口调用(一)
  12. java 树什么意思是什么意思是什么_Java数据结构和算法 - 什么是2-3-4树
  13. python影评_用Python分析18万条《八佰》影评,看看观众怎么说?
  14. Tablespace free space
  15. 数据分析 - 9.MECE法(学习笔记)
  16. Android手机如何修改DPI触发平板模式
  17. Pygame 实现一个简单画图板
  18. matplotlib给某一个点添加注释
  19. 为什么王者荣耀总是服务器中断,王者荣耀服务器正在维护中怎么回事 7月4日王者荣耀进不去怎么办...
  20. 基金买卖波段指标 主图 源码 效果图

热门文章

  1. 看懂自适应模糊神经网络(ANFIS)并附ANFIS代码
  2. 咕泡学院P5人工智能深度学习笔记
  3. 1月18日主题讨论日
  4. 第78届中国教育装备展示会即将在重庆开幕,亮点来了!
  5. 求你不要在工作时间刷课了
  6. 上海电力大学研究生计算机技术历年分数线,2019上海电力大学研究生分数线(以及历年复试)...
  7. iOS - OC NSCalendar 日历iOS - OC NSCalendar 日历
  8. Linux多进程(二)
  9. Java自带 JVM监控工具
  10. android21是什么意思,android – 有什么用… -v21.xml?