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

📄 sea_9743.htm

📁 ARM编辑、编译软件
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD><TITLE>Searching Operations</TITLE></HEAD>
<BODY>
<A HREF="ug.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the user guide home page.</STRONG></P>
<P>&copy;Copyright 1996 Rogue Wave Software</P>
<H2>Searching Operations</H2>
<P>The next category of algorithms we will describe are those that are used to locate elements within a sequence that satisfy certain properties.  Most commonly the result of a search is then used as an argument to a further operation, such as a <SAMP>copy </SAMP>(<i><a href="inp_4704.htm#partitionasequenceintotwogroups">Chapter 13: Partition a Sequence into Two Groups</a></i>), a <SAMP>partition</SAMP> (<i><a href="ini_5794.htm#copyonesequenceintoanothersequence">Chapter 13: Copy One Sequence into Another Sequence</a></i>) or an <SAMP>in-place merge</SAMP> (<i><a href="inp_4704.htm#mergetwoadjacentsequencesintoone">Chapter 13: Merge two Adjacent Sequences into One</a></i>)</P>
<A HREF="sidebar.htm#sidebar56"><IMG SRC="images/note.gif" BORDER=0> <STRONG>Obtaining the Source</STRONG></A>

<P>The searching routines described in this section return an iterator that identifies the first element that satisfies the search condition. It is common to store this value in an iterator variable, as follows:</P>
<PRE>   list&#60;int>::iterator where;
   where = find(aList.begin(), aList.end(), 7);
</PRE>
<P>If you want to locate <I>all</I> the elements that satisfy the search conditions you must write a loop.  In that loop, the value yielded by a previous search is first advanced (since otherwise the value yielded by the previous search would once again be returned), and the resulting value is used as a starting point for the new search.   For example, the following loop from the <SAMP>adjacent_find()</SAMP> example program (<i><a href="sea_9743.htm#findconsecutiveduplicateelements">Chapter 13: Find Consecutive Duplicate Elements</a></i>) will print the value of all repeated characters in a string argument.</P>

<A HREF="sidebar.htm#sidebar57"><IMG SRC="images/note.gif" BORDER=0> <STRONG>Check Search Results</STRONG></A>

<PRE>   while ((where = adjacent_find(where, stop)) != stop) {
      cout &#60;&#60; "double " &#60;&#60; *where &#60;&#60; " in position " 
         &#60;&#60; where - start &#60;&#60; endl;
      ++where;
      }
</PRE>
<P>Many of the searching algorithms have an optional argument that can specify a function to be used to compare elements, in place of the equality operator for the container element type (operator <SAMP>==</SAMP>).  In the descriptions of the algorithms we write these optional arguments inside a square bracket, to indicate they need not be specified if the standard equality operator is acceptable.</P>

<A NAME="findanelementsatisfyingacondition"><H3>Find an Element Satisfying a Condition</H3></A>
<P>There are two algorithms, <SAMP>find()</SAMP> and <SAMP>find_if(),</SAMP> that are used to find the first element that satisfies a condition.  The declarations of these two algorithms are as follows:</P>
<PRE>InputIterator find_if (InputIterator first, InputIterator last, 
      Predicate);

InputIterator find (InputIterator first, InputIterator last, 
      const T&#38;);
</PRE>
<P>The algorithm <SAMP>find_if()</SAMP> takes as argument a predicate function, which can be any function that returns a boolean value (<i>see <a href="pre_1157.htm">Chapter 3</a></i>).  The <SAMP>find_if()</SAMP> algorithm returns a new iterator that designates the first element in the sequence that satisfies the predicate.  The second argument, the past-the-end iterator, is returned if no element is found that matches the requirement.  Because the resulting value is an iterator, the dereference operator (the <SAMP>*</SAMP> operator) must be used to obtain the matching value.  This is illustrated in the example program.</P>
<P>The second form of the algorithm, <SAMP>find(),</SAMP> replaces the predicate function with a specific value, and returns the first element in the sequence that tests equal to this value, using the appropriate equality operator (the <SAMP>==</SAMP> operator) for the given data type.</P>

<A HREF="sidebar.htm#sidebar58"><IMG SRC="images/note.gif" BORDER=0> <STRONG>Searching Sets and Maps</STRONG></A>

<P>The following example program illustrates the use of these algorithms:</P>
<PRE>void find_test ()
   // illustrate the use of the find algorithm
{
   int vintageYears[] = {1967, 1972, 1974, 1980, 1995};
   int * start = vintageYears;
   int * stop = start + 5;
   int * where = find_if (start, stop, isLeapYear);

   if (where != stop)
      cout &#60;&#60; "first vintage leap year is " &#60;&#60; *where &#60;&#60; endl;
   else
      cout &#60;&#60; "no vintage leap years" &#60;&#60; endl;

   where = find(start, stop, 1995);

   if (where != stop)
      cout &#60;&#60; "1995 is position " &#60;&#60; where - start 
         &#60;&#60; " in sequence" &#60;&#60; endl;
   else
      cout "1995 does not occur in sequence" &#60;&#60; endl;
}</PRE>
<A NAME="findconsecutiveduplicateelements"><H3>Find Consecutive Duplicate Elements</H3></A>
<P>The <SAMP>adjacent_find()</SAMP> algorithm is used to discover the first element in a sequence equal to the next immediately following element.  For example, if a sequence contained the values 1 4 2 5 6 6 7 5, the algorithm would return an iterator corresponding to the first 6 value.  If no value satisfying the condition is found, then the end-of-sequence iterator is returned.  The declaration of the algorithm is as follows:</P>
<PRE>ForwardIterator adjacent_find (ForwardIterator first, 
   ForwardIterator last [, BinaryPredicate ] );</PRE>
<!--ASQ-1 The style "teletype gap" is not associated; its content follows: --><P>The first two arguments specify the sequence to be examined. The optional third argument must be a binary predicate (a binary function returning a boolean value).  If present, the binary function is used to test adjacent elements, otherwise the equality operator (operator <SAMP>==</SAMP>) is used.</P>
<P>The example program searches a text string for adjacent letters.  In the example text these are found in positions 5, 7, 9, 21 and 37.  The increment is necessary inside the loop in order to avoid the same position being discovered repeatedly.</P>
<PRE>void adjacent_find_example ()
   // illustrate the use of the adjacent_find instruction
{
   char * text = "The bookkeeper carefully opened the door.";</PRE>
<!--ASQ-1 The style "teletype gap" is not associated; its content follows: --><PRE>   char * start = text;
   char * stop = text + strlen(text);
   char * where = start;

   cout &#60;&#60; "In the text: " &#60;&#60; text &#60;&#60; endl;
   while ((where = adjacent_find(where, stop)) != stop) {
      cout &#60;&#60; "double " &#60;&#60; *where 
         &#60;&#60; " in position " &#60;&#60; where - start &#60;&#60; endl;
      ++where;
   }
}
</PRE>
<A NAME="findasubsequencewithinasequence"><H3>Find a Subsequence within a Sequence</H3></A>
<P>The algorithm <SAMP>search()</SAMP> is used to locate the beginning of a particular subsequence within a larger sequence.  The easiest example to understand is the problem of looking for a particular substring within a larger string, although the algorithm can be generalized to other uses.  The arguments are assumed to have at least the capabilities of forward iterators.</P>
<PRE>ForwardIterator search 
   (ForwardIterator first1, ForwardIterator last1, 
   ForwardIterator first2, ForwardIterator last2 
   [, BinaryPredicate ]);
</PRE>

<A HREF="sidebar.htm#sidebar59"><IMG SRC="images/note.gif" BORDER=0> <STRONG>Speed of Search</STRONG></A>

<P>Suppose, for example, that we wish to discover the location of the string <SAMP>"ration"</SAMP> in the string <SAMP>"dreams and aspirations".</SAMP>  The solution to this problem is shown in the example program.  If no appropriate match is found, the value returned is the past-the-end iterator for the first sequence.</P>
<PRE>void search_example ()
      // illustrate the use of the search algorithm
{
   char * base = "dreams and aspirations";

⌨️ 快捷键说明

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