std::mismatch() with examples in C++
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.
- mismatch( start_iter1, end_iter1, start_iter2 ) This version of mismatch only test for inequality.
Here, there are 3 arguments,
start_iter1: Beginning iterator to 1st container
end_iter1: Last iterator to 1st container
start_iter2: Starting iterator to the 2nd iterator from where comparison is desired to begin.This function returns the 1st mismatch pair pointer, first element pointing to position of first mismatch element of 1st container, second element pointing to position of first mismatch element of 2nd container. If no mismatch is found, 1st element points to position after last element of 1st container and 2nd points to corresponding position in 2nd container.
// 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;
}
chevron_rightfilter_noneOutput:
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
- mismatch( start_iter1, end_iter1, start_iter2, comparator) : This function is almost similar to the working as the above mentioned version, but it offers to find not only equality mismatches, but also other user-defined and desired mismatches via user- defined comparator function that is sent as 4th argument and returns a boolean true or false.
// 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;
}
chevron_rightfilter_noneOutput:
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
This article is contributed by Manjeet Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- ratio_not_equal() in C++ with examples
- C/C++ if statement with Examples
- C/C++ do while loop with Examples
- C/C++ if else if ladder with Examples
- C/C++ if else statement with Examples
- C/C++ while loop with Examples
- ios eof() function in C++ with Examples
- set_symmetric_difference in C++ with Examples
- SDL library in C/C++ with examples
- ratio_equal() in C++ with examples
- feclearexcept in C++ with Examples
- C/C++ For loop with Examples
- Pointers in C/C++ with Examples
- mbrtoc32() in C/C++ with Examples
- mbrtoc16() in C/C++ with Examples