【C++航海王:追寻罗杰的编程之路】STL—next_permutation函数

简介: 【C++航海王:追寻罗杰的编程之路】STL—next_permutation函数

1.next_permutation函数的定义

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

next_permutaion(起始地址,末尾地址+1)

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

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

2.简单使用

2.1普通数组全排列

#define _CRT_SECURE_NO_WARNINGS 1
 
#include <iostream>
#include <algorithm>
using namespace std;
 
int main()
{
  int arr[4] = { 1, 2, 3, 4 };
 
  do {
    for (int i = 0; i < 4; i++)
    {
      cout << arr[i] << " ";
    }
    cout << endl;
  } while (next_permutation(arr, arr + 4));
 
 
  return 0;
}

运行结果:

1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1

2.2结构体全排列

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

#define _CRT_SECURE_NO_WARNINGS 1
 
#include <iostream>
#include <algorithm>
using namespace std;
 
typedef struct
{
  int test;
 
  bool operator < (const fyd& a)
  {
    return test < a.test;
  }
 
}fyd;
 
fyd arr[4];
 
int main()
{
  arr[0].test = 2;
  arr[1].test = 1;
  arr[2].test = 4;
  arr[3].test = 3;
 
  do {
    for (int i = 0; i < 4; i++)
    {
      cout << arr[i].test << " ";
    }
    cout << endl;
  } while (next_permutation(arr, arr + 4));
 
  return 0;
}

运行结果:

1 2 3 4
1 2 4 3
1 3 2 4
1 3 4 2
1 4 2 3
1 4 3 2
2 1 3 4
2 1 4 3
2 3 1 4
2 3 4 1
2 4 1 3
2 4 3 1
3 1 2 4
3 1 4 2
3 2 1 4
3 2 4 1
3 4 1 2
3 4 2 1
4 1 2 3
4 1 3 2
4 2 1 3
4 2 3 1
4 3 1 2
4 3 2 1

2.3string

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

#define _CRT_SECURE_NO_WARNINGS 1
 
 
#include <iostream>
#include <algorithm>
using namespace std;
 
int main()
{
    string s;
    cin >> s;
    do{
        cout << s << endl;
    }while (next_permutation(s.begin(), s.end()));
    
    return 0;
}

运行结果:

abc //input
 
abc
acb
bac
bca
cab
cba

3.补充

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

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

例如:

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


目录
相关文章
|
2天前
|
编译器 C++ 开发者
C++一分钟之-C++20新特性:模块化编程
【6月更文挑战第27天】C++20引入模块化编程,缓解`#include`带来的编译时间长和头文件管理难题。模块由接口(`.cppm`)和实现(`.cpp`)组成,使用`import`导入。常见问题包括兼容性、设计不当、暴露私有细节和编译器支持。避免这些问题需分阶段迁移、合理设计、明确接口和关注编译器更新。示例展示了模块定义和使用,提升代码组织和维护性。随着编译器支持加强,模块化将成为C++标准的关键特性。
16 3
|
3天前
|
存储 C++
【C++航海王:追寻罗杰的编程之路】一篇文章带你了解二叉搜索树
【C++航海王:追寻罗杰的编程之路】一篇文章带你了解二叉搜索树
9 1
|
3天前
|
存储 自然语言处理 C++
【C++航海王:追寻罗杰的编程之路】set|map|multiset|multimap简单介绍
【C++航海王:追寻罗杰的编程之路】set|map|multiset|multimap简单介绍
11 0
【C++航海王:追寻罗杰的编程之路】set|map|multiset|multimap简单介绍
|
22小时前
|
程序员 编译器 C++
探索C++语言宝库:解锁基础知识与实用技能(类型变量+条件循环+函数模块+OOP+异常处理)
探索C++语言宝库:解锁基础知识与实用技能(类型变量+条件循环+函数模块+OOP+异常处理)
4 0
|
2天前
|
C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
7 0
|
2天前
|
存储 C++ 容器
C++ STL标准库 《map容器详解》
C++ STL标准库 《map容器详解》
7 0
|
2天前
|
缓存 C++
详细解读C++常用库函数C函数库cstdio
详细解读C++常用库函数C函数库cstdio
|
2天前
详细解读C++char类型函数
详细解读C++char类型函数
|
3天前
|
算法 搜索推荐 C++
C++之STL常用算法(遍历、查找、排序、拷贝、替换、算数生成、集合)
C++之STL常用算法(遍历、查找、排序、拷贝、替换、算数生成、集合)
12 0
|
3天前
|
C++
C++函数对象(仿函数)
C++函数对象(仿函数)
7 0