📜  C++中的std :: next_permutation和prev_permutation

📅  最后修改于: 2021-05-30 20:19:47             🧑  作者: Mango

std :: next_permutation

它用于将[first,last)范围内的元素重新排列到下一个字典上更大的排列。排列是N的每一个!元素可以采用的可能排列方式(其中N是范围内的元素数量)。可以根据它们在字典上的比较方式来排列不同的排列顺序。代码的复杂度为O(n * n!),这还包括打印所有排列。
句法:

template 
bool next_permutation (BidirectionalIterator first,
                       BidirectionalIterator last);
Parameters: 
first, last : Bidirectional iterators to the initial
and 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 lexicographicaly greater permutation.
Otherwise, the function returns false to indicate that 
the arrangementis not greater than the previous, 
but the lowest possible (sorted in ascending order).

应用程序: next_permutation是为给定值数组查找下一个字典序更大的值。
例子:

Input : next permutation of 1 2 3 is 
Output : 1 3 2

Input : next permutation of 4 6 8 is 
Output : 4 8 6
CPP
// C++ program to illustrate
// next_permutation example
 
// this header file contains next_permutation function
#include 
#include 
using namespace std;
 
int main()
{
    int arr[] = { 1, 2, 3 };
 
    sort(arr, arr + 3);
 
    cout << "The 3! possible permutations with 3 elements:\n";
    do {
        cout << arr[0] << " " << arr[1] << " " << arr[2] << "\n";
    } while (next_permutation(arr, arr + 3));
 
    cout << "After loop: " << arr[0] << ' '
         << arr[1] << ' ' << arr[2] << '\n';
 
    return 0;
}


CPP
// C++ program to illustrate
// prev_permutation example
 
// this header file contains prev_permutation function
#include 
 
#include 
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:\n";
    do {
        cout << arr[0] << " " << arr[1] << " " << arr[2] << "\n";
    } while (prev_permutation(arr, arr + 3));
 
    cout << "After loop: " << arr[0] << ' ' << arr[1]
         << ' ' << arr[2] << '\n';
 
    return 0;
}


输出:

The 3! possible permutations with 3 elements:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
After loop: 1 2 3

std :: prev_permutation

它用于将[first,last)范围内的元素重新排列为上一个按字典顺序排列的排列。排列是N的每一个!元素可以采用的可能排列方式(其中N是范围内的元素数量)。可以根据它们在字典上的比较方式来对不同的排列进行排序。
句法 :

template 
bool prev_permutation (BidirectionalIterator first,
                         BidirectionalIterator last );
parameters: 
first, last : Bidirectional iterators to the initial
and 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 lexicographicaly 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_permutation是为给定值数组查找先前的字典编排较小的值。
例子:

Input : prev permutation of 3 2 1 is 
Output : 3 1 2 

Input : prev permutation of 8 6 4 is 
Output :8 4 6

CPP

// C++ program to illustrate
// prev_permutation example
 
// this header file contains prev_permutation function
#include 
 
#include 
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:\n";
    do {
        cout << arr[0] << " " << arr[1] << " " << arr[2] << "\n";
    } while (prev_permutation(arr, arr + 3));
 
    cout << "After loop: " << arr[0] << ' ' << arr[1]
         << ' ' << arr[2] << '\n';
 
    return 0;
}

输出:

The 3! possible permutations with 3 elements:
3 2 1
3 1 2
2 3 1
2 1 3
1 3 2
1 2 3
After loop: 3 2 1
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”