template< class InputIt1, class InputIt2 >
bool equal( InputIt1 first1, InputIt1 last1,
InputIt2 first2 );
|
(1) |
(C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
bool equal( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2 );
|
(2) |
(C++17 起) |
template< class InputIt1, class InputIt2, class BinaryPred >
bool equal( InputIt1 first1, InputIt1 last1,
InputIt2 first2, BinaryPred p );
|
(3) |
(C++20 起为 constexpr) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class BinaryPred >
bool equal( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, BinaryPred p );
|
(4) |
(C++17 起) |
template< class InputIt1, class InputIt2 >
bool equal( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2 );
|
(5) |
(C++14 起) (C++20 起为 constexpr) |
template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 >
bool equal( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2 );
|
(6) |
(C++17 起) |
template< class InputIt1, class InputIt2, class BinaryPred >
bool equal( InputIt1 first1, InputIt1 last1,
InputIt2 first2, InputIt2 last2, BinaryPred p );
|
(7) |
(C++14 起) (C++20 起为 constexpr) |
template< class ExecutionPolicy,
class ForwardIt1, class ForwardIt2, class BinaryPred >
bool equal( ExecutionPolicy&& policy,
ForwardIt1 first1, ForwardIt1 last1,
ForwardIt2 first2, ForwardIt2 last2, BinaryPred p );
|
(8) |
(C++17 起) |
| | |
检查 [first1, last1) 与从 first2 开始的另一个范围是否相等:
- 对于重载 (1-4),第二个范围包含
std::distance(first1, last1) 个元素。
- 对于重载 (5-8),第二个范围是
[first2, last2)。
1,5) 用 operator== 比较元素。
3,7) 用给定的二元谓词 p 比较元素。
2,4,6,8) 同 (1,3,5,7),但按照 policy 执行。
这些重载只有在满足以下所有条件时才会参与重载决议:
|
std::is_execution_policy_v<std::decay_t<ExecutionPolicy>> 是 true。
|
(C++20 前) |
|
std::is_execution_policy_v<std::remove_cvref_t<ExecutionPolicy>> 是 true。
|
(C++20 起) |
参数
| first1, last1
|
-
|
要比较的第一元素范围的迭代器对
|
| first2, last2
|
-
|
要比较的第二元素范围的迭代器对
|
| policy
|
-
|
所用的执行策略
|
| p
|
-
|
若元素应被当做相等则返回 true 的二元谓词
谓词函数的签名应等价于如下:
bool pred(const Type1 &a, const Type2 &b);
虽然签名不必有 const & ,函数也不能修改传递给它的对象,而且必须接受(可为 const 的)类型 Type1 与 Type2 的值,无关乎值类别(从而不允许 Type1 & ,亦不允许 Type1 ,除非 Type1 的移动等价于复制(C++11 起))。
类型 Type1 与 Type2 必须使得 InputIt1 与 InputIt2 类型的对象在解引用后分别能隐式转换到 Type1 与 Type2。
|
| 类型要求
|
-InputIt1, InputIt2 必须满足老式输入迭代器 (LegacyInputIterator) 。
|
-ForwardIt1, ForwardIt2 必须满足老式向前迭代器 (LegacyForwardIterator) 。
|
-BinaryPred 必须满足二元谓词 (BinaryPredicate) 。
|
返回值
1-4) 如果两个范围内的对应元素都相等,那么返回 true。否则返回 false。
5-8) 如果 std::distance(first1, last1) 与 std::distance(first2, last2) 相等,并且两个范围内的对应元素都相等,那么返回 true。否则返回 false。
复杂度
给定 \(\scriptsize N_1\)N1 为 std::distance(first1, last1),\(\scriptsize N_2\)N2 为 std::distance(first2, last2):
1) 应用最多 \(\scriptsize N_1\)N1 次 operator== 进行比较。
2) 应用 \(\scriptsize O(N_1)\)O(N1) 次 operator== 进行比较。
3) 应用最多 \(\scriptsize N_1\)N1 次谓词 p。
4) 应用 \(\scriptsize O(N_1)\)O(N1) 次谓词 p。
否则,给定 \(\scriptsize N\)N 为 \(\scriptsize \min(N_1,N_2)\)min(N1,N2):
5) 应用最多 \(\scriptsize N\)N 次 operator== 进行比较。
6) 应用 \(\scriptsize O(N)\)O(N) 次 operator== 进行比较。
7) 应用最多 \(\scriptsize N\)N 次谓词 p。
8) 应用 \(\scriptsize O(N)\)O(N) 次谓词 p。
异常
拥有名为 ExecutionPolicy 的模板形参的重载按下列方式报告错误:
- 如果作为算法一部分调用的函数的执行抛出异常,且
ExecutionPolicy 是标准策略之一,那么调用 std::terminate。对于任何其他 ExecutionPolicy,行为由实现定义。
- 如果算法无法分配内存,那么抛出 std::bad_alloc。
可能的实现
| equal (1)
|
template<class InputIt1, class InputIt2>
constexpr //< 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 //< 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
{
// 随机访问迭代器实现(可以快速检测范围大小)
template<class RandomIt1, class RandomIt2>
constexpr //< 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;
}
// 输入迭代器实现(需要手动与 “last2” 进行比较)
template<class InputIt1, class InputIt2>
constexpr //< 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
{
// 随机访问迭代器实现(可以快速检测范围大小)
template<class RandomIt1, class RandomIt2, class BinaryPred>
constexpr //< 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;
}
// 输入迭代器实现(需要手动与 “last2” 进行比较)
template<class InputIt1, class InputIt2, class BinaryPred>
constexpr //< 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());
}
|
注解
std::equal 不可应用到由 std::unordered_set、std::unordered_multiset、std::unordered_map 或 std::unordered_multimap 的迭代器构成的范围,因为即使此类容器存储相同的元素,在容器内元素存储的顺序也可能不同。
比较整个容器或字符串视图(C++17 起)是否相等时,针对该类型的 operator== 重载通常是更好的选择。
序列版本的 std::equal 不保证是短路的。例如,如果两个范围的首对元素不相等,剩余元素也可能被被比较。在用 std::memcmp 或实现特有的向量化算法比较范围时可能发生非短路比较。
示例
下面的代码使用 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) ? " 是" : " 不是")
<< "回文\n";
}
int main()
{
test("radar");
test("hello");
}
输出:
参阅
|
|
查找首个满足特定条件的元素 (函数模板) |
|
|
当一个范围字典序小于另一个时返回 true (函数模板) |
|
|
查找两个范围的首个不同之处 (函数模板) |
|
|
搜索元素范围的首次出现 (函数模板) |
|
|
判断两组元素是否相同 (算法函数对象) |
|
|
实现 x == y 的函数对象 (类模板) |
|
|
返回匹配特定键值的元素范围 (函数模板) |