std::equal
定义于头文件 <algorithm> |
||
template< class InputIt1, class InputIt2 > bool equal( InputIt1 first1, InputIt1 last1, |
(1) | (C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > bool equal( ExecutionPolicy&& policy, |
(2) | (C++17 起) |
template< class InputIt1, class InputIt2, class BinaryPred > bool equal( InputIt1 first1, InputIt1 last1, |
(3) | (C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPred > |
(4) | (C++17 起) |
template< class InputIt1, class InputIt2 > bool equal( InputIt1 first1, InputIt1 last1, |
(5) | (C++14 起) (C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > bool equal( ExecutionPolicy&& policy, |
(6) | (C++17 起) |
template< class InputIt1, class InputIt2, class BinaryPred > bool equal( InputIt1 first1, InputIt1 last1, |
(7) | (C++14 起) (C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPred > |
(8) | (C++17 起) |
检查 [first1, last1) 范围和从 first2 开始的范围是否相等。
- 对于重载 (1-4),第二个范围有 std::distance(first1, last1) 个元素。
- 对于重载 (5-8),第二个范围是 [first2, last2)。
std::is_execution_policy_v |
(C++20 前) |
std::is_execution_policy_v |
(C++20 起) |
目录 |
[edit] 参数
first1, last1 | - | 定义要比较的第一个元素范围的迭代器对 |
first2, last2 | - | 定义要比较的第二个元素范围的迭代器对 |
policy | - | 要使用的 执行策略 |
p | - | 二元谓词,如果元素应被视为相等,则返回 true。 谓词函数的签名应等效于以下内容: bool pred(const Type1 &a, const Type2 &b); 虽然签名不需要包含 const &,但函数不得修改传递给它的对象,并且必须能够接受 Type1 和 Type2 类型的所有值(可能是 const),无论其值类别如何(因此,不允许 Type1 &,C++11 起也不允许 Type1,除非对于 Type1,移动等效于复制)。 |
类型要求 | ||
-InputIt1, InputIt2 必须满足 LegacyInputIterator 的要求。 | ||
-ForwardIt1, ForwardIt2 必须满足 LegacyForwardIterator 的要求。 | ||
-BinaryPred 必须满足 BinaryPredicate 的要求。 |
[edit] 返回值
[edit] 复杂度
给定 N1 为 std::distance(first1, last1) 且 N2 为 std::distance(first2, last2)
[edit] 异常
带有模板参数 ExecutionPolicy
的重载按如下方式报告错误
- 如果作为算法一部分调用的函数执行抛出异常,并且 ExecutionPolicy 是标准策略之一,则调用 std::terminate。对于任何其他 ExecutionPolicy,行为是实现定义的。
- 如果算法未能分配内存,则抛出 std::bad_alloc。
[edit] 可能的实现
equal (1) |
---|
template<class InputIt1, class InputIt2> constexpr //< since C++20 bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2) { for (; first1 != last1; ++first1, ++first2) if (!(*first1 == *first2)) return false; return true; } |
equal (3) |
template<class InputIt1, class InputIt2, class BinaryPred> constexpr //< since C++20 bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPred p) { for (; first1 != last1; ++first1, ++first2) if (!p(*first1, *first2)) return false; return true; } |
equal (5) |
namespace detail { // random-access iterator implementation (allows quick range size detection) template<class RandomIt1, class RandomIt2> constexpr //< since C++20 bool equal(RandomIt1 first1, RandomIt1 last1, RandomIt2 first2, RandomIt2 last2, std::random_access_iterator_tag, std::random_access_iterator_tag) { if (last1 - first1 != last2 - first2) return false; for (; first1 != last1; ++first1, ++first2) if (!(*first1 == *first2)) return false; return true; } // input iterator implementation (needs to manually compare with “last2”) template<class InputIt1, class InputIt2> constexpr //< since C++20 bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, std::input_iterator_tag, std::input_iterator_tag) { for (; first1 != last1 && first2 != last2; ++first1, ++first2) if (!(*first1 == *first2)) return false; return first1 == last1 && first2 == last2; } } template<class InputIt1, class InputIt2> constexpr //< since C++20 bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2) { details::equal(first1, last1, first2, last2, typename std::iterator_traits<InputIt1>::iterator_category(), typename std::iterator_traits<InputIt2>::iterator_category()); } |
equal (7) |
namespace detail { // random-access iterator implementation (allows quick range size detection) template<class RandomIt1, class RandomIt2, class BinaryPred> constexpr //< since C++20 bool equal(RandomIt1 first1, RandomIt1 last1, RandomIt2 first2, RandomIt2 last2, BinaryPred p, std::random_access_iterator_tag, std::random_access_iterator_tag) { if (last1 - first1 != last2 - first2) return false; for (; first1 != last1; ++first1, ++first2) if (!p(*first1, *first2)) return false; return true; } // input iterator implementation (needs to manually compare with “last2”) template<class InputIt1, class InputIt2, class BinaryPred> constexpr //< since C++20 bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPred p, std::input_iterator_tag, std::input_iterator_tag) { for (; first1 != last1 && first2 != last2; ++first1, ++first2) if (!p(*first1, *first2)) return false; return first1 == last1 && first2 == last2; } } template<class InputIt1, class InputIt2, class BinaryPred> constexpr //< since C++20 bool equal(InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPred p) { details::equal(first1, last1, first2, last2, p, typename std::iterator_traits<InputIt1>::iterator_category(), typename std::iterator_traits<InputIt2>::iterator_category()); } |
[edit] 注意
std::equal 不应用于比较由 std::unordered_set、std::unordered_multiset、std::unordered_map 或 std::unordered_multimap 的迭代器形成的范围,因为即使两个容器存储相同的元素,这些容器中元素的存储顺序也可能不同。
当比较整个容器或字符串视图的相等性时,通常首选相应类型的 operator==(C++17 起)。
顺序 std::equal 不保证短路。例如,如果两个范围的第一个元素对不相等,其余元素也可能被比较。当范围与 std::memcmp 或实现特定的向量化算法进行比较时,可能会发生非短路比较。
[edit] 示例
以下代码使用 std::equal 测试字符串是否为回文。
#include <algorithm> #include <iomanip> #include <iostream> #include <string_view> constexpr bool is_palindrome(const std::string_view& s) { return std::equal(s.cbegin(), s.cbegin() + s.size() / 2, s.crbegin()); } void test(const std::string_view& s) { std::cout << std::quoted(s) << (is_palindrome(s) ? " is" : " is not") << " a palindrome\n"; } int main() { test("radar"); test("hello"); }
输出
"radar" is a palindrome "hello" is not a palindrome
[edit] 另请参阅
(C++11) |
寻找第一个满足特定条件的元素 (函数模板) |
如果一个范围在字典上小于另一个范围,则返回 true (函数模板) | |
寻找两个范围开始不同的第一个位置 (函数模板) | |
搜索一个范围的元素首次出现的位置 (函数模板) | |
(C++20) |
判断两组元素是否相同 (算法函数对象) |
实现 x == y 的函数对象 (类模板) | |
返回与特定键匹配的元素范围 (函数模板) |