C++ Codes
Algorithms
Algorithm Analysis in C++
Beginners
Code Snippets
Graphics
Data Structures
File Manipulation
Games
Mathematics
Miscellaneous
Visual C++ Library
C++ > Visual C++ 5.0 Standard C++ Library sample source codes
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
Privacy Policy
|
Link to Us
|
Links