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

📄 all.html

📁 从www.CppReference.com打包的C++参考手册
💻 HTML
📖 第 1 页 / 共 5 页
字号:
    count_if  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;algorithm&gt;  size_t count_if( iterator start, iterator end, UnaryPred p );</pre>  <p>The count_if() function returns the number of elements between  <em>start</em> and <em>end</em> for which the predicate <em>p</em>  returns true.</p>  <p>For example, the following code uses count_if() with a predicate  that returns true for the integer 3 to count the number of items in  an array that are equal to 3:</p>  <pre class="example-code"> int nums[] = { 0, 1, 2, 3, 4, 5, 9, 3, 13 }; int start = 0; int end = 9;            int target_value = 3; int num_items = count_if( nums+start,                    nums+end,                    bind2nd(equal_to&lt;int&gt;(), target_value) );              cout &lt;&lt; &quot;nums[] contains &quot; &lt;&lt; num_items &lt;&lt; &quot; items matching &quot; &lt;&lt; target_value &lt;&lt; endl;               </pre>  <p>When run, the above code displays the following output:</p>  <pre class="example-code"> nums[] contains 2 items matching 3             </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="count.html">count</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    equal  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;algorithm&gt;  bool equal( iterator start1, iterator end1, iterator start2 );  bool equal( iterator start1, iterator end1, iterator start2, BinPred p );</pre>  <p>The equal() function returns true if the elements in two ranges  are the same. The first range of elements are those between  <em>start1</em> and <em>end1</em>. The second range of elements has  the same size as the first range but starts at <em>start2</em>.</p>  <p>If the binary predicate <em>p</em> is specified, then it is used  instead of == to compare each pair of elements.</p>  <p>For example, the following code uses equal() to compare two  vectors of integers:</p>  <pre class="example-code"> vector&lt;int&gt; v1; for( int i = 0; i &lt; 10; i++ ) {   v1.push_back( i ); }               vector&lt;int&gt; v2; for( int i = 0; i &lt; 10; i++ ) {   v2.push_back( i ); }               if( equal( v1.begin(), v1.end(), v2.begin() ) ) {   cout &lt;&lt; &quot;v1 and v2 are equal&quot; &lt;&lt; endl; } else {   cout &lt;&lt; &quot;v1 and v2 are NOT equal&quot; &lt;&lt; endl; }              </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="find_if.html">find_if</a><br>    <a href=    "lexicographical_compare.html">lexicographical_compare</a><br>    <a href="mismatch.html">mismatch</a><br>    <a href="search.html">search</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    equal_range  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;algorithm&gt;  pair&lt;iterator,iterator&gt; equal_range( iterator first, iterator last, const <a href="../containers.html">TYPE</a>&amp; val );  pair&lt;iterator,iterator&gt; equal_range( iterator first, iterator last, const <a href="../containers.html">TYPE</a>&amp; val, CompFn comp );</pre>  <p>The equal_range() function returns the range of elements between  <em>first</em> and <em>last</em> that are equal to <em>val</em>. This  function assumes that the elements between <em>first</em> and  <em>last</em> are in order according to <em>comp</em>, if it is  specified, or the &lt; operator otherwise.</p>  <p>equal_range() can be thought of as a combination of the <a href=  "lower_bound.html">lower_bound</a>() and `upper_bound1`() functions,  since the first of the pair of iterators that it returns is what  <a href="lower_bound.html">lower_bound</a>() returns and the second  iterator in the pair is what `upper_bound1`() returns.</p>  <p>For example, the following code uses equal_range() to determine  all of the possible places that the number 8 can be inserted into an  ordered vector of integers such that the existing ordering is  preserved:</p>  <pre class="example-code"> vector&lt;int&gt; nums; nums.push_back( -242 ); nums.push_back( -1 ); nums.push_back( 0 ); nums.push_back( 5 ); nums.push_back( 8 ); nums.push_back( 8 ); nums.push_back( 11 );           pair&lt;vector&lt;int&gt;::iterator, vector&lt;int&gt;::iterator&gt; result; int new_val = 8;                result = equal_range( nums.begin(), nums.end(), new_val );              cout &lt;&lt; &quot;The first place that &quot; &lt;&lt; new_val &lt;&lt; &quot; could be inserted is before &quot;      &lt;&lt; *result.first &lt;&lt; &quot;, and the last place that it could be inserted is before &quot;      &lt;&lt; *result.second &lt;&lt; endl;            </pre>  <p>The above code produces the following output:</p>  <pre class="example-code"> The first place that 8 could be inserted is before 8, and the last place that it could be inserted is before 11              </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="binary_search.html">binary_search</a><br>    <a href="lower_bound.html">lower_bound</a><br>    <a href="upper_bound.html">upper_bound</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    fill  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;algorithm&gt;  #include &lt;algorithm&gt;  void fill( iterator start, iterator end, const <a href="../containers.html">TYPE</a>&amp; val );</pre>  <p>The function fill() assigns <em>val</em> to all of the elements  between <em>start</em> and <em>end</em>.</p>  <p>For example, the following code uses fill() to set all of the  elements of a vector of integers to -1:</p>  <pre class="example-code"> vector&lt;int&gt; v1; for( int i = 0; i &lt; 10; i++ ) {   v1.push_back( i ); }               cout &lt;&lt; &quot;Before, v1 is: &quot;; for( unsigned int i = 0; i &lt; v1.size(); i++ ) {   cout &lt;&lt; v1[i] &lt;&lt; &quot; &quot;; } cout &lt;&lt; endl;             fill( v1.begin(), v1.end(), -1 );               cout &lt;&lt; &quot;After, v1 is: &quot;; for( unsigned int i = 0; i &lt; v1.size(); i++ ) {   cout &lt;&lt; v1[i] &lt;&lt; &quot; &quot;; } cout &lt;&lt; endl;            </pre>  <p>When run, the above code displays:</p>  <pre class="example-code"> Before, v1 is: 0 1 2 3 4 5 6 7 8 9 After, v1 is: -1 -1 -1 -1 -1 -1 -1 -1 -1 -1            </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="fill_n.html">fill_n</a><br>    <a href="generate.html">generate</a><br>    <a href="transform.html">transform</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    fill_n  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;algorithm&gt;  #include &lt;algorithm&gt;  iterator fill_n( iterator start, size_t n, const <a href="../containers.html">TYPE</a>&amp; val );</pre>  <p>The fill_n() function is similar to (C++ I/O) <a href=  "../cppio/fill.html">fill</a>(). Instead of assigning <em>val</em> to  a range of elements, however, fill_n() assigns <em>val</em> to the  first <em>n</em> elements starting at <em>start</em>.</p>  <p>For example, the following code uses fill_n() to assign -1 to the  first half of a vector of integers:</p>  <pre class="example-code"> vector&lt;int&gt; v1; for( int i = 0; i &lt; 10; i++ ) {   v1.push_back( i ); }               cout &lt;&lt; &quot;Before, v1 is: &quot;; for( unsigned int i = 0; i &lt; v1.size(); i++ ) {   cout &lt;&lt; v1[i] &lt;&lt; &quot; &quot;; } cout &lt;&lt; endl;             fill_n( v1.begin(), v1.size()/2, -1 );          cout &lt;&lt; &quot;After, v1 is: &quot;; for( unsigned int i = 0; i &lt; v1.size(); i++ ) {   cout &lt;&lt; v1[i] &lt;&lt; &quot; &quot;; } cout &lt;&lt; endl;            </pre>  <p>When run, this code displays:</p>  <pre class="example-code"> Before, v1 is: 0 1 2 3 4 5 6 7 8 9 After, v1 is: -1 -1 -1 -1 -1 5 6 7 8 9         </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="fill.html">fill</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    find  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;algorithm&gt;  iterator find( iterator start, iterator end, const <a href="../containers.html">TYPE</a>&amp; val );</pre>  <p>The find() algorithm looks for an element matching <em>val</em>  between <em>start</em> and <em>end</em>. If an element matching  <em>val</em> is found, the return value is an iterator that points to  that element. Otherwise, the return value is an iterator that points  to <em>end</em>.</p>  <p>For example, the following code uses find() to search a vector of  integers for the number 3:</p>  <pre class="example-code"> int num_to_find = 3;            vector&lt;int&gt; v1; for( int i = 0; i &lt; 10; i++ ) {   v1.push_back(i); }               vector&lt;int&gt;::iterator result; result = find( v1.begin(), v1.end(), num_to_find );             if( result == v1.end() ) {   cout &lt;&lt; &quot;Did not find any element matching &quot; &lt;&lt; num_to_find &lt;&lt; endl; }               else {   cout &lt;&lt; &quot;Found a matching element: &quot; &lt;&lt; *result &lt;&lt; endl; }              </pre>  <p>In the next example, shown below, the find() function is used on  an array of integers. This example shows how the C++ Algorithms can  be used to manipulate arrays and pointers in the same manner that  they manipulate containers and iterators:</p>  <pre class="example-code"> int nums[] = { 3, 1, 4, 1, 5, 9 }; int num_to_find = 5; int start = 0; int end = 2; int* result = find( nums + start, nums + end, num_to_find );                 if( result == nums + end ) {   cout &lt;&lt; &quot;Did not find any number matching &quot; &lt;&lt; num_to_find &lt;&lt; endl; } else {   cout &lt;&lt; &quot;Found a matching number: &quot; &lt;&lt; *result &lt;&lt; endl; }              </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="adjacent_find.html">adjacent_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="mismatch.html">mismatch</a><br>    <a href="search.html">search</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    find_end  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;algorithm&gt;  iterator find_end( iterator start, iterator end, iterator seq_start, iterator seq_end );  iterator find_end( iterator start, iterator end, iterator seq_start, iterator seq_end, BinPred bp );</pre>  <p>The find_end() function searches for the sequence of elements  denoted by <em>seq_start</em> and <em>seq_end</em>. If such a  sequence if found between <em>start</em> and <em>end</em>, an  iterator to the first element of the last found sequence is returned.  If no such sequence is found, an iterator pointing to <em>end</em> is  returned.</p>  <p>If the binary predicate <em>bp</em> is specified, then it is used  to when elements match.</p>  <p>For example, the following code uses find_end() to search for two  different sequences of numbers. The the first chunk of code, the last  occurence of &quot;1 2 3&quot; is found. In the second chunk of code,  the sequence that is being searched for is not found:</p>  <pre class="example-code"> int nums[] = { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 }; int* result; int start = 0; int end = 11;           int target1[] = { 1, 2, 3 }; result = find_end( nums + start, nums + end, target1 + 0, target1 + 2 ); if( *result == nums[end] ) {   cout &lt;&lt; &quot;Did not find any subsequence matching { 1, 2, 3 }&quot; &lt;&lt; endl; } else {   cout &lt;&lt; &quot;The last matching subsequence is at: &quot; &lt;&lt; *result &lt;&lt; endl; }               int target2[] = { 3, 2, 3 }; result = find_end( nums + start, nums + end, target2 + 0, target2 + 2 ); if( *result == nums[end] ) {   cout &lt;&lt; &quot;Did not find any subsequence matching { 3, 2, 3 }&quot; &lt;&lt; endl; } else {   cout &lt;&lt; &quot;The last matching subsequence is at: &quot; &lt;&lt; *result &lt;&lt; endl; }              </pre>  <div class="related-name-format">    Related topics:  </div>  <div class="related-content">    <a href="adjacent_find.html">adjacent_find</a><br>    <a href="find.html">find</a><br>    <a href="find_first_of.html">find_first_of</a><br>    <a href="find_if.html">find_if</a><br>    <a href="search_n.html">search_n</a>  </div>  </div>  </td>    </tr>  </table></body></html><hr>  <div class="name-format">    find_first_of  </div>  <div class="syntax-name-format">    Syntax:  </div>  <pre class="syntax-box">  #include &lt;algorithm&gt;  iterator find_first_of( iterator start, iterator end, iterator find_start, iterator find_end );  iterator find_first_of( iterator start, iterator end, iterator find_start, iterator find_end, BinPred bp );</pre>  <p>The find_first_of() function searches for the first occurence of  any element between <em>find_start</em> and <em>find_end</em>. The  data that are searched are those between <em>start</em> and  <em>end</em>.</p>  <p>If any element between <em>find_start</em> and <em>find_end</em>  is found, an iterator pointing to that element is returned.  Otherwise, an iterator pointing to <em>end</em> is returned.</p>  <p>For example, the following code searches for a 9, 4, or 7 in an  array of integers:</p>

⌨️ 快捷键说明

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