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),第二個範圍擁有 std::distance(first1, last1) 個元素。
- 對於過載 (3,4),第二個範圍是
[
first2,
last2)
。
若 ForwardIt1
和 ForwardIt2
有不同的值型別,則程式非良構。
若比較函式不是等價關係,則行為未定義。
目錄 |
[編輯] 引數
first1, last1 | - | 定義要比較的第一個元素範圍的迭代器對 |
first2, last2 | - | 定義要比較的第二個元素範圍的迭代器對 |
p | - | 二元謂詞,如果元素應被視為相等,則返回 true。 謂詞函式的簽名應等效於以下內容: bool pred(const Type1 &a, const Type2 &b); 雖然簽名不需要有 const &,但該函式絕不能修改傳遞給它的物件,且必須能接受 |
型別要求 | ||
-ForwardIt1, ForwardIt2 必須滿足 LegacyForwardIterator 的要求。 |
[編輯] 返回值
若範圍 [
first1,
last1)
是範圍 [
first2,
last2)
的排列,則為 true,否則為 false。
[編輯] 複雜度
給定 N 為 std::distance(first1, last1)
) 次比較。
) 次。
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) |
確定一個序列是否是另一個序列的排列 (演算法函式物件) |