C++ 算法函数 equal()

2024 年 8 月 30 日 | 阅读 2 分钟

C++ 算法 equal() 函数比较两个容器中的元素,如果两个容器中的所有元素都匹配,则返回 true。第一个范围是 [first1,last1),第二个范围从 first2 开始。

语法

参数

first1: 它是 [first1, last1) 中第一个元素的输入迭代器。

last1: 它是 [first1, last1) 中最后一个元素的输入迭代器。

first2: 它是 [first2, last2) 中第一个元素的输入迭代器。

pred:这是一个二元函数,接受两个元素作为参数并执行函数设计好的任务。

返回值

如果两个容器中的所有元素都匹配,则该函数返回 true;否则返回 false。

示例 1

输出

Both the containers have matching elements.
Both the containers do not have equal elements.

示例 2

输出

The vector consists of: 10, 20,30,40,50
Both the containers have equal elements.

复杂度

该函数从 first1 元素到 last1 元素具有线性复杂度。

数据竞争

访问两个范围中的对象。

异常

如果任何参数抛出异常,则该函数会抛出异常。


下一个主题C++ 算法 find 函数