std::ranges::is_permutation
| ヘッダー <algorithm> で定義 |
||
| 呼び出しシグネチャ |
||
| template< std::forward_iterator I1, std::sentinel_for<I1> S1, std::forward_iterator I2, std::sentinel_for<I2> S2, |
(1) | (C++20以降) |
| template< ranges::forward_range R1, ranges::forward_range R2, class Proj1 = std::identity, class Proj2 = std::identity, |
(2) | (C++20以降) |
[first1, last1) の要素の順列が存在し、対応する射影 Proj1, Proj2 を適用し、バイナリ述語 Pred を比較子として使用した後に、範囲 [first2, last2) と 等しく なる場合に true を返します。それ以外の場合は false を返します。このページで説明されている関数のようなエンティティは、アルゴリズム関数オブジェクト(非公式にはニーブロイドとして知られている)です。つまり、
- これらのいずれかを呼び出す際に、明示的なテンプレート引数リストを指定することはできません。
- これらのいずれも実引数依存の名前探索には見えません。
- これらのいずれかが関数呼び出し演算子の左側の名前として通常の非修飾名探索によって見つかった場合、実引数依存の名前探索は抑制されます。
目次 |
[edit] パラメータ
| first1, last1 | - | 最初の要素の 範囲 を定義するイテレータ-センチネルのペア |
| first2, last2 | - | 2番目の要素の 範囲 を定義するイテレータ-センチネルのペア |
| r1 | - | 最初の range の要素 |
| r2 | - | 2番目の range の要素 |
| pred | - | 射影された要素に適用する述語 |
| proj1 | - | 最初の範囲の要素に適用する射影 |
| proj2 | - | 2番目の範囲の要素に適用する射影 |
[edit] 戻り値
範囲 [first1, last1) が範囲 [first2, last2) の順列である場合は true。
[edit] 計算量
述語と各射影の適用は最大 O(N2) 回、またはシーケンスがすでに等しい場合はちょうど N 回です。ここで N は ranges::distance(first1, last1) です。ただし、ranges::distance(first1, last1) != ranges::distance(first2, last2) の場合、述語と射影の適用は行われません。
[edit] 注記
「順列」関係は 同値関係 です。
ranges::is_permutation は、例えばソート、シャッフル、パーティションなどの再配置アルゴリズムの正しさを確認するためにテストで使用できます。p が元のシーケンスで、q が「変更された」シーケンスである場合、ranges::is_permutation(p, q) == true は、q が p と「同じ」要素(順序が入れ替わっている可能性がある)で構成されていることを意味します。
[edit] 可能な実装
struct is_permutation_fn { template<std::forward_iterator I1, std::sentinel_for<I1> S1, std::forward_iterator I2, std::sentinel_for<I2> S2, class Proj1 = std::identity, class Proj2 = std::identity, std::indirect_equivalence_relation<std::projected<I1, Proj1>, std::projected<I2, Proj2>> Pred = ranges::equal_to> constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { // skip common prefix auto ret = std::ranges::mismatch(first1, last1, first2, last2, std::ref(pred), std::ref(proj1), std::ref(proj2)); first1 = ret.in1, first2 = ret.in2; // iterate over the rest, counting how many times each element // from [first1, last1) appears in [first2, last2) for (auto i {first1}; i != last1; ++i) { const auto i_proj {std::invoke(proj1, *i)}; auto i_cmp = [&]<typename T>(T&& t) { return std::invoke(pred, i_proj, std::forward<T>(t)); }; if (i != ranges::find_if(first1, i, i_cmp, proj1)) continue; // this *i has been checked if (const auto m {ranges::count_if(first2, last2, i_cmp, proj2)}; m == 0 or m != ranges::count_if(i, last1, i_cmp, proj1)) return false; } return true; } template<ranges::forward_range R1, ranges::forward_range R2, class Proj1 = std::identity, class Proj2 = std::identity, std::indirect_equivalence_relation< std::projected<ranges::iterator_t<R1>, Proj1>, std::projected<ranges::iterator_t<R2>, Proj2>> Pred = ranges::equal_to> constexpr bool operator()(R1&& r1, R2&& r2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { return (*this)(ranges::begin(r1), ranges::end(r1), ranges::begin(r2), ranges::end(r2), std::move(pred), std::move(proj1), std::move(proj2)); } }; inline constexpr is_permutation_fn is_permutation {}; |
[edit] 例
#include <algorithm> #include <array> #include <cmath> #include <iostream> #include <ranges> auto& operator<<(auto& os, std::ranges::forward_range auto const& v) { os << "{ "; for (const auto& e : v) os << e << ' '; return os << "}"; } int main() { static constexpr auto r1 = {1, 2, 3, 4, 5}; static constexpr auto r2 = {3, 5, 4, 1, 2}; static constexpr auto r3 = {3, 5, 4, 1, 1}; static_assert( std::ranges::is_permutation(r1, r1) && std::ranges::is_permutation(r1, r2) && std::ranges::is_permutation(r2, r1) && std::ranges::is_permutation(r1.begin(), r1.end(), r2.begin(), r2.end())); std::cout << std::boolalpha << "is_permutation(" << r1 << ", " << r2 << "): " << std::ranges::is_permutation(r1, r2) << '\n' << "is_permutation(" << r1 << ", " << r3 << "): " << std::ranges::is_permutation(r1, r3) << '\n' << "is_permutation with custom predicate and projections: " << std::ranges::is_permutation( std::array {-14, -11, -13, -15, -12}, // 1st range std::array {'F', 'E', 'C', 'B', 'D'}, // 2nd range [](int x, int y) { return abs(x) == abs(y); }, // predicate [](int x) { return x + 10; }, // projection for 1st range [](char y) { return int(y - 'A'); }) // projection for 2nd range << '\n'; }
出力
is_permutation({ 1 2 3 4 5 }, { 3 5 4 1 2 }): true
is_permutation({ 1 2 3 4 5 }, { 3 5 4 1 1 }): false
is_permutation with custom predicate and projections: true[edit] 関連項目
| (C++20) |
要素の範囲の次に大きい辞書順の順列を生成する (アルゴリズム関数オブジェクト) |
| (C++20) |
要素の範囲の次に小さい辞書順の順列を生成する (アルゴリズム関数オブジェクト) |
| (C++11) |
あるシーケンスが別のシーケンスの順列であるかを判断する (関数テンプレート) |
| 要素の範囲の次に大きい辞書順の順列を生成する (関数テンプレート) | |
| 要素の範囲の次に小さい辞書順の順列を生成する (関数テンプレート) | |
| (C++20) |
relationが同値関係を課すことを規定する(コンセプト) |