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

std::is_permutation

提供: cppreference.com
< cpp‎ | algorithm
 
 
アルゴリズムライブラリ
実行ポリシー (C++17)
非変更シーケンス操作
(C++11)(C++11)(C++11)
(C++17)
変更シーケンス操作
未初期化記憶域の操作
分割操作
ソート操作
バイナリサーチ操作
集合操作 (ソート済み範囲用)
ヒープ操作
(C++11)
最小/最大演算
(C++11)
(C++17)
順列
is_permutation
(C++11)
数値演算
C のライブラリ
 
ヘッダ <algorithm> で定義
(1)
template<class ForwardIt1, class ForwardIt2 >

bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,

                     ForwardIt2 first2 );
(C++11およびそれ以降)
(C++20以前)
template<class ForwardIt1, class ForwardIt2 >

constexprbool is_permutation( ForwardIt1 first1, ForwardIt1 last1,

                               ForwardIt2 first2 );
(C++20およびそれ以降)
(2)
template<class ForwardIt1, class ForwardIt2, class BinaryPredicate >

bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,

                     ForwardIt2 first2, BinaryPredicate p );
(C++11およびそれ以降)
(C++20以前)
template<class ForwardIt1, class ForwardIt2, class BinaryPredicate >

constexprbool is_permutation( ForwardIt1 first1, ForwardIt1 last1,

                               ForwardIt2 first2, BinaryPredicate p );
(C++20およびそれ以降)
(3)
template<class ForwardIt1, class ForwardIt2 >

bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,

                     ForwardIt2 first2, ForwardIt2 last2 );
(C++14およびそれ以降)
(C++20以前)
template<class ForwardIt1, class ForwardIt2 >

constexprbool is_permutation( ForwardIt1 first1, ForwardIt1 last1,

                               ForwardIt2 first2, ForwardIt2 last2 );
(C++20およびそれ以降)
(4)
template<class ForwardIt1, class ForwardIt2, class BinaryPredicate >

bool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
                     ForwardIt2 first2, ForwardIt2 last2,

                     BinaryPredicate p );
(C++14およびそれ以降)
(C++20以前)
template<class ForwardIt1, class ForwardIt2, class BinaryPredicate >

constexprbool is_permutation( ForwardIt1 first1, ForwardIt1 last1,
                               ForwardIt2 first2, ForwardIt2 last2,

                               BinaryPredicate p );
(C++20およびそれ以降)

範囲 [first2,last2) と等しい範囲を作る範囲 [first1, last1) 内の要素の順列が存在するならば true を返します。 last2 が指定されない場合は first2 +(last1 - first1) になります。

1,3) 要素は operator== を用いて比較されます。 比較が同値関係を持たない場合、動作は未定義です。
2,4) 要素は指定された二項述語 p を用いて比較されます。 比較が同値関係を持たない場合、動作は未定義です。

目次

[編集]引数

first1, last1 - 比較する要素の範囲
first2, last2 - 2つめの比較する範囲
p - 要素が等しいとみなされるべき場合に true を返す二項述語。

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

 bool pred(const Type &a, const Type &b);

TypeForwardIt1ForwardIt2 の両方の値型であるべきです。 シグネチャが const& を持つ必要はありませんが、関数は渡されたオブジェクトを変更してはなりません。 ​

型の要件
-
ForwardIt1, ForwardIt2ForwardIterator の要件を満たさなければなりません。
-
ForwardIt1, ForwardIt2 は同じ値型を持たなければなりません。

[編集]戻り値

範囲 [first1, last1) が範囲 [first2, last2) の順列であれば true

[編集]計算量

多くとも O(N2) 回の述語の適用、またはシーケンスがすでに等しい場合はちょうど N 回。 ただし N=std::distance(first1, last1) です。

しかし、 ForwardIt1 および ForwardIt2RandomAccessIterator の要件を満たし、 std::distance(first1, last1)!=std::distance(first2, last2) である場合、述語の適用は行われません。

[編集]実装例

template<class ForwardIt1, class ForwardIt2>bool is_permutation(ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first){// skip common prefixstd::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 = d_first;std::advance(d_last, std::distance(first, last));for(ForwardIt1 i = first; i != last;++i){if(i !=std::find(first, i, *i))continue;// already counted this *i   auto m =std::count(d_first, d_last, *i);if(m==0||std::count(i, last, *i)!= m){returnfalse;}}}returntrue;}

[編集]

#include <algorithm>#include <vector>#include <iostream>int main(){std::vector<int> v1{1,2,3,4,5};std::vector<int> v2{3,5,4,1,2};std::cout<<"3,5,4,1,2 is a permutation of 1,2,3,4,5? "<<std::boolalpha<< std::is_permutation(v1.begin(), v1.end(), v2.begin())<<'\n';   std::vector<int> v3{3,5,4,1,1};std::cout<<"3,5,4,1,1 is a permutation of 1,2,3,4,5? "<<std::boolalpha<< 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

[編集]関連項目

指定範囲の要素より辞書的に大きな次の順列を生成します
(関数テンプレート)[edit]
指定範囲の要素より辞書的に小さな次の順列を生成します
(関数テンプレート)[edit]
close