Last Updated : 06 Jul, 2017
C++ STL has lots of useful functions that helps us to achieve various programming tasks. One such function is "
mismatch()" . This function, defined in "
algorithm" header file, helps to
compare 2 containers for mismatches. This function has 2 versions. Both are discussed in this article.
// C++ code to demonstrate the working of
// mismatch( start_iter1, end_iter1, start_iter2 )
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main()
{
// initializing vectors
vector<int> v1 = { 1, 10, 15, 20 };
vector<int> v2 = { 1, 10, 25, 30, 45 };
vector<int> v3 = { 1, 10, 15, 20 };
vector<int> v4 = { 1, 10, 15, 20, 24 };
// declaring pointer pair
pair< vector<int>::iterator,
vector<int>::iterator > mispair;
// using mismatch() to search for 1st mismatch
mispair = mismatch(v1.begin(), v1.end(), v2.begin());
// printing the mismatch pair
// 1st mismatch at 15 and 25
cout << "The 1st mismatch element of 1st container : ";
cout << *mispair.first << endl;
cout << "The 1st mismatch element of 2nd container : ";
cout << *mispair.second << endl;
// using mismatch() to search for 1st mismatch
mispair = mismatch(v3.begin(), v3.end(), v4.begin());
// printing the mismatch pair
// no mismatch
// points to position after last 0 and corresponding 24
cout << "The returned value from 1st container is : ";
cout << *mispair.first << endl;
cout << "The returned value from 2nd container is : ";
cout << *mispair.second << endl;
}
Output:
The 1st mismatch element of 1st container : 15 The 1st mismatch element of 2nd container : 25 The returned value from 1st container is : 0 The returned value from 2nd container is : 24
// C++ code to demonstrate the working of
// mismatch( start_iter1, end_iter1, start_iter2, comparator )
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
// comparator function
// returns true when element from
// 1st element is greater than 2nd
bool compare(int a, int b)
{
return (a>b);
}
int main()
{
// initializing vectors
vector<int> v1 = { 23, 13, 15, 20 };
vector<int> v2 = { 1, 10, 25, 30, 45 };
vector<int> v3 = { 12, 100, 152, 204 };
vector<int> v4 = { 1, 10, 15, 20, 24 };
// declaring pointer pair
pair< vector<int>::iterator,
vector<int>::iterator > mispair;
// using mismatch() to search for 1st mismatch
mispair = mismatch(v1.begin(), v1.end(), v2.begin(), compare);
// printing the mismatch pair
// 1st mismatch at 15 and 25
// 15 is 1st element less than 2nd at same position
cout << "The 1st mismatch element of 1st container : ";
cout << *mispair.first << endl;
cout << "The 1st mismatch element of 2nd container : ";
cout << *mispair.second << endl;
// using mismatch() to search for 1st mismatch
mispair = mismatch(v3.begin(), v3.end(), v4.begin(), compare);
// printing the mismatch pair
// no mismatch
// all elements in 1st container are greater than 2nd
// points to position after last 0 and corresponding 24
cout << "The returned value from 1st container is : ";
cout << *mispair.first << endl;
cout << "The returned value from 2nd container is : ";
cout << *mispair.second << endl;
}
Output:
The 1st mismatch element of 1st container : 15 The 1st mismatch element of 2nd container : 25 The returned value from 1st container is : 0 The returned value from 2nd container is : 24
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4