⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mis_8857.htm

📁 ARM编辑、编译软件
💻 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>&copy;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 &#60;algorithm></PRE>
<PRE>
template &#60;class InputIterator1, class InputIterator2>
  pair&#60;InputIterator1,InputIterator2>
  <B>mismatch</B>(InputIterator1 first1, InputIterator1 last1,
           InputIterator2 first2);
template &#60;class InputIterator1, class InputIterator2,
          class BinaryPredicate>
  pair&#60;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 &#60;algorithm>
 #include &#60;vector>
 #include &#60;iostream.h>
 int main(void)
 {
   typedef vector&#60;int>::iterator  iterator;
   int d1[4] = {1,2,3,4};
   int d2[4] = {1,3,2,4};
   // Set up two vectors
   vector&#60;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&#60;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&#60;iterator, iterator> p2 = <B>mismatch</B>(vi1.begin(), vi1.end(),
                                        vi2.begin(),
                                        less_equal&#60;int>());
   // Output results
   cout &#60;&#60; *p1.first &#60;&#60; ", " &#60;&#60; *p1.second &#60;&#60; endl;
   cout &#60;&#60; *p2.first &#60;&#60; ", " &#60;&#60; *p2.second &#60;&#60; 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&#60;int, allocator></SAMP></P>
<P>instead of: </P>
<P><SAMP>vector&#60;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 + -