std::is_permutation
| ヘッダー <algorithm> で定義 |
||
| template< class ForwardIt1, class ForwardIt2 > bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(1) | (C++11以降) (C++20 以降 constexpr) |
| template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > |
(2) | (C++11以降) (C++20 以降 constexpr) |
| template< class ForwardIt1, class ForwardIt2 > bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(3) | (C++14以降) (C++20 以降 constexpr) |
| template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > |
(4) | (C++14以降) (C++20 以降 constexpr) |
[first1, last1) が first2 から始まる範囲の 順列 であるかどうかをチェックします。
- オーバーロード (1,2) の場合、2 番目の範囲は std::distance(first1, last1) 個の要素を持ちます。
- オーバーロード (3,4) の場合、2 番目の範囲は
[first2,last2)です。
ForwardIt1 と ForwardIt2 が異なる 値型 を持つ場合、プログラムは不正です。
比較関数が 同値関係 でない場合、動作は未定義です。
目次 |
[編集] パラメーター
| first1, last1 | - | 比較する最初の要素の範囲を定義するイテレータのペア |
| first2, last2 | - | 比較する2番目の要素の範囲を定義するイテレータのペア |
| p | - | 要素が等しいと見なされる場合に true を返す二項述語。 述語関数のシグネチャは、以下と同等である必要がある。 bool pred(const Type1 &a, const Type2 &b); シグネチャは const & を持つ必要はないが、関数は渡されたオブジェクトを変更してはならず、値カテゴリに関わらず、(おそらく const の) |
| 型要件 | ||
-ForwardIt1, ForwardIt2 は LegacyForwardIterator の要件を満たさなければなりません。 | ||
[編集] 戻り値
範囲 [first1, last1) が範囲 [first2, last2) の順列である場合は true、そうでない場合は false。
[編集] 計算量
std::distance(first1, last1) を N とすると
) 回の比較が行われます。
) 回の適用が行われます。
ForwardIt1 と ForwardIt2 の両方が LegacyRandomAccessIterator であり、かつ last1 - first1 != last2 - first2 が true の場合、比較は行われません。) 回の比較が行われます。
) 回の適用が行われます。
[編集] 可能な実装
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[編集] 関連項目
| 要素の範囲の次に大きい辞書順の順列を生成する (関数テンプレート) | |
| 要素の範囲の次に小さい辞書順の順列を生成する (関数テンプレート) | |
| (C++20) |
relationが同値関係を課すことを規定する(コンセプト) |
| (C++20) |
あるシーケンスが別のシーケンスの順列であるかを判断する (アルゴリズム関数オブジェクト) |