Namespaces
Variants

std:: is_permutation

From cppreference.net
Algorithm library
Constrained algorithms and algorithms on ranges (C++20)
Constrained algorithms, e.g. ranges::copy , ranges::sort , ...
Execution policies (C++17)
Non-modifying sequence operations
Batch operations
(C++17)
Search operations
Modifying sequence operations
Copy operations
(C++11)
(C++11)
Swap operations
Transformation operations
Generation operations
Removing operations
Order-changing operations
(until C++17) (C++11)
(C++20) (C++20)
Sampling operations
(C++17)

Sorting and related operations
Partitioning operations
Sorting operations
Binary search operations
(on partitioned ranges)
Set operations (on sorted ranges)
Merge operations (on sorted ranges)
Heap operations
Minimum/maximum operations
Lexicographical comparison operations
Permutation operations
is_permutation
(C++11)


C library
Numeric operations
Operations on uninitialized memory
定義先ヘッダ <algorithm>
template < class ForwardIt1, class ForwardIt2 >

bool is_permutation ( ForwardIt1 first1, ForwardIt1 last1,

ForwardIt2 first2 ) ;
(1) (C++11以降)
(constexpr C++20以降)
template < class ForwardIt1, class ForwardIt2,

class BinaryPredicate >
bool is_permutation ( ForwardIt1 first1, ForwardIt1 last1,

ForwardIt2 first2, BinaryPredicate p ) ;
(2) (C++11以降)
(constexpr C++20以降)
template < class ForwardIt1, class ForwardIt2 >

bool is_permutation ( ForwardIt1 first1, ForwardIt1 last1,

ForwardIt2 first2, ForwardIt2 last2 ) ;
(3) (C++14以降)
(constexpr C++20以降)
template < class ForwardIt1, class ForwardIt2,

class BinaryPredicate >
bool is_permutation ( ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2,

BinaryPredicate p ) ;
(4) (C++14以降)
(constexpr C++20以降)

[ first1 , last1 ) permutation であるかどうかをチェックします。これは first2 から始まる範囲の順列かどうかを判定します:

  • オーバーロード (1,2) の場合、第二範囲は std:: distance ( first1, last1 ) 個の要素を持ちます。
  • オーバーロード (3,4) の場合、第二範囲は [ first2 , last2 ) です。
1,3) 要素は operator == を使用して比較されます。
2,4) 要素は指定された二項述語 p を使用して比較されます。

ForwardIt1 ForwardIt2 が異なる value types を持つ場合、プログラムは不適格となります。

比較関数が 同値関係 でない場合、動作は未定義です。

目次

翻訳の説明: - 「Contents」を「目次」に翻訳しました - その他のテキスト(Parameters、Return value、Complexityなど)はC++関連の専門用語として翻訳せず、原文のまま保持しました - HTMLタグ、属性、クラス名などはすべて変更せず保持しました - 番号部分もそのまま保持しました

パラメータ

first1, last1 - 比較する最初の要素の範囲を定義するイテレータのペア
first2, last2 - 比較する2番目の要素の範囲を定義するイテレータのペア
p - 要素が等しいと扱われるべき場合に​ true を返す二項述語。

述語関数のシグネチャは以下と同等であるべき:

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

シグネチャが const & を持つ必要はないが、関数は渡されたオブジェクトを変更してはならず、 値カテゴリ に関係なく(したがって、 Type1 & は許可されない 、また Type1 Type1 に対してムーブがコピーと等価でない限り許可されない (C++11以降) )すべての Type1 Type2 型の値を受け入れられなければならない。
Type1 Type2 は、 InputIt1 InputIt2 型のオブジェクトが間接参照可能であり、それぞれ Type1 Type2 に暗黙的に変換可能でなければならない。 ​

型要件
-
ForwardIt1, ForwardIt2 LegacyForwardIterator の要件を満たさなければならない。

戻り値

true 範囲 [ first1 , last1 ) が範囲 [ first2 , last2 ) の順列である場合、 false それ以外の場合。

計算量

与えられた N std:: distance ( first1, last1 ) として:

1) 2つの範囲が等しい場合、正確に N 回の比較( operator == を使用)が行われ、それ以外の場合、最悪ケースでは O(N 2
)
回の比較が行われます。
2) 厳密に N 回の述語 p の適用(両範囲が等しい場合)、それ以外の場合は最悪ケースで O(N 2
)
回の適用。
3,4) もし ForwardIt1 ForwardIt2 が両方とも LegacyRandomAccessIterator であり、かつ last1 - first1 ! = last2 - first2 true の場合、比較は行われません。
それ以外の場合:
3) 正確に N 回の比較( operator == を使用)が、2つの範囲が等しい場合に実行されます。それ以外の場合、最悪ケースでは O(N 2
)
回の比較が実行されます。
4) 厳密に N 回の述語 p の適用が、二つの範囲が等しい場合に行われ、それ以外の場合には最悪ケースで O(N 2
)
回の適用が行われる。

実装例

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

関連項目

要素範囲の次に大きい辞書順の順列を生成する
(関数テンプレート)
要素範囲の次に小さい辞書順の順列を生成する
(関数テンプレート)
ある relation が同値関係を課すことを指定する
(コンセプト)
あるシーケンスが別のシーケンスの順列であるかどうかを判定する
(アルゴリズム関数オブジェクト)