Simple Program interpretation of C ++ STL algorithm series 8: mismatch

Source: Internet
Author: User

 

C ++ STL's Non-mutating algorithms is a set of template functions that do not destroy operation data, it is used for processing sequence data one by one, element search, subsequence search, statistics, and matching.

The mismatch algorithm compares two sequences to find the location of the first unmatched element. It has the following two function prototypes to find out the first element * I in the iterator interval [first1, last1), and The iterator interval [first2, first2 + (last1-first1 )) the element * (first2 + (I-first1) on is not equal (or does not meet the binary predicate binary_pred condition ). Return the iterator of the two elements by matching the pair object, indicating the position of the element that does not match.

 

Function prototype:

 

 

Template <class InputIterator1, class InputIterator2>

Pair <InputIterator1, InputIterator2> mismatch (

InputIterator1 _ First1,

InputIterator1 _ Last1,

InputIterator2 _ First2

);

Template <class InputIterator1, class InputIterator2, class BinaryPredicate>

Pair <InputIterator1, InputIterator2> mismatch (

InputIterator1 _ First1,

InputIterator1 _ Last1,

InputIterator2 _ First2

BinaryPredicate _ Comp

);

 

Sample Code:

 

 

/*************************************** ****************************

* Copyright (C) Jerry Jiang

* File Name: mismatch. cpp

* Author: Jerry Jiang

* Create Time: 2011-10-9 21:16:53

* Mail: jbiaojerry@gmail.com

* Blog: http://blog.csdn.net/jerryjbiao

* Description: A simple program interpreting the 8 th of the C ++ STL algorithm series

* Non-variable algorithm: Element mismatch searches for mismatch

**************************************** **************************/

 

# Include <algorithm>

# Include <vector>

# Include <iostream>

 

Using namespace std;

 

Bool strEqual (const char * s1, const char * s2)

{

Return strcmp (s1, s2) = 0? True: false;

}

 

Typedef vector <int >:: iterator ivecIter;

 

Int main ()

{

Vector <int> ivec1, ivec2;

Ivec1.push _ back (2 );

Ivec1.push _ back (0 );

Ivec1.push _ back (1 );

Ivec1.push _ back (4 );

 

Ivec2.push _ back (2 );

Ivec2.push _ back (0 );

Ivec2.push _ back (1 );

Ivec2.push _ back (7 );

 

Pair <ivecIter, ivecIter> retCode;

RetCode = mismatch (ivec1.begin (), ivec1.end (), ivec2.begin ());

If (retCode. first = ivec1.end () & retCode. second = ivec2.begin ())

{

Cout <"ivec1 and ivec2 are identical" <endl;

}

Else

{

Cout <"ivec1 and ivec2 are different. The unmatched elements are: \ n"

<* RetCode. first <endl

<* RetCode. second <endl;

}

 

 

Char * str1 [] = {"appple", "pear", "watermelon", "banana", "grape "};

Char * str2 [] = {"appple", "pears", "watermelons", "banana", "grape "};

 

Pair <char **, char **> retCode2 = mismatch (str1, str1 + 5, str2, strEqual );

If (retCode2.first = str1 + 5 & retCode2.second = str2 + 5)

{

Cout <"str1 and str2 are identical" <endl;

}

Else

{

Cout <"str1 and str2 are different. The unmatched string is:" <endl

<Str1 [retCode2.first-str1] <endl

<Str2 [retCode2.second-str2] <endl;

}

 

 

Return 0;

}

From: Jerry Jiang's program life

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.