C++ 算法 中的 mismatch函数

首页 / C++入门教程 / C++ 算法 中的 mismatch函数

C++算法mismatch()函数比较两个集合以发现值是否不匹配。该函数返回两个不匹配的集合的第一个元素。

mismatch - 语法

template<class InputIterator1, classInputIterator2>
pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator2 first2>

template<class InputIterator1, class InputIterator2, class BinaryPredicate> pair<InputIterator1,InputIterator2>
mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2,BinaryPredicate pred);

mismatch - 参数

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

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

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

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

mismatch - 返回值

如果函数发现一对不匹配的元素,则它将返回第一对这样的元素,每个集合中都有一个。

如果集合中的所有元素都不匹配,则该函数返回pair(first1,first2)

如果该对中的整个元素匹配,则该函数将对last1和具有相同位置的元素返回到第二个集合中的last1。

mismatch - 例子1

#include<iostream>
#include<algorithm>
#include<vector>
#include<utility>
bool newpredicate(int m, int n)
{
	return(m==n);
}
int main()
{
	std::vector<int> newvector;
	for (int m=1; m<6; m++)
	newvector.push_back(m*10);
	int newints[]={10,20,80,320,1024};
	std::pair<std::vector<int>::iterator,int*> newpair;
	newpair=std::mismatch(newvector.begin(),newvector.end(),newints);
	std::cout<<"Out of the given elements the first mismatching pair is:"<<*newpair.first;
	std::cout<<" and "<<*newpair.second<<"\n";
	++newpair.first; ++newpair.second;
	newpair=std::mismatch(newpair.first, newvector.end(),newpair.second, newpredicate);
	std::cout<<"The next pair of mismatching elements are:"<<*newpair.first;
	std::cout<<" and "<<*newpair.second<<"\n";
	return 0;
}

输出:

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

mismatch - 例子2

#include<iostream>
#include<algorithm> 
#include<vector>
using namespace std;
bool comp(int c, int d)
{   
    return (c>d);
}
 int main()
{
    vector<int> u1 = { 23, 13, 15, 20 };
    vector<int> u2 = { 1, 10, 25, 30, 45 };
    vector<int> u3 = { 12, 100, 152, 204 };
    vector<int> u4 = { 1, 10, 15, 20, 24 };
    pair< vector<int>::iterator,
    vector<int>::iterator > unpair;
     unpair = mismatch(u1.begin(), u1.end(), u2.begin());
     
    
    cout << "From the first container the element that does not match is: ";
    cout << *unpair.first << endl;
     cout << " From the second container the element that does not match container is: ";
    cout << *unpair.second << endl;
    unpair = mismatch(u3.begin(), u3.end(), u4.begin());
    cout << "From first container return value is:";
    cout << *unpair.first << endl;
    cout << " From second container return value is: ";
    cout << *unpair.second << endl;
   }

输出:

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  

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

Linux性能优化实战 -〔倪朋飞〕

从0开始做增长 -〔刘津〕

深入浅出计算机组成原理 -〔徐文浩〕

研发效率破局之道 -〔葛俊〕

Django快速开发实战 -〔吕召刚〕

李智慧 · 高并发架构实战课 -〔李智慧〕

云原生架构与GitOps实战 -〔王炜〕

结构执行力 -〔李忠秋〕

结构思考力 · 透过结构看思考 -〔李忠秋〕

好记忆不如烂笔头。留下您的足迹吧 :)