名前空間
変種
操作

std::is_permutation

From cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
制約付きアルゴリズムとRangeアルゴリズム (C++20)
制約付きアルゴリズム、例: ranges::copy, ranges::sort, ...
実行ポリシー (C++17)
シーケンスを変更しない操作
一括操作
(C++17)
検索操作
(C++11)                (C++11)(C++11)

シーケンスを変更する操作
コピー操作
(C++11)
(C++11)
スワップ操作
変換操作
生成操作
削除操作
順序変更操作
(C++17まで)(C++11)
(C++20)(C++20)
サンプリング操作
(C++17)

ソートおよび関連操作
パーティション操作
ソート操作
二分探索操作
(パーティション化された範囲)
集合操作 (ソート済み範囲)
マージ操作 (ソート済み範囲)
ヒープ操作
最小/最大操作
(C++11)
(C++17)
辞書順比較操作
順列操作
is_permutation
(C++11)


Cライブラリ
数値演算
未初期化メモリに対する操作
 
ヘッダー <algorithm> で定義
template< class ForwardIt1, class ForwardIt2 >

bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,

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

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

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

bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,

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

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

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

[first1last1)first2 から始まる範囲の 順列 であるかどうかをチェックします。

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

ForwardIt1ForwardIt2 が異なる 値型 を持つ場合、プログラムは不正です。

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

目次

[編集] パラメーター

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

述語関数のシグネチャは、以下と同等である必要がある。

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

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

型要件
-
ForwardIt1, ForwardIt2LegacyForwardIterator の要件を満たさなければなりません。

[編集] 戻り値

範囲 [first1last1) が範囲 [first2last2) の順列である場合は true、そうでない場合は false

[編集] 計算量

std::distance(first1, last1)N とすると

1) 2 つの範囲が等しい場合、operator== を使用した比較は正確に N 回行われます。そうでない場合、最悪ケースでは O(N2
)
回の比較が行われます。
2) 2 つの範囲が等しい場合、述語 p の適用は正確に N 回行われます。そうでない場合、最悪ケースでは O(N2
)
回の適用が行われます。
3,4) ForwardIt1ForwardIt2 の両方が LegacyRandomAccessIterator であり、かつ last1 - first1 != last2 - first2true の場合、比較は行われません。
そうでない場合
3) 2 つの範囲が等しい場合、operator== を使用した比較は正確に N 回行われます。そうでない場合、最悪ケースでは O(N2
)
回の比較が行われます。
4) 2 つの範囲が等しい場合、述語 p の適用は正確に N 回行われます。そうでない場合、最悪ケースでは O(N2
)
回の適用が行われます。

[編集] 可能な実装

template<class ForwardIt1, class ForwardIt2>
bool is_permutation(ForwardIt1 first, ForwardIt1 last,
                    ForwardIt2 d_first)
{
    // skip common prefix
    std::tie(first, d_first) = std::mismatch(first, last, d_first);
 
    // iterate over the rest, counting how many times each element
    // from [first, last) appears in [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; // this *i has been checked
 
            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が同値関係を課すことを規定する
(コンセプト) [編集]
あるシーケンスが別のシーケンスの順列であるかを判断する
(アルゴリズム関数オブジェクト)[編集]
English 日本語 中文(简体) 中文(繁體)