TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ algorithm mismatch() function

C++ algorithm mismatch() function tutorial for beginners and professionals with examples on adjacent_find(),any_of(), copy(), copy_if(), count(), count_if(), equal(), find(), find_end(), find_first_of(), find_if(), find_if_not(), for_each() etc.

<< Back to CPP

C++ Algorithm Functions mismatch()

C++ Algorithm mismatch() function compares both the containers to spot for any mismatch of values. The function returns the first element of both the containers that does not match.

Syntax

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);

Parameter

first1: It is an input iterator to the first element of the [first1, last1).

last1: It is an input iterator to the last element of the [first1, last1).

first2: It is an input iterator to the first element of the [first2, last2).

pred: It is a binary function that accepts two elements as arguments and performs the task designed by the function.

Return value

If the function finds a pair of elements that does not match then it returns the first pair of such element, one from each container.

In case none of the elements from the containers match the the function returns the pair(first1, first2)

If the entire element in the pair matches then the function returns a pair of last1 and the element with same respective position to last1 in the second container.

Example 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;
}

Output:

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

Example 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;
   }

Output:

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  

Complexity

The function has linear complexity from the first1 element to the last1 element.

Data races

Objects in both ranges are accessed.

Exceptions

The function throws an exception if any of the argument throws one.






Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf