STD::C++中的NExtx置换和Pype置换

std::下一个排列

null

它用于将范围内的元素[第一、最后一个]重新排列成下一个按字典顺序排列的更大排列。一个排列是元素可以采取的N!种可能的排列方式中的每一种(其中N是范围内的元素数)。不同的排列可以根据它们按字典顺序相互比较的方式排序。代码的复杂度为O(N*N!)还包括打印所有排列。

语法:

template bool next_permutation (BidirectionalIterator first,                       BidirectionalIterator last);Parameters: first, last : Bidirectional iterators to the initialand final positions of the sequence. The range used is [first, last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.return value: true : if the function could rearrange the object as a lexicographically greater permutation.Otherwise, the function returns false to indicate that the arrangement is not greater than the previous, but the lowest possible (sorted in ascending order).

申请: 下一个排列是为给定的值数组找到下一个按字典顺序排列的更大值。

例如:

Input : next permutation of 1 2 3 is Output : 1 3 2Input : next permutation of 4 6 8 is Output : 4 8 6

C++

// C++ program to illustrate
// next_permutation example
// this header file contains next_permutation function
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 1, 2, 3 };
sort(arr, arr + 3);
cout << "The 3! possible permutations with 3 elements:" ;
do {
cout << arr[0] << " " << arr[1] << " " << arr[2] << "" ;
} while (next_permutation(arr, arr + 3));
cout << "After loop: " << arr[0] << ' '
<< arr[1] << ' ' << arr[2] << '' ;
return 0;
}


输出:

The 3! possible permutations with 3 elements:1 2 31 3 22 1 32 3 13 1 23 2 1After loop: 1 2 3

std::prev_置换

它用于将[first,last]范围内的元素重新排列为之前的按字典顺序排列的排列。排列是元素可以采取的N!种可能的排列中的每一种(其中N是范围内元素的数量)。不同的排列可以根据它们如何按字典顺序相互比较而排序。

语法:

template bool prev_permutation (BidirectionalIterator first,                         BidirectionalIterator last );parameters: first, last : Bidirectional iterators to the initialand final positions of the sequence. The range used is [first, last), which contains all theelements between first and last, including the element pointed by first but not the elementpointed by last.return value: true : if the function could rearrange the object as a lexicographically smaller permutation.Otherwise, the function returns false to indicate that the arrangement is not less than the previous, but the largest possible (sorted in descending order).

申请: prev_置换是为给定的值数组查找以前的按字典顺序排列的较小值。

例如:

Input : prev permutation of 3 2 1 is Output : 3 1 2 Input : prev permutation of 8 6 4 is Output :8 4 6

C++

// C++ program to illustrate
// prev_permutation example
// this header file contains prev_permutation function
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 1, 2, 3 };
sort(arr, arr + 3);
reverse(arr, arr + 3);
cout << "The 3! possible permutations with 3 elements:" ;
do {
cout << arr[0] << " " << arr[1] << " " << arr[2] << "" ;
} while (prev_permutation(arr, arr + 3));
cout << "After loop: " << arr[0] << ' ' << arr[1]
<< ' ' << arr[2] << '' ;
return 0;
}


输出:

The 3! possible permutations with 3 elements:3 2 13 1 22 3 12 1 31 3 21 2 3After loop: 3 2 1

© 版权声明
THE END
喜欢就支持一下吧,技术咨询可以联系QQ407933975
点赞11 分享