C++ STL algorithm, mismatch() program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the C++ mismatch() to compare two ranges element by element either for equality or equivalent in a sense specified by a binary predicate and locates the first position where a difference occurs in C++ programming

To show: How to use the C++ algorithm, mismatch() to compare two ranges element by element either for equality or equivalent in a sense specified by a binary predicate and locates the first position where a difference occurs in C++ programming

 

// C++ STL algorithm, mismatch()

#include <vector>

#include <list>

#include <algorithm>

#include <iostream>

using namespace std;

 

// return whether second element is twice the first

bool twice(int elem1, int elem2)

{ return ((elem1 * 2) == elem2);}

 

int main(void)

{

// containers

vector <int> vec1, vec2;

list <int> lst;

// iterators

vector <int>::iterator Iter1, Iter2;

list <int>::iterator lst_Iter, lst_inIter;

int i, j, k;

 

// pushing data in ranges

for(i = 0; i <= 5; i++)

vec1.push_back(5*i);

for(j = 0; j <= 7; j++)

lst.push_back(5*j);

for(k = 0; k <= 5; k++)

vec2.push_back(10*k);

 

// print the data

cout<<"vec1 vector data: ";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

cout<<"lst list data: ";

for(lst_Iter = lst.begin(); lst_Iter!= lst.end(); lst_Iter++)

cout<<*lst_Iter<<" ";

cout<<endl;

cout<<"vec2 vector data: ";

for(Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)

cout<<*Iter2<<" ";

cout<<endl;

// testing vec1 vector and lst list for mismatch under identity

pair<vector <int>::iterator, list <int>::iterator> result1;

result1 = mismatch(vec1.begin(), vec1.end(), lst.begin());

cout<<"\nOperation: mismatch(vec1.begin(), vec1.end(), lst.begin())"<<endl;

if(result1.first == vec1.end())

cout<<"The two ranges do not differ."<<endl;

else

cout<<"The fist mismatch is between "<<*result1.first<<" and "<<*result1.second<<endl;

// modifying the lst list

cout<<"\nDo some operation on the lst list..."<<endl;

lst_inIter = lst.begin();

lst_inIter++;

lst_inIter++;

lst.insert(lst_inIter, 70);

// print the data

cout<<"The modified lst list data: ";

for(lst_Iter = lst.begin(); lst_Iter!= lst.end(); lst_Iter++)

cout<<*lst_Iter<<" ";

cout<<endl;

// testing vec1 vector with modified lst list for mismatch under identity

cout<<"\nOperation: mismatch(vec1.begin(), vec1.end(), lst.begin())"<<endl;

result1 = mismatch(vec1.begin(), vec1.end(), lst.begin());

if(result1.first == vec1.end())

cout<<"The two ranges do not differ"<<endl;

else

cout<<"The first mismatch is between "<<*result1.first<<" and "<<*result1.second<< endl;

// test vec1 and vec2 vectors for mismatch under the binary predicate twice

pair<vector <int>::iterator, vector <int>::iterator> result2;

cout<<"\nOperation: mismatch(vec1.begin(), vec1.end(), vec2.begin(), twice)"<<endl;

result2 = mismatch(vec1.begin(), vec1.end(), vec2.begin(), twice);

if(result2.first == vec1.end())

cout<<"The two ranges do not differ based on the binary predicate twice()"<<endl;

else

cout<<"The first mismatch is between "<<*result2.first<<" and "<<*result2.second<<endl;

return 0;

}

 

Output examples:

 

vec1 vector data: 0 5 10 15 20 25

lst list data: 0 5 10 15 20 25 30 35

vec2 vector data: 0 10 20 30 40 50

Operation: mismatch(vec1.begin(), vec1.end(), lst.begin())

The two ranges do not differ.

Do some operation on the lst list...

The modified lst list data: 0 5 70 10 15 20 25 30 35

Operation: mismatch(vec1.begin(), vec1.end(), lst.begin())

The first mismatch is between 10 and 70

Operation: mismatch(vec1.begin(), vec1.end(), vec2.begin(), twice)

The two ranges do not differ based on the binary predicate twice()

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index