當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


C++ mismatch()用法及代碼示例

C++ 算法 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);

參數

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

last1:它是[first1,last1)最後一個元素的輸入迭代器。

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

pred: 它是一個二元函數,它接受兩個元素作為參數並執行函數設計的任務。

返回值

如果該函數找到一對不匹配的元素,則它返回第一對這樣的元素,每個容器中的一個。

如果容器中的元素均不匹配,則該函數返回對 (first1, first2)

如果該對中的整個元素匹配,則該函數返回一對 last1 和與第二個容器中的 last1 具有相同各自位置的元素。

例子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

例子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  

複雜度

該函數從 first1 元素到 last1 元素具有線性複雜度。

數據競爭

訪問兩個範圍內的對象。

異常

如果任何參數拋出異常,該函數將拋出異常。






相關用法


注:本文由純淨天空篩選整理自 C++ Algorithm Functions mismatch()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。