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

📄 all.html

📁 从www.CppReference.com打包的C++参考手册
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head>  <meta name="generator" content=  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">  <title>C++ Algorithms</title>  <link href="../cppreference.css" rel="stylesheet" type="text/css"></head><body><table>  <tr>  <td>  <div class="body-content">  <div class="header-box">    <a href="../index.html">cppreference.com</a> &gt; <a href=    "index.html">C++ Algorithms</a>  </div>  <div class="name-format">    accumulate  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;numeric&gt;  <a href="../containers.html">TYPE</a> accumulate( iterator start, iterator end, <a href="../containers.html">TYPE</a> val );  <a href="../containers.html">TYPE</a> accumulate( iterator start, iterator end, <a href="../containers.html">TYPE</a> val, BinaryFunction f );</pre>  <p>The accummulate() function computes the sum of <em>val</em> and  all of the elements in the range [<em>start</em>,<em>end</em>).</p>  <p>If the binary function <em>f</em> if specified, it is used instead  of the + operator to perform the summation.</p>  <p>accumulate() runs in <a href="../complexity.html">linear  time</a>.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="adjacent_difference.html">adjacent_difference</a><br>    <a href="count.html">count</a><br>    <a href="inner_product.html">inner_product</a><br>    <a href="partial_sum.html">partial_sum</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    adjacent_difference  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;numeric&gt;  iterator adjacent_difference( iterator start, iterator end, iterator result );  iterator adjacent_difference( iterator start, iterator end, iterator result, BinaryFunction f );</pre>  <p>The adjacent_difference() function calculates the differences  between adjacent elements in the range [<em>start</em>,<em>end</em>)  and stores the result starting at <em>result</em>.</p>  <p>If a binary function <em>f</em> is given, it is used instead of  the - operator to compute the differences.</p>  <p>adjacent_difference() runs in <a href="../complexity.html">linear  time</a>.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="accumulate.html">accumulate</a><br>    <a href="count.html">count</a><br>    <a href="inner_product.html">inner_product</a><br>    <a href="partial_sum.html">partial_sum</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    adjacent_find  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;algorithm&gt;  iterator adjacent_find( iterator start, iterator end );  iterator adjacent_find( iterator start, iterator end, BinPred pr );</pre>  <p>The adjacent_find() function searches between <em>start</em> and  <em>end</em> for two consecutive identical elements. If the binary  predicate <em>pr</em> is specified, then it is used to test whether  two elements are the same or not.</p>  <p>The return value is an iterator that points to the first of the  two elements that are found. If no matching elements are found, the  returned iterator points to <em>end</em>.</p>  <p>For example, the following code creates a vector containing the  integers between 0 and 10 with 7 appearing twice in a row.  adjacent_find() is then used to find the location of the pair of  7&#39;s:</p>  <pre class="example-code"> vector&lt;int&gt; v1; for( int i = 0; i &lt; 10; i++ ) {   v1.push_back(i);   // add a duplicate 7 into v1   if( i == 7 ) {     v1.push_back(i);              } }               vector&lt;int&gt;::iterator result; result = adjacent_find( v1.begin(), v1.end() );                 if( result == v1.end() ) {   cout &lt;&lt; &quot;Did not find adjacent elements in v1&quot; &lt;&lt; endl; }               else {   cout &lt;&lt; &quot;Found matching adjacent elements starting at &quot; &lt;&lt; *result &lt;&lt; endl; }              </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="find.html">find</a><br>    <a href="find_end.html">find_end</a><br>    <a href="find_first_of.html">find_first_of</a><br>    <a href="find_if.html">find_if</a><br>    <a href="unique.html">unique</a><br>    <a href="unique_copy.html">unique_copy</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    binary_search  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;algorithm&gt;  bool binary_search( iterator start, iterator end, const <a href="../containers.html">TYPE</a>&amp; val );  bool binary_search( iterator start, iterator end, const <a href="../containers.html">TYPE</a>&amp; val, Comp f );</pre>  <p>The binary_search() function searches from <em>start</em> to  <em>end</em> for <em>val</em>. The elements between <em>start</em>  and <em>end</em> that are searched should be in ascending order as  defined by the &lt; operator. Note that a binary search <strong>will  not work</strong> unless the elements being searched are in  order.</p>  <p>If <em>val</em> is found, binary_search() returns true, otherwise  false.</p>  <p>If the function <em>f</em> is specified, then it is used to  compare elements.</p>  <p>For example, the following code uses binary_search() to determine  if the integers 0-9 are in an array of integers:</p>  <pre class="example-code"> int nums[] = { -242, -1, 0, 5, 8, 9, 11 }; int start = 0; int end = 7;            for( int i = 0; i &lt; 10; i++ ) {   if( binary_search( nums+start, nums+end, i ) ) {     cout &lt;&lt; &quot;nums[] contains &quot; &lt;&lt; i &lt;&lt; endl;   } else {     cout &lt;&lt; &quot;nums[] DOES NOT contain &quot; &lt;&lt; i &lt;&lt; endl;   } }              </pre>  <p>When run, this code displays the following output:</p>  <pre class="example-code"> nums[] contains 0 nums[] DOES NOT contain 1 nums[] DOES NOT contain 2 nums[] DOES NOT contain 3 nums[] DOES NOT contain 4 nums[] contains 5 nums[] DOES NOT contain 6 nums[] DOES NOT contain 7 nums[] contains 8 nums[] contains 9              </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="equal_range.html">equal_range</a><br>    <a href="is_sorted.html">is_sorted</a><br>    <a href="lower_bound.html">lower_bound</a><br>    <a href="partial_sort.html">partial_sort</a><br>    <a href="partial_sort_copy.html">partial_sort_copy</a><br>    <a href="sort.html">sort</a><br>    <a href="stable_sort.html">stable_sort</a><br>    <a href="upper_bound.html">upper_bound</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    copy  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;algorithm&gt;  iterator copy( iterator start, iterator end, iterator dest );</pre>  <p>The copy() function copies the elements between <em>start</em> and  <em>end</em> to <em>dest</em>. In other words, after copy() has  run,</p>  <pre class="example-code"> *dest == *start *(dest+1) == *(start+1) *(dest+2) == *(start+2) ... *(dest+N) == *(start+N)                </pre>  <p>The return value is an iterator to the last element copied. copy()  runs in <a href="../complexity.html">linear time</a>.</p>  <p>For example, the following code uses copy() to copy the contents  of one vector to another:</p>  <pre class="example-code"> vector&lt;int&gt; from_vector; for( int i = 0; i &lt; 10; i++ ) {   from_vector.push_back( i ); }               vector&lt;int&gt; to_vector(10);                copy( from_vector.begin(), from_vector.end(), to_vector.begin() );              cout &lt;&lt; &quot;to_vector contains: &quot;; for( unsigned int i = 0; i &lt; to_vector.size(); i++ ) {   cout &lt;&lt; to_vector[i] &lt;&lt; &quot; &quot;; } cout &lt;&lt; endl;            </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="copy_backward.html">copy_backward</a><br>    <a href="copy_n.html">copy_n</a><br>    <a href="generate.html">generate</a><br>    <a href="remove_copy.html">remove_copy</a><br>    <a href="swap.html">swap</a><br>    <a href="transform.html">transform</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    copy_backward  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;algorithm&gt;  iterator copy_backward( iterator start, iterator end, iterator dest );</pre>  <p>copy_backward() is similar to (C++ Strings) <a href=  "../cppstring/copy.html">copy</a>(), in that both functions copy  elements from <em>start</em> to <em>end</em> to <em>dest</em>. The  copy_backward() function , however, starts depositing elements at  <em>dest</em> and then works backwards, such that:</p>  <pre class="example-code"> *(dest-1) == *(end-1) *(dest-2) == *(end-2) *(dest-3) == *(end-3) ... *(dest-N) == *(end-N)          </pre>  <p>The following code uses copy_backward() to copy 10 integers into  the end of an empty vector:</p>  <pre class="example-code"> vector&lt;int&gt; from_vector; for( int i = 0; i &lt; 10; i++ ) {   from_vector.push_back( i ); }               vector&lt;int&gt; to_vector(15);                copy_backward( from_vector.begin(), from_vector.end(), to_vector.end() );               cout &lt;&lt; &quot;to_vector contains: &quot;; for( unsigned int i = 0; i &lt; to_vector.size(); i++ ) {   cout &lt;&lt; to_vector[i] &lt;&lt; &quot; &quot;; } cout &lt;&lt; endl;            </pre>  <p>The above code produces the following output:</p>  <pre class="example-code"> to_vector contains: 0 0 0 0 0 0 1 2 3 4 5 6 7 8 9              </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="copy.html">copy</a><br>    <a href="copy_n.html">copy_n</a><br>    <a href="swap.html">swap</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    copy_n  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;algorithm&gt;  iterator copy_n( iterator from, size_t num, iterator to ); </pre>  <p>The copy_n() function copies <em>num</em> elements starting at  <em>from</em> to the destination pointed at by <em>to</em>. To put it  another way, copy_n() performs <em>num</em> assignments and  duplicates a subrange.</p>  <p>The return value of copy_n() is an iterator that points to the  last element that was copied, i.e. (to + num).</p>  <p>This function runs in <a href="../complexity.html">linear  time</a>.</p>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="copy.html">copy</a><br>    <a href="copy_backward.html">copy_backward</a><br>    <a href="swap.html">swap</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    count  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;algorithm&gt;  size_t count( iterator start, iterator end, const <a href="../containers.html">TYPE</a>&amp; val );</pre>  <p>The count() function returns the number of elements between  <em>start</em> and <em>end</em> that match <em>val</em>.</p>  <p>For example, the following code uses count() to determine how many  integers in a vector match a target value:</p>  <pre class="example-code"> vector&lt;int&gt; v; for( int i = 0; i &lt; 10; i++ ) {   v.push_back( i ); }               int target_value = 3; int num_items = count( v.begin(), v.end(), target_value );              cout &lt;&lt; &quot;v contains &quot; &lt;&lt; num_items &lt;&lt; &quot; items matching &quot; &lt;&lt; target_value &lt;&lt; endl;            </pre>  <p>The above code displays the following output:</p>  <pre class="example-code"> v contains 1 items matching 3          </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="accumulate.html">accumulate</a><br>    <a href="adjacent_difference.html">adjacent_difference</a><br>    <a href="count_if.html">count_if</a><br>    <a href="inner_product.html">inner_product</a><br>    <a href="partial_sum.html">partial_sum</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">

⌨️ 快捷键说明

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