std::is_permutation
ヘッダ <algorithm> で定義
|
||
(1) | ||
template< class ForwardIt1, class ForwardIt2 > bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(C++11以上) (C++20未満) |
|
template< class ForwardIt1, class ForwardIt2 > constexpr bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(C++20以上) | |
(2) | ||
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(C++11以上) (C++20未満) |
|
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > constexpr bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(C++20以上) | |
(3) | ||
template< class ForwardIt1, class ForwardIt2 > bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(C++14以上) (C++20未満) |
|
template< class ForwardIt1, class ForwardIt2 > constexpr bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(C++20以上) | |
(4) | ||
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(C++14以上) (C++20未満) |
|
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > constexpr bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(C++20以上) | |
範囲 [first2,last2)
と等しい範囲を作る範囲 [first1, last1)
内の要素の順列が存在するならば true を返します。 last2
が指定されない場合は first2 + (last1 - first1) になります。
目次 |
[編集] 引数
first1, last1 | - | 比較する要素の範囲 |
first2, last2 | - | 2つめの比較する範囲 |
p | - | 要素が等しいとみなされるべき場合に true を返す二項述語。 述語関数のシグネチャは以下と同等であるべきです。 bool pred(const Type &a, const Type &b);
|
型の要件 | ||
-ForwardIt1, ForwardIt2 は LegacyForwardIterator の要件を満たさなければなりません。
| ||
-ForwardIt1, ForwardIt2 は同じ値型を持たなければなりません。
|
[編集] 戻り値
範囲 [first1, last1)
が範囲 [first2, last2)
の順列であれば true。
[編集] 計算量
多くとも O(N2) 回の述語の適用、またはシーケンスがすでに等しい場合はちょうど N 回。 ただし N=std::distance(first1, last1) です。
しかし、 ForwardIt1
および ForwardIt2
が LegacyRandomAccessIterator の要件を満たし、 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 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 = 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) { return false; } } } return true; } |
[編集] 例
#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
[編集] 関連項目
指定範囲の要素より辞書的に大きな次の順列を生成します (関数テンプレート) | |
指定範囲の要素より辞書的に小さな次の順列を生成します (関数テンプレート) |