The Wayback Machine - https://web.archive.org/web/20161016170856/http://ja.cppreference.com:80/w/cpp/algorithm/prev_permutation
名前空間
変種
操作

std::prev_permutation

提供: cppreference.com
< cpp‎ | algorithm

 
 
アルゴリズムライブラリ
機能します
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
シーケンス動作を非改変
Original:
Non-modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
シーケンス動作を変更する
Original:
Modifying sequence operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
操作を仕切る
Original:
Partitioning operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(ソートされた範囲で)ソート操作
Original:
Sorting operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
バイナリ検索操作(ソート範囲で)
Original:
Binary search operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
(ソートされた範囲で)操作を設定します
Original:
Set operations (on sorted ranges)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
ヒープ操作
Original:
Heap operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
最小値/最大値操作
Original:
Minimum/maximum operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
数値演算
Original:
Numeric operations
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Cライブラリ
Original:
C library
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
 
Defined in header <algorithm>
template<class BidirIt >
bool prev_permutation( BidirIt first, BidirIt last);
(1)
template<class BidirIt, class Compare >
bool prev_permutation( BidirIt first, BidirIt last, Compare comp);
(2)
辞書的に[first, last)またはoperator<に対して順序付けされているすべての順列の集合から以前順列に範囲compを変換します。返しtrue場合など順列が存在すると、そうでなければ最後の順列に範囲を変換します(std::sort(first, last); std::reverse(first, last);であれば)を返しfalse.
Original:
Transforms the range [first, last) into the previous permutation from the set of all permutations that are lexicographically ordered with respect to operator< or comp. Returns true if such permutation exists, otherwise transforms the range into the last permutation (as if by std::sort(first, last); std::reverse(first, last);) and returns false.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

目次

[編集]パラメータ

first, last -
ランダムな置換をする要素の範囲
Original:
the range of elements to permute
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
comp - 比較関数. 最初の値が二つ目の値より小さい 場合、 ​trueを返します.

比較関数のシグネチャは以下と同等でなければなりません.

 bool cmp(const Type1 &a, const Type2 &b);

シグネチャはconstを含まなくても構いませんが, 比較関数は渡されたオブジェクトを変更してはなりません.
The types Type1 and Type2 must be such that an object of type BidirIt can be dereferenced and then implicitly converted to both of them. ​

型の要件
-
BidirItValueSwappable and BidirectionalIterator

[編集]値を返します

新しい順列が辞書式順序で古いの前true場合。 false第一順列に達していて、範囲は最後の順列にリセットされていた場合..
Original:
true if the new permutation precedes the old in lexicographical order. false if the first permutation was reached and the range was reset to the last permutation.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]複雑性

最も(last-first)/2スワップで.
Original:
At most (last-first)/2 swaps.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[編集]可能な実装

template<class BidirIt>bool prev_permutation(BidirIt first, BidirIt last){if(first == last)returnfalse; BidirIt i = last;if(first ==--i)returnfalse;   while(1){ BidirIt i1, i2;   i1 = i;if(*i1 <*--i){ i2 = last;while(!(*--i2 <*i));std::iter_swap(i, i2);std::reverse(i1, last);returntrue;}if(i == first){std::reverse(first, last);returnfalse;}}}

[編集]

次のコードは、文字列を逆の順序で "abc"のすべての6つの順列を出力します
Original:
The following code prints all six permutations of the string "abc" in reverse order
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

#include <algorithm>#include <string>#include <iostream>#include <functional>int main(){std::string s="abc";std::sort(s.begin(), s.end(), std::greater<char>());do{std::cout<< s <<' ';}while(std::prev_permutation(s.begin(), s.end()));std::cout<<'\n';}

出力:

cba cab bca bac acb abc

[編集]参照

あるシーケンスが別のシーケンスの順列並び替えになっているか検査します
(関数テンプレート)[edit]
prev_permutation
generates the next smaller lexicographic permutation of a range of elements
(関数テンプレート)[edit]
close