C++ 算法函数 mismatch()

30 Aug 2024 | 3 分钟阅读

C++ 算法 mismatch() 函数比较两个容器,以找出任何值不匹配的地方。该函数返回两个容器中第一个不匹配的元素。

语法

参数

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

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

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

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

返回值

如果函数找到一对不匹配的元素,则返回这对元素的第一个,每个容器中一个。

如果容器中的任何元素都不匹配,则函数返回对 (first1, first2)。

如果对中的所有元素都匹配,则函数返回 last1 和第二个容器中与 last1 具有相同相应位置的元素的对。

示例 1

输出

Out of the given elements the first mismatching pair is: 30 and 80
The next pair of mismatching elements are: 40 and 320

示例 2

输出

From the first container the element that does not match is: 23                                 
From the second container the element that does not match container is: 1                      
 From firt container return value is:12         
From second container return value is: 1  

复杂度

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

数据竞争

访问两个范围中的对象。

异常

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