std:: is_permutation
|
定義先ヘッダ
<algorithm>
|
||
|
template
<
class
ForwardIt1,
class
ForwardIt2
>
bool
is_permutation
(
ForwardIt1 first1, ForwardIt1 last1,
|
(1) |
(C++11以降)
(constexpr C++20以降) |
|
template
<
class
ForwardIt1,
class
ForwardIt2,
class
BinaryPredicate
>
|
(2) |
(C++11以降)
(constexpr C++20以降) |
|
template
<
class
ForwardIt1,
class
ForwardIt2
>
bool
is_permutation
(
ForwardIt1 first1, ForwardIt1 last1,
|
(3) |
(C++14以降)
(constexpr C++20以降) |
|
template
<
class
ForwardIt1,
class
ForwardIt2,
class
BinaryPredicate
>
|
(4) |
(C++14以降)
(constexpr C++20以降) |
[
first1
,
last1
)
が
permutation
であるかどうかをチェックします。これは
first2
から始まる範囲の順列かどうかを判定します:
- オーバーロード (1,2) の場合、第二範囲は std:: distance ( first1, last1 ) 個の要素を持ちます。
-
オーバーロード
(3,4)
の場合、第二範囲は
[first2,last2)です。
ForwardIt1
と
ForwardIt2
が異なる
value types
を持つ場合、プログラムは不適格となります。
比較関数が 同値関係 でない場合、動作は未定義です。
目次 |
パラメータ
| first1, last1 | - | 比較する最初の要素の範囲を定義するイテレータのペア |
| first2, last2 | - | 比較する2番目の要素の範囲を定義するイテレータのペア |
| p | - |
要素が等しいと扱われるべき場合に
true
を返す二項述語。
述語関数のシグネチャは以下と同等であるべき: bool pred ( const Type1 & a, const Type2 & b ) ;
シグネチャが
const
&
を持つ必要はないが、関数は渡されたオブジェクトを変更してはならず、
値カテゴリ
に関係なく(したがって、
Type1
&
は許可されない
、また
Type1
|
| 型要件 | ||
-
ForwardIt1, ForwardIt2
は
LegacyForwardIterator
の要件を満たさなければならない。
|
||
戻り値
true
範囲
[
first1
,
last1
)
が範囲
[
first2
,
last2
)
の順列である場合、
false
それ以外の場合。
計算量
与えられた N を std:: distance ( first1, last1 ) として:
) 回の比較が行われます。
) 回の適用。
ForwardIt1
と
ForwardIt2
が両方とも
LegacyRandomAccessIterator
であり、かつ
last1
-
first1
!
=
last2
-
first2
が
true
の場合、比較は行われません。
) 回の比較が実行されます。
) 回の適用が行われる。
実装例
template<class ForwardIt1, class ForwardIt2> bool is_permutation(ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first) { // 共通プレフィックスをスキップ std::tie(first, d_first) = std::mismatch(first, last, d_first); // 残りの要素を反復処理し、[first, last) の各要素が // [d_first, d_last) に何回出現するかをカウント if (first != last) { ForwardIt2 d_last = std::next(d_first, std::distance(first, last)); for (ForwardIt1 i = first; i != last; ++i) { if (i != std::find(first, i, *i)) continue; // この *i は既にチェック済み auto m = std::count(d_first, d_last, *i); if (m == 0 || std::count(i, last, *i) != m) return false; } } return true; } |
注記
std::is_permutation
は、並べ替えアルゴリズム(例:ソート、シャッフル、パーティショニング)の正確性を検証するために
テスト
で使用できます。
x
が元の範囲で
y
が
並べ替えられた
範囲の場合、
std
::
is_permutation
(
x, y
)
==
true
は、
y
が
「同じ」
要素で構成されている(異なる位置にある可能性がある)ことを意味します。
例
#include <algorithm> #include <iostream> template<typename Os, typename V> Os& operator<<(Os& os, const V& v) { os << "{ "; for (const auto& e : v) os << e << ' '; return os << '}'; } int main() { static constexpr auto v1 = {1, 2, 3, 4, 5}; static constexpr auto v2 = {3, 5, 4, 1, 2}; static constexpr auto v3 = {3, 5, 4, 1, 1}; std::cout << v2 << " is a permutation of " << v1 << ": " << std::boolalpha << std::is_permutation(v1.begin(), v1.end(), v2.begin()) << '\n' << v3 << " is a permutation of " << v1 << ": " << std::is_permutation(v1.begin(), v1.end(), v3.begin()) << '\n'; }
出力:
{ 3 5 4 1 2 } is a permutation of { 1 2 3 4 5 }: true
{ 3 5 4 1 1 } is a permutation of { 1 2 3 4 5 }: false
関連項目
|
要素範囲の次に大きい辞書順の順列を生成する
(関数テンプレート) |
|
|
要素範囲の次に小さい辞書順の順列を生成する
(関数テンプレート) |
|
|
(C++20)
|
ある
relation
が同値関係を課すことを指定する
(コンセプト) |
|
(C++20)
|
あるシーケンスが別のシーケンスの順列であるかどうかを判定する
(アルゴリズム関数オブジェクト) |