std::ranges::equal
定義於標頭檔案 <algorithm> |
||
呼叫簽名 (Call signature) |
||
template< std::input_iterator I1, std::sentinel_for<I1> S1, std::input_iterator I2, std::sentinel_for<I2> S2, |
(1) | (C++20 起) |
template< ranges::input_range R1, ranges::input_range R2, class Pred = ranges::equal_to, |
(2) | (C++20 起) |
[
first1,
last1)
的投影值等於範圍 [
first2,
last2)
的投影值,則返回 true,否則返回 false。如果兩個範圍具有相同數量的元素,並且每對對應的投影元素都滿足 pred,則認為它們相等。也就是說,對於兩個範圍中所有對應的元素對,std::invoke(pred, std::invoke(proj1, *first1), std::invoke(proj2, *first2)) 返回 true。
本頁描述的類函式實體是 演算法函式物件(非正式地稱為 niebloids),即
目錄 |
[編輯] 引數
first1, last1 | - | 定義要比較的第一個元素範圍的迭代器-哨兵對 |
r1 | - | 要比較的第一個元素範圍 |
first2, last2 | - | 定義要比較的第二個元素範圍的迭代器-哨兵對 |
r2 | - | 要比較的第二個元素範圍 |
pred | - | 應用於投影元素的二元謂詞 |
proj1 | - | 應用於第一個元素範圍的投影 |
proj2 | - | 應用於第二個元素範圍的投影 |
[編輯] 返回值
如果範圍 [
first1,
last1)
的長度不等於範圍 [
first2,
last2)
的長度,則返回 false。
如果兩個範圍中的元素在投影后相等,則返回 true。
否則返回 false。
[編輯] 注意
不應使用 ranges::equal
比較由 std::unordered_set、std::unordered_multiset、std::unordered_map 或 std::unordered_multimap 的迭代器形成的範圍,因為這些容器中元素的儲存順序可能不同,即使兩個容器儲存相同的元素。
在比較整個容器或字串檢視是否相等時,通常更傾向於使用對應型別的 operator==。
ranges::equal
不保證短路。例如,如果兩個範圍的第一個元素對不比較相等,其餘元素也可能被比較。非短路比較可能發生在範圍與 std::memcmp 或實現特定的向量化演算法進行比較時。
[編輯] 複雜度
最多應用 min(last1 - first1, last2 - first2) 次謂詞和相應的投影。
然而,如果 S1 和 S2 都為其各自的迭代器建模 std::sized_sentinel_for,並且 last1 - first1 != last2 - first2,則不進行謂詞的應用(無需檢視任何元素即可檢測到大小不匹配)。
[編輯] 可能實現
struct equal_fn { template<std::input_iterator I1, std::sentinel_for<I1> S1, std::input_iterator I2, std::sentinel_for<I2> S2, class Pred = ranges::equal_to, class Proj1 = std::identity, class Proj2 = std::identity> requires std::indirectly_comparable<I1, I2, Pred, Proj1, Proj2> constexpr bool operator()(I1 first1, S1 last1, I2 first2, S2 last2, Pred pred = {}, Proj1 proj1 = {}, Proj2 proj2 = {}) const { if constexpr (std::sized_sentinel_for<S1, I1> and std::sized_sentinel_for<S2, I2>) if (std::ranges::distance(first1, last1) != std::ranges::distance(first2, last2)) return false; for (; first1 != last1; ++first1, (void)++first2) if (!std::invoke(pred, std::invoke(proj1, *first1), std::invoke(proj2, *first2))) return false; return true; } template<ranges::input_range R1, ranges::input_range R2, class Pred = ranges::equal_to, class Proj1 = std::identity, class Proj2 = std::identity> requires std::indirectly_comparable<ranges::iterator_t<R1>, ranges::iterator_t<R2>, Pred, Proj1, Proj2> 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::ref(pred), std::ref(proj1), std::ref(proj2)); } }; inline constexpr equal_fn equal; |
[編輯] 示例
以下程式碼使用 ranges::equal 來測試字串是否為迴文。
#include <algorithm> #include <iomanip> #include <iostream> #include <ranges> #include <string_view> constexpr bool is_palindrome(const std::string_view s) { namespace views = std::views; auto forward = s | views::take(s.size() / 2); auto backward = s | views::reverse | views::take(s.size() / 2); return std::ranges::equal(forward, backward); } void test(const std::string_view s) { std::cout << std::quoted(s) << " is " << (is_palindrome(s) ? "" : "not ") << "a palindrome\n"; } int main() { test("radar"); test("hello"); static_assert(is_palindrome("ABBA") and not is_palindrome("AC/DC")); }
輸出
"radar" is a palindrome "hello" is not a palindrome
[編輯] 參閱
(C++20)(C++20)(C++20) |
尋找第一個滿足特定條件的元素 (演算法函式物件) |
如果一個範圍在字典上小於另一個範圍,則返回 true (演算法函式物件) | |
(C++20) |
尋找兩個範圍開始不同的第一個位置 (演算法函式物件) |
(C++20) |
搜尋一個範圍的元素首次出現的位置 (演算法函式物件) |
(C++20) |
返回與特定鍵匹配的元素範圍 (演算法函式物件) |
實現 x == y 的函式物件 (類模板) | |
判斷兩組元素是否相同 (函式模板) |