C++ Programming Code Examples
C++ > Visual C++ 5.0 Standard C++ Library Code Examples
Algorithm mismatch - Search two sequences for a mismatched item.
Algorithm mismatch - Search two sequences for a mismatched item.
mismatch
Header
<algorithm>
template<class InputIterator1, class InputIterator2>
pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2)
Search two sequences for a mismatched item. Compares corresponding pairs of elements from two ranges and returns the first mismatched pair.
The algorithm finds the first position at which a value in range [first1, last1) disagrees with the value in the range starting at first2. It returns a pair of iterators i and j such that i points into the range [first1, last1), and j points into the range beginning at first2. i and j should be equidistant from the beginning of their corresponding ranges.
The non-predicate version uses operator== for comparison.
template<class InputIterator1, class InputIterator2, BinaryPredicate pr>
pair<InputIterator1, InputIterator2> mismatch(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pr)
Search two sequences for a mismatched item. Compares corresponding pairs of elements from two ranges and returns the first mismatched pair.
The algorithm finds the first position at which a value in range [first1, last1) disagrees with the value in the range starting at first2. It returns a pair of iterators i and j such that i points into the range [first1, last1), and j points into the range beginning at first2. i and j should be equidistant from the beginning of their corresponding ranges.
The predicate version uses predicate function pr for comparison.
Samples
#pragma warning (disable :4786)
#include <algorithm>
#include <iostream>
#include <string>
#include <utility>
#include <functional>
int main()
{
int set1[5] = {10, 15, 20, 25, 30} ;
int set2[4] = {10, 15, 25, 30} ;
std::ostream_iterator<int> intOstreamIt(std::cout, ", ") ;
std::cout << "set1 = " ;
std::copy(set1, set1+5, intOstreamIt) ;
std::cout << std::endl ;
std::cout << "set2 = " ;
std::copy(set2, set2+4, intOstreamIt) ;
std::cout << std::endl ;
std::pair<int*, int*> pi ;
//non-predicate version of mismatch
std::cout << "\n\nusing non-predicate version of mismatch" << std::endl ;
pi = std::mismatch(set1, set1+5, set2) ;
std::cout << "first mismatched element in set1 = " << *(pi.first) << std::endl ;
std::cout << "first mismatched element in set2 = " << *(pi.second) << std::endl ;
//predicate version of mismatch
std::cout << "\n\nusing predicate version of mismatch" << std::endl ;
pi = std::mismatch(set1, set1+5, set2, std::equal_to<int>()) ;
std::cout << "first mismatched element in set1 = " << *(pi.first) << std::endl ;
std::cout << "first mismatched element in set2 = " << *(pi.second) << std::endl ;
return 0 ;
}
Program Output
set1 = 10, 15, 20, 25, 30,
set2 = 10, 15, 25, 30,
using non-predicate version of mismatch
first mismatched element in set1 = 20
first mismatched element in set2 = 25
using predicate version of mismatch
first mismatched element in set1 = 20
first mismatched element in set2 = 25
Iterators are just like pointers used to access the container elements. Iterators are one of the four pillars of the Standard Template Library or STL in C++. An iterator is used to point to the memory address of the STL container classes. For better understanding, you can relate them with a pointer, to some extent. Iterators act as a bridge that connects algorithms to STL containers and allows the modifications of the data present inside the container. They allow you to iterate over the container, access and assign the values, and run different operators over them, to get the desired result. • Iterators are used to traverse from one element to another element, a process is known as iterating through the container. • The main advantage of an iterator is to provide a common interface for all the containers type. • Iterators make the algorithm independent of the type of the container used.
Return first position where two ranges differ. Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns the first element of both sequences that does not match. 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. The elements are compared using operator== (or pred, in version (2)). The function shall not modify any of its arguments. This can either be a function pointer or a function object. Function returns a pair, where its members first and second point to the first element in both sequences that did not compare equal to each other.
Templates are powerful features of C++ which allows us to write generic programs. Similar to function templates, we can use class templates to create a single class to work with different data types. Class templates come in handy as they can make our code shorter and more manageable. A class template starts with the keyword template followed by template parameter(s) inside <> which is followed by the class declaration. T is the template argument which is a placeholder for the data type used, and class is a keyword. Inside the class body, a member variable var and a member function functionName() are both of type T.
Ostream iterators are output iterators that write sequentially to an output stream (such as cout). They are constructed from a basic_ostream object, to which they become associated, so that whenever an assignment operator (=) is used on the ostream_iterator (dereferenced or not) it inserts a new element into the stream. Optionally, a delimiter can be specified on construction. This delimiter is written to the stream after each element is inserted.
In C++, pair is defined as a container in a header library <utility> which combines the two data elements having either the same data types or different data types. In general, the pair in C++ is defined as a tuple in Python programming language which also can give the output as a combined result of joining the two items specified by the pair container and it consists of the first element will be first and the second element will be second only it cannot be disturbed in the order or sequence of elements specified and are always accessed by the dot operator followed by the keyword "first" and "second" elements respectively. In C++ the pair is a container in <utility> header and is also a container class in STL (Standard Template Library) which uses "std" namespace so it will be as std::pair template class for demonstrating pair as a tuple.
copy() function is used to copy items from one iterator to another iterator with a specific range. We can define the start and end position of the source and it will copy all items in this rage to a different destination. To use copy() function, we need to include <bits/stdc+.h> or header file. It copies all the elements pointed by first and last. first element is included in the output but last is not. output is the start position of the final result iterator. It returns one iterator to the end of the destination range where elements have been copied. Function returns an iterator to the end of the destination range where elements have been copied.
A C++ template is a powerful feature added to C++. It allows you to define the generic classes and generic functions and thus provides support for generic programming. Generic programming is a technique where generic types are used as parameters in algorithms so that they can work for a variety of data types. We can define a template for a function. For example, if we have an add() function, we can create versions of the add function for adding the int, float or double type values. Where Ttype: It is a placeholder name for a data type used by the function. It is used within the function definition. It is only a placeholder that the compiler will automatically replace this placeholder with the actual data type. class: A class keyword is used to specify a generic type in a template declaration.
A program shall contain a global function named main, which is the designated start of the program in hosted environment. main() function is the entry point of any C++ program. It is the point at which execution of program is started. When a C++ program is executed, the execution control goes directly to the main() function. Every C++ program have a main() function.
#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C/C++ program. This directive is read by the preprocessor and orders it to insert the content of a user-defined or system header file into the following program. These files are mainly imported from an outside source into the current program. The process of importing such files that might be system-defined or user-defined is known as File Inclusion. This type of preprocessor directive tells the compiler to include a file in the source code program.
Function object class for equality comparison. Binary function object class whose call returns whether its two arguments compare equal (as returned by operator ==). equal_to is a function object class for equality comparison and binary function object class whose call returns whether its two arguments compare equal (as returned by operator ==). Generically, function objects are instances of a class with member function operator() defined. This member function allows the object to be used with the same syntax as a function call.