當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ Algorithm prev_permutation()用法及代碼示例

C++ 算法 prev_permutation() 函數用於將 [first, last) 範圍內的元素重新排序為之前的字典序排列。

排列被指定為可以對一組或多個事物進行排序或排列的幾種可能方式中的每一種。它表示為N!其中 N = 範圍內的元素數。

對於第一個版本使用運算符 < 比較元素,或者對於第二個版本使用給定的二進製比較函數 comp 比較元素。

用法

default (1)   template <class BidirectionalIterator>
                       bool prev_permutation (BidirectionalIterator first,
                                                                           BidirectionalIterator last );

custom (2)   template <class BidirectionalIterator, class Compare>
                       bool prev_permutation (BidirectionalIterator first,
                                                       BidirectionalIterator last, Compare comp);

參數

first: 一個雙向迭代器,指向要排列的範圍內的第一個元素。

last:一個雙向迭代器,指向要排列的範圍中最後一個的位置。

comp: 一個用戶定義的二元謂詞函數,它接受兩個參數,如果兩個參數按順序返回真,否則返回假。它遵循嚴格的弱排序來對元素進行排序。

返回值

如果函數可以將對象重新排序為字典序較小的排列,則返回 true。

否則,該函數返回 false 以指示排列不小於前一個,而是可能的最大(按降序排序)。

複雜度

複雜性在第一個和最後一個之間的距離的一半內達到線性。

數據競爭

範圍 [first, last) 中的對象被修改。

異常

如果元素交換或迭代器上的操作引發異常,則此函數將引發異常。

注意:無效的參數會導致未定義的行為。

例子1

讓我們看一個簡單的例子來演示 prev_permutation() 的使用:

#include <algorithm>
#include <string>
#include <iostream>
#include <functional>

using namespace std;

int main()
{
    string s="abc";
    sort(s.begin(), s.end(), greater<char>());
    do {
        cout << s << ' ';
    } while(prev_permutation(s.begin(), s.end()));
    cout << '\n';
    
    return 0;
}

輸出:

cba cab bca bac acb abc

例子2

讓我們看另一個簡單的例子:

#include <iostream>     // std::cout
#include <algorithm>    // std::next_permutation, std::sort, std::reverse

using namespace std;

int main () {
  int myints[] = {1,2,3};

  sort (myints,myints+3);
  reverse (myints,myints+3);

  cout << "The 3! possible permutations with 3 elements:\n";
  do {
    cout << myints[0] << ' ' << myints[1] << ' ' << myints[2] << '\n';
  } while ( prev_permutation(myints,myints+3) );

  cout << "After loop:" << myints[0] << ' ' << myints[1] << ' ' << myints[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

例子3

讓我們看另一個簡單的例子來演示使用默認版本的 prev_permutation 的使用:

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
    string s;

    cout << "Enter the String:";
    cin >> s;
    sort(s.rbegin(), s.rend());
    cout << "Permutations of " << s << endl;
    do
    {
        cout << s << "\t";
    }
    while (prev_permutation(s.begin(), s.end()));
    return 0;
}

輸出:

Enter the String:NIK
Permutations of NKI
NKI	NIK	KNI	KIN	INK	IKN

示例 4

讓我們看一個簡單的例子來演示使用比較函數的 prev_permutation() 的使用:

#include <iostream>
#include <algorithm>
using namespace std;
// Function to rearrange the specified string as lexicographically smaller
// permutation. It returns false if the string cannot be rearranged as
// lexicographically smaller permutation, else it returns true
bool prevPermutation(string &s, int n)
{
    // Find largest index i such that s[i-1] is more than s[i]
    int i = n-1;
    while (i > 0 && s[i-1] <= s[i])
    {
        // Return false if i is at first index of the string
        // This means we're already at lowest possible permutation
        if (--i == 0)
            return false;
    }

    // s[i..n-1] is now sorted in natural order

    // Find highest index j such that j >= i and s[j] < s[i-1]
    int j = i;
    while (j < n && s[j] <= s[i-1])
        j++;

    j--;

    // Swap characters at index i-1 with index j
    swap(s[i-1], s[j]);

    // Reverse the substring s[i..n-1] and return true
    reverse (s.begin() + i, s.end());

    return true;
}

// Program to find lexicographically smaller permutations of a string
int main()
{
    string s = "231";

    // std::sort(rbegin(s), rend(s));

    // find all lexicographically smaller permutations using
    // std::prev_permutation
    while (1)
    {
        // print current permutation
        cout << s << " ";

        // find previous permutation in lexicographic order
        if (!prevPermutation(s, s.length()))
            break;
    }

    return 0;
}

輸出:

231 213 132 123





相關用法


注:本文由純淨天空篩選整理自 C++ Algorithm prev_permutation ()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。