This C++ program demonstrates the mismatch algorithm. The mismatch algorithm used takes three arguments – the first two arguments are iterators to beginning and end of first container and the third argument is iterator to beginning of second container. The mismatch algorithm returns pair of iterator to the first mismatch of container elements.
Here is the source code of the C++ program demonstrates the mismatch algorithm. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C++ Program to demonstrate mismatch() algorithm
*/
#include <iostream>
#include <algorithm>
#include <vector>
void fun(int val)
{
std::cout << val << " ";
}
// Print vector routine
void print(std::vector<int> v)
{
for_each(v.begin(), v.end(), fun);
std::cout << std::endl;
}
int main()
{
std::vector <int> k(5), l(5);
typedef std::vector <int>::iterator i;
for (int i = 0; i < k.size(); i++)
{
k[i] = i;
if(i < 3)
l[i] = i;
}
std::pair <i, i> p = mismatch(k.begin(), k.end(), l.begin());
std::cout << "Vector k : ";
print(k);
std::cout << "Vector l : ";
print(l);
std::cout << "Mismatched indexes : " << (p.first - k.begin())
<< " " << (p.second - l.begin()) << std::endl;
std::cout << "Mismatched values : " << *(p.first)
<< " " << *(p.second) << std::endl;
}
$ a.out Vector k : 0 1 2 3 4 Vector l : 0 1 2 0 0 Mismatched indexes : 3 3 Mismatched values : 3 0
Sanfoundry Global Education & Learning Series – 1000 C++ Programs.
advertisement
advertisement
If you wish to look at all C++ Programming examples, go to C++ Programs.
If you find any mistake above, kindly email to [email protected]Related Posts:
- Practice Computer Science MCQs
- Check Programming Books
- Apply for Computer Science Internship
- Practice Programming MCQs
- Check C++ Books