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

📄 sea_9743.htm

📁 ARM编辑、编译软件
💻 HTM
📖 第 1 页 / 共 2 页
字号:
   char * text = "ration";

   char * where = search(base, base + strlen(base), 
         text, text + strlen(text));

   if (*where != '\0')
      cout << "substring position: " << where - base << endl;
   else
      cout << "substring does not occur in text" << endl;
}
</PRE>
<P>Note that this algorithm, unlike many that manipulate two sequences, uses a starting and ending iterator pair for both sequences, not just the first sequence.</P>
<P>Like the algorithms <SAMP>equal()</SAMP> and <SAMP>mismatch(),</SAMP> an alternative version of <SAMP>search()</SAMP> takes an optional binary predicate that is used to compare elements from the two sequences.</P>

<A NAME="locatemaximumorminimumelement"><H3>Locate Maximum or Minimum Element</H3></A>
<P>The functions <SAMP>max()</SAMP> and <SAMP>min()</SAMP> can be used to find the maximum and minimum of a pair of values.  These can optionally take a third argument that defines the comparison function to use in place of the less-than operator (operator <SAMP>&#60;</SAMP>).  The arguments are values, not iterators:</P>
<PRE>template &#60;class T> 
   const T&#38; max(const T&#38; a, const T&#38; b [, Compare ] );
template &#60;class T> 
   const T&#38; min(const T&#38; a, const T&#38; b [, Compare ] );
</PRE>
<P>The maximum and minimum functions are generalized to entire sequences by the generic algorithms <SAMP>max_element()</SAMP> and <SAMP>min_element().</SAMP> For these functions the arguments are input iterators.</P>
<PRE>ForwardIterator max_element (ForwardIterator first, 
      ForwardIterator last [, Compare ] );
ForwardIterator min_element (ForwardIterator first, 
      ForwardIterator last [, Compare ] );
</PRE>

<A HREF="sidebar.htm#sidebar60"><IMG SRC="images/note.gif" BORDER=0> <STRONG>Largest and Smallest Elements of a Set</STRONG></A>

<P>These algorithms return an iterator that denotes the largest or smallest of the values in a sequence, respectively.  Should more than one value satisfy the requirement, the result yielded is the first satisfactory value.  Both algorithms can optionally take a third argument, which is the function to be used as the comparison operator in place of the default operator.</P>
<P>The example program illustrates several uses of these algorithms.  The function named <SAMP>split()</SAMP> used to divide a string into words in the string example is described in <a href="exa_5786.htm">Chapter 19 (<i>Example Program of auto_ptr</i>)</a>.  The function <SAMP>randomInteger() </SAMP>is described in <a href="var_0565.htm#randomaccessiterators">Chapter 2 (<i>Random Access Iterators</i>)</a>.</P>
<PRE>void max_min_example ()
   // illustrate use of max_element and min_element algorithms
{
   // make a vector of random numbers between 0 and 99
   vector&#60;int> numbers(25);
   for (int i = 0; i &#60; 25; i++)
      numbers[i] = randomInteger(100);

   // print the maximum
   vector&#60;int>::iterator max = 
      max_element(numbers.begin(), numbers.end());
   cout &#60;&#60; "largest value was " &#60;&#60; * max &#60;&#60; endl;

   // example using strings
   string text = 
      "It was the best of times, it was the worst of times.";
   list&#60;string> words;
   split (text, " .,!:;", words);
   cout &#60;&#60; "The smallest word is " 
         &#60;&#60; * min_element(words.begin(), words.end())
         &#60;&#60; " and the largest word is "
         &#60;&#60; * max_element(words.begin(), words.end())
         &#60;&#60; endl;
}
</PRE>

<A NAME="locatethefirstmismatchedelementsinparallelsequences"><H3>Locate the First Mismatched Elements in Parallel Sequences</H3></A>
<P>The name <SAMP>mismatch()</SAMP>  might lead you to think this algorithm was the inverse of the <SAMP>equal()</SAMP> algorithm, which determines if two sequences are equal (<i>see <a href="sca_1926.htm#generalizedinnerproduct">Chapter 13: Generalized Inner Product</a></i>).  Instead, the <SAMP>mismatch()</SAMP> algorithm returns a <B><I>pair</I></B><B><I> </I></B>of iterators that together indicate the first positions where two parallel sequences have differing elements.  (The structure <A HREF="../stdref/pai_5818.htm"><B><I>pair</I></B></A> is described in <a href="map_9326.htm">Chapter 9: <i>The Map Data Abstraction</i></a>).  The second sequence is denoted only by a starting position, without an ending position.  It is assumed (but not checked) that the second sequence contains at least as many elements as the first.  The arguments and return type for <SAMP>mismatch()</SAMP> can be described as follows:</P>
<PRE>pair&#60;InputIterator, InputIterator> mismatch 
   (InputIterator first1, InputIterator last1, 
      InputIterator first2 [, BinaryPredicate ] );</PRE>
<!--ASQ-1 The style "teletype gap" is not associated; its content follows: --><P>The elements of the two sequences are examined in parallel, element by element.  When a mismatch is found, that is, a point where the two sequences differ, then a <B><I>pair</I></B> containing iterators denoting the locations of the two differing elements is constructed and returned.  If the first sequence becomes exhausted before discovering any mismatched elements, then the resulting pair contains the ending value for the first sequence, and the last value examined in the second sequence.  (The second sequence need not yet be exhausted).</P>
<P>The example program illustrates the use of this procedure.  The function <SAMP>mismatch_test()</SAMP> takes as arguments two string values.  These are lexicographically compared and a message printed indicating their relative ordering. (This is similar to the analysis performed by the <SAMP>lexicographic_compare()</SAMP> algorithm, although that function simply returns a boolean value.) Because the <SAMP>mismatch()</SAMP> algorithm assumes the second sequence is at least as long as the first, a comparison of the two string lengths is performed first, and the arguments are reversed if the second string is shorter than the first.  After the call on <SAMP>mismatch()</SAMP> the elements of the resulting pair are separated into their component parts.  These parts are then tested to determine the appropriate ordering.</P>
<PRE>   void mismatch_test (char * a, char * b)
          // illustrate the use of the mismatch algorithm
   {
      pair&#60;char *, char *> differPositions(0, 0);
      char * aDiffPosition;
      char * bDiffPosition;</PRE>
<!--ASQ-1 The style "teletype gap" is not associated; its content follows: --><PRE>      if (strlen(a) &#60; strlen(b)) {
         // make sure longer string is second
         differPositions = mismatch(a, a + strlen(a), b);
         aDiffPosition = differPositions.first;
         bDiffPosition = differPositions.second;
         }
      else {
         differPositions = mismatch(b, b + strlen(b), a);
         // note following reverse ordering
         aDiffPosition = differPositions.second;
         bDiffPosition = differPositions.first;
         }</PRE><!--ASQ-1 The style "teletype gap" is not associated; its content follows: --><PRE>      // compare resulting values
      cout &#60;&#60; "string " &#60;&#60; a;
      if (*aDiffPosition == *bDiffPosition)
         cout &#60;&#60; " is equal to ";
      else if (*aDiffPosition &#60; *bDiffPosition)
         cout &#60;&#60; " is less than ";
      else
         cout &#60;&#60; " is greater than ";
      cout &#60;&#60; b &#60;&#60; endl;
   }
</PRE><P>A second form of the <SAMP>mismatch()</SAMP> algorithm is similar to the one illustrated, except it accepts a binary predicate as a fourth argument.  This binary function is used to compare elements, in place of the <SAMP>==</SAMP> operator.</P>

<HR>
<A HREF="ini_5794.htm"><IMG SRC="images/prev.gif"></A> <A HREF="booktoc.htm"><IMG SRC="images/toc.gif"></A> <A HREF="inp_4704.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -