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. “cvSnakeImage”: 找不到标识符
  2. C语言数组+冒泡排序
  3. SwiftUI3优秀文章List 去掉左右间距问题
  4. 如何判断链表有环并计算环的长度
  5. Java 这一年:IntelliJ 称霸 IDE,Kotlin 成最大赢家!
  6. Java图像识别车牌_车牌识别测试图片集(237幅车牌照片)(文件名均是车牌号)
  7. mysql下载 补丁_mysql 官方补丁在哪里下载?
  8. Element UI 中国省市区级联数据js
  9. 【问题解决】QT报错 undefined reference to `__imp__ZN11QSerialPortD1Ev‘
  10. CTFshow 2022 菜狗杯 WEB WP补充
  11. windows 8 修改窗口颜色为淡绿色
  12. uni-app小程序 阿里巴巴字体图标兼容ios
  13. 中国移动GPRS概况
  14. 多渠道归因分析(Attribution):传统归因(一)
  15. mysql relay log是什么意思_MySQL--binlog和relay log的生成和删除
  16. AnyLogic第二讲行人仿真空间逻辑讲解
  17. c语言输入float就报错,c语言 输入float类型 出错处理
  18. 数据结构与算法-打印等腰三角形算法
  19. 彻底关闭ACDSEE 弹出的注册ID对话框
  20. 机器人削面机的种类和价格_刀削面机器人价格与人工相比一年低一半

热门文章

  1. Android开源项目分类汇总--转载
  2. TXT阅读器 C# winfrom 开发
  3. Oracle 导出以XX开头的表结构及表数据并导入其他用户
  4. python 获取浏览器安装位置,并使用指定浏览器打开指定网页
  5. 女神节表白:因一个分号被拒!
  6. 华为鸿蒙如何内测,第一批华为鸿蒙内测机型出炉!有你用的手机吗?
  7. 你好,区块链操作系统上的完全去中心化国际象棋
  8. WIN10下Visual Studio 2012的安装
  9. 在word文档中如何输入复杂的公式,和特殊符号
  10. ubuntu20.04上openvino安装及环境配置