📄 mis_8857.htm
字号:
<HTML><TITLE>mismatch</TITLE><BODY>
<A HREF="ref.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the Class Reference home page.</STRONG></P>
<P>©Copyright 1996 Rogue Wave Software</P>
<H2>mismatch</H2>
<HR><PRE> Algorithm</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>Compares elements from two sequences and returns the first two elements that don't match each other.</P>
<H3>Contents</H3>
<UL>
<A HREF="#Synopsis"><LI>Synopsis</LI></A>
<A HREF="#Description"><LI>Description</LI></A>
<A HREF="#Complexity"><LI>Complexity</LI></A>
<A HREF="#Example"><LI>Example</LI></A>
<A HREF="#Warning"><LI>Warning</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include <algorithm></PRE>
<PRE>
template <class InputIterator1, class InputIterator2>
pair<InputIterator1,InputIterator2>
<B>mismatch</B>(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2);
template <class InputIterator1, class InputIterator2,
class BinaryPredicate>
pair<InputIterator1, Inputiterator2>
<B>mismatch</B>(InputIterator first1, InputIterator1 last1,
InputIterator2 first2,
BinaryPredicate binary_pred);
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>The <B><I>mismatch</B></I> algorithm compares members of two sequences and returns<B><I> </B></I>two iterators (<SAMP>i</SAMP> and <SAMP>j</SAMP>) that point to the first location in each sequence where the sequences differ from each other. Notice that the algorithm denotes both a starting position and an ending position for the first sequence, but denotes only a starting position for the second sequence. <B><I>mismatch</B></I> assumes that the second sequence has at least as many members as the first sequence. If the two sequences are identical, <B><I>mismatch</B></I> returns a pair of iterators that point to the end of the first sequence and the corresponding location at which the comparison stopped in the second sequence.</P>
<P>The first version of <B><I>mismatch</B></I> checks members of a sequence for equality, while the second version lets you specify a comparison function. The comparison function must be a binary predicate.</P>
<P>The iterators <SAMP>i</SAMP> and <SAMP>j</SAMP> returned by <B><I>mismatch</B></I> are defined as follows:</P>
<PRE>j == first2 + (i - first1) </PRE>
<PRE></PRE><P>and <SAMP>i</SAMP> is the first iterator in the range <SAMP>[first1, last1)</SAMP> for which the appropriate one of the following conditions hold: </P>
<PRE>!(*i == *(first2 + (i - first1)))
</PRE>
<P>or</P>
<PRE>binary_pred(*i, *(first2 + (i - first1))) == false</PRE>
<PRE></PRE><P>If all of the members in the two sequences match, <B><I>mismatch</B></I> returns a pair of <SAMP>last1</SAMP> and <SAMP>first2 + (last1 - first1)</SAMP>. </P>
<A NAME="Complexity"><H3>Complexity</H3></A>
<P>At most <SAMP>last1 - first1</SAMP> applications of the corresponding predicate are done.</P>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// mismatch.cpp
//
#include <algorithm>
#include <vector>
#include <iostream.h>
int main(void)
{
typedef vector<int>::iterator iterator;
int d1[4] = {1,2,3,4};
int d2[4] = {1,3,2,4};
// Set up two vectors
vector<int> vi1(d1,d1 + 4), vi2(d2,d2 + 4);
// p1 will contain two iterators that point to the
// first pair of elements that are different between
// the two vectors
pair<iterator, iterator> p1 = <B>mismatch</B>(vi1.begin(), vi1.end(),
vi2.begin());
// find the first two elements such that an element in the
// first vector is greater than the element in the second
// vector.
pair<iterator, iterator> p2 = <B>mismatch</B>(vi1.begin(), vi1.end(),
vi2.begin(),
less_equal<int>());
// Output results
cout << *p1.first << ", " << *p1.second << endl;
cout << *p2.first << ", " << *p2.second << endl;
return 0;
}</PRE><PRE>Output :
2, 3
3, 2
</PRE>
<A NAME="Warning"><H3>Warning</H3></A>
<P>If your compiler does not support default template parameters, then you need to always supply the <SAMP>Allocator</SAMP> template argument. For instance, you will need to write :</P>
<P><SAMP>vector<int, allocator></SAMP></P>
<P>instead of: </P>
<P><SAMP>vector<int></SAMP></P>
<HR>
<A HREF="min_5058.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="mod_6765.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -