码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • STL—next_permutation函数


    目录

    1.next_permutation函数的定义

    2.简单使用

    2.1普通数组全排列

     2.2结构体全排列

    2.3string

    3.补充


    1.next_permutation函数的定义

    next_permutation函数会按照字母表顺序生成给定序列的下一个较大的排列,直到整个序列为降序为止。与其相对的还有一个函数——prev_permutation函数。

    next_permutaion(起始地址,末尾地址+1)
    next_permutaion(起始地址,末尾地址+1,自定义排序)

    注:next_permutation只能获得上一个排列,如果要获得全排列,那么就需要先对数组进行升序排序

    2.简单使用

    2.1普通数组全排列

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include
    3. #include
    4. using namespace std;
    5. int main()
    6. {
    7. int arr[4] = { 1, 2, 3, 4 };
    8. do {
    9. for (int i = 0; i < 4; i++)
    10. {
    11. cout << arr[i] << " ";
    12. }
    13. cout << endl;
    14. } while (next_permutation(arr, arr + 4));
    15. return 0;
    16. }

     运行结果:

    1. 1 2 3 4
    2. 1 2 4 3
    3. 1 3 2 4
    4. 1 3 4 2
    5. 1 4 2 3
    6. 1 4 3 2
    7. 2 1 3 4
    8. 2 1 4 3
    9. 2 3 1 4
    10. 2 3 4 1
    11. 2 4 1 3
    12. 2 4 3 1
    13. 3 1 2 4
    14. 3 1 4 2
    15. 3 2 1 4
    16. 3 2 4 1
    17. 3 4 1 2
    18. 3 4 2 1
    19. 4 1 2 3
    20. 4 1 3 2
    21. 4 2 1 3
    22. 4 2 3 1
    23. 4 3 1 2
    24. 4 3 2 1

     2.2结构体全排列

    由于结构体默认不能比较大小,所以就不能使用默认的next_permutation()排列比较函数,需要使用自定义排列比较函数。

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include
    3. #include
    4. using namespace std;
    5. typedef struct
    6. {
    7. int test;
    8. bool operator < (const fyd& a)
    9. {
    10. return test < a.test;
    11. }
    12. }fyd;
    13. fyd arr[4];
    14. int main()
    15. {
    16. arr[0].test = 2;
    17. arr[1].test = 1;
    18. arr[2].test = 4;
    19. arr[3].test = 3;
    20. do {
    21. for (int i = 0; i < 4; i++)
    22. {
    23. cout << arr[i].test << " ";
    24. }
    25. cout << endl;
    26. } while (next_permutation(arr, arr + 4));
    27. return 0;
    28. }

    运行结果:

    1. 1 2 3 4
    2. 1 2 4 3
    3. 1 3 2 4
    4. 1 3 4 2
    5. 1 4 2 3
    6. 1 4 3 2
    7. 2 1 3 4
    8. 2 1 4 3
    9. 2 3 1 4
    10. 2 3 4 1
    11. 2 4 1 3
    12. 2 4 3 1
    13. 3 1 2 4
    14. 3 1 4 2
    15. 3 2 1 4
    16. 3 2 4 1
    17. 3 4 1 2
    18. 3 4 2 1
    19. 4 1 2 3
    20. 4 1 3 2
    21. 4 2 1 3
    22. 4 2 3 1
    23. 4 3 1 2
    24. 4 3 2 1

    2.3string

    string等数据结构不能直接用名字代表地址,只能够使用自带的迭代器begin()、end()实现全排列。

    1. #define _CRT_SECURE_NO_WARNINGS 1
    2. #include
    3. #include
    4. using namespace std;
    5. int main()
    6. {
    7. string s;
    8. cin >> s;
    9. do{
    10. cout << s << endl;
    11. }while (next_permutation(s.begin(), s.end()));
    12. return 0;
    13. }

    运行结果:

    1. abc //input
    2. abc
    3. acb
    4. bac
    5. bca
    6. cab
    7. cba

    3.补充

    推荐大家使用:cplusplus.com - The C++ Resources Network

    可以查询到对应函数对应的头文件、底层代码及使用方式等。

    例如:

     剩下的就不多说啦!自己探索叭!

  • 相关阅读:
    【笔记】Sturctured Streaming笔记总结(Python版)
    【数学建模学习笔记【集训十天】之第三天】
    基于Dockerfile创建镜像
    【计算机网络】网络原理
    CentOS 升级 OpenSSL 至最新版教程
    框架内容整理
    《JavaScript悟道》——阅读笔记
    深入学习JVM(Java虚拟机)
    基础ArkTS组件:二维码,滚动条与滑动条,多选框与多选框群组(HarmonyOS学习第三课【3.4】)
    c++内存对齐
  • 原文地址:https://blog.csdn.net/weixin_74809706/article/details/134432617
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号