首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::equal

Defined in header <algorithm>

?

?

template< class InputIt1, class InputIt2 > bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2 );

(1)

?

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > bool equal( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2 );

(2)

(since C++17)

template< class InputIt1, class InputIt2, class BinaryPredicate > bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, BinaryPredicate p );

(3)

?

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate > bool equal( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, BinaryPredicate p );

(4)

(since C++17)

template< class InputIt1, class InputIt2 > bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2 );

(5)

(since C++14)

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2 > bool equal( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2 );

(6)

(since C++17)

template< class InputIt1, class InputIt2, class BinaryPredicate > bool equal( InputIt1 first1, InputIt1 last1, InputIt2 first2, InputIt2 last2, BinaryPredicate p );

(7)

(since C++14)

template< class ExecutionPolicy, class ForwardIt1, class ForwardIt2, class BinaryPredicate > bool equal( ExecutionPolicy&& policy, ForwardIt1 first1, ForwardIt1 last1, ForwardIt2 first2, ForwardIt2 last2, BinaryPredicate p );

(8)

(since C++17)

1,3%-29true如果范围[first1, last1)等于范围[first2, first2 + (last1 - first1)),和false不然的话

5.7%29true如果范围[first1, last1)等于范围[first2, last2),和false否则。

2,4,6,8%29等于%281,3,5,7%29,但根据policy。此重载只参与以下情况下的过载解决方案:std::is_execution_policy_v<std::decay_t<ExecutionPolicy>>是真的

对于每个迭代器,这两个范围被认为是相等的。i在范围内[first1,last1),,,*i等号*(first2 + (i - first1)).超载%281,2,5,6%29次使用operator==要确定两个元素是否相等,而重载%283、4、7、8%29则使用给定的二进制谓词p...

参数

first1, last1

-

the first range of the elements to compare

first2, last2

-

the second range of the elements to compare

policy

-

the execution policy to use. See execution policy for details.

p

-

binary predicate which returns ?true if the elements should be treated as equal. The signature of the predicate function should be equivalent to the following: bool pred(const Type1 &a, const Type2 &b); The signature does not need to have const &, but the function must not modify the objects passed to it. The types Type1 and Type2 must be such that objects of types InputIt1 and InputIt2 can be dereferenced and then implicitly converted to Type1 and Type2 respectively. ?

类型要求

-InputIt 1,InputIt 2必须符合InputIterator的要求。

-前进1,前进2必须符合先行者的要求。

返回值

5-8%29如果范围的长度[first1, last1)不等于范围的长度。[first2, last2)、回报false

如果两个范围中的元素相等,则返回true...

否则返回false...

注记

std::equal不应用于比较迭代器形成的范围。std::unordered_set,,,std::unordered_multiset,,,std::unordered_map,或std::unordered_multimap因为,即使两个容器存储相同的元素,这些元素在这些容器中的存储顺序也可能不同。

当比较整个容器是否相等时,operator==对于相应的容器通常是首选的。

复杂性

最多1.3%29last1---first1谓词的应用

5,7%,29,最多为28last1---first1,,,last2---first2谓词的应用程序%29。

但是,如果InputIt1InputIt2满足…的要求RandomAccessIteratorlast1 - first1 != last2 - first2然后,在不查看任何元素%29的情况下,就不会检测到谓词的应用程序%28不匹配。

2,4,6,8%29相同,但复杂度指定为O%28x%29,而不是“至多x”。

例外

带有名为ExecutionPolicy报告错误如下:

  • 如果执行作为算法一部分调用的函数,则引发异常ExecutionPolicy是其中之一标准政策,,,std::terminate叫做。对于任何其他人ExecutionPolicy,行为是由实现定义的。
  • 如果算法不能分配内存,std::bad_alloc被扔了。

可能的实施

第一版

*。

模板<class InputIt 1,class InputIt 2>bool等于%28 InputIt1 First 1,InputIt 1 last1,InputIt2 first 2%29{for%28;first 1%21=last1;+first 1,+first 1,+first 2%29{if%28%21%28%2A首1==%2A前2%29%29{返回false;}返回true;}

第二版

模板<类InputIt 1,类InputIt 2,类BinaryPredicate>bool等于%28 InputIt1 First 1,InputIt 1 last1,InputIt2 first 2,BinaryPredicate p%29{表示%28;开头1%21=LAST 1;++first 1,+First 2%29{if%28%21 p%28%2A首先,%2A前2%29%29{返回false;}返回true;}

下面的代码使用equal()若要测试字符串是否为回文,请执行以下操作。

二次

代码语言:javascript
复制
#include <algorithm>
#include <iostream>
#include <string>
 
bool is_palindrome(const std::string& s)
{
    return std::equal(s.begin(), s.begin() + s.size()/2, s.rbegin());
}
 
void test(const std::string& s)
{
    std::cout << "\"" << s << "\" "
        << (is_palindrome(s) ? "is" : "is not")
        << " a palindrome\n";
}
 
int main()
{
    test("radar");
    test("hello");
}

二次

产出:

二次

代码语言:javascript
复制
"radar" is a palindrome
"hello" is not a palindrome

二次

findfind_iffind_if_not (C++11)

finds the first element satisfying specific criteria (function template)

lexicographical_compare

returns true if one range is lexicographically less than another (function template)

mismatch

finds the first position where two ranges differ (function template)

search

searches for a range of elements (function template)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com