std::equal
| 在标头 <algorithm> 定义
|
||
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)。
operator== 比较元素。p 比较元素。policy 执行。|
|
(C++20 前) |
|
|
(C++20 起) |
参数
| first1, last1 | - | 要比较的第一元素范围的迭代器对 |
| first2, last2 | - | 要比较的第二元素范围的迭代器对 |
| policy | - | 所用的执行策略 |
| p | - | 若元素应被当做相等则返回 true 的二元谓词谓词函数的签名应等价于如下:
虽然签名不必有 |
| 类型要求 | ||
-InputIt1, InputIt2 必须满足老式输入迭代器 (LegacyInputIterator) 。
| ||
-ForwardIt1, ForwardIt2 必须满足老式向前迭代器 (LegacyForwardIterator) 。
| ||
-BinaryPred 必须满足二元谓词 (BinaryPredicate) 。
| ||
返回值
true。否则返回 false。std::distance(first1, last1) 与 std::distance(first2, last2) 相等,并且两个范围内的对应元素都相等,那么返回 true。否则返回 false。复杂度
给定 N1 为 std::distance(first1, last1),N2 为 std::distance(first2, last2):
operator== 进行比较。operator== 进行比较。p。p。InputIt1 和 InputIt2 都是老式随机访问迭代器 (LegacyRandomAccessIterator) ,并且 last1 - first1 != last2 - first2 是 true,那么不会进行任何比较。operator== 进行比较。operator== 进行比较。p。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");
}
输出:
"radar" 是回文
"hello" 不是回文
参阅
(C++11) |
查找首个满足特定条件的元素 (函数模板) |
当一个范围字典序小于另一个时返回 true (函数模板) | |
| 查找两个范围的首个不同之处 (函数模板) | |
| 搜索元素范围的首次出现 (函数模板) | |
(C++20) |
判断两组元素是否相同 (算法函数对象) |
实现 x == y 的函数对象 (类模板) | |
| 返回匹配特定键值的元素范围 (函数模板) |