📄 all.html
字号:
count_if </div> <div class="syntax-name-format"> Syntax: </div> <pre class="syntax-box"> #include <algorithm> 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<int>(), target_value) ); cout << "nums[] contains " << num_items << " items matching " << target_value << 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 <algorithm> 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<int> v1; for( int i = 0; i < 10; i++ ) { v1.push_back( i ); } vector<int> v2; for( int i = 0; i < 10; i++ ) { v2.push_back( i ); } if( equal( v1.begin(), v1.end(), v2.begin() ) ) { cout << "v1 and v2 are equal" << endl; } else { cout << "v1 and v2 are NOT equal" << 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 <algorithm> pair<iterator,iterator> equal_range( iterator first, iterator last, const <a href="../containers.html">TYPE</a>& val ); pair<iterator,iterator> equal_range( iterator first, iterator last, const <a href="../containers.html">TYPE</a>& 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 < 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<int> 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<vector<int>::iterator, vector<int>::iterator> result; int new_val = 8; result = equal_range( nums.begin(), nums.end(), new_val ); cout << "The first place that " << new_val << " could be inserted is before " << *result.first << ", and the last place that it could be inserted is before " << *result.second << 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 <algorithm> #include <algorithm> void fill( iterator start, iterator end, const <a href="../containers.html">TYPE</a>& 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<int> v1; for( int i = 0; i < 10; i++ ) { v1.push_back( i ); } cout << "Before, v1 is: "; for( unsigned int i = 0; i < v1.size(); i++ ) { cout << v1[i] << " "; } cout << endl; fill( v1.begin(), v1.end(), -1 ); cout << "After, v1 is: "; for( unsigned int i = 0; i < v1.size(); i++ ) { cout << v1[i] << " "; } cout << 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 <algorithm> #include <algorithm> iterator fill_n( iterator start, size_t n, const <a href="../containers.html">TYPE</a>& 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<int> v1; for( int i = 0; i < 10; i++ ) { v1.push_back( i ); } cout << "Before, v1 is: "; for( unsigned int i = 0; i < v1.size(); i++ ) { cout << v1[i] << " "; } cout << endl; fill_n( v1.begin(), v1.size()/2, -1 ); cout << "After, v1 is: "; for( unsigned int i = 0; i < v1.size(); i++ ) { cout << v1[i] << " "; } cout << 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 <algorithm> iterator find( iterator start, iterator end, const <a href="../containers.html">TYPE</a>& 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<int> v1; for( int i = 0; i < 10; i++ ) { v1.push_back(i); } vector<int>::iterator result; result = find( v1.begin(), v1.end(), num_to_find ); if( result == v1.end() ) { cout << "Did not find any element matching " << num_to_find << endl; } else { cout << "Found a matching element: " << *result << 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 << "Did not find any number matching " << num_to_find << endl; } else { cout << "Found a matching number: " << *result << 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 <algorithm> 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 "1 2 3" 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 << "Did not find any subsequence matching { 1, 2, 3 }" << endl; } else { cout << "The last matching subsequence is at: " << *result << endl; } int target2[] = { 3, 2, 3 }; result = find_end( nums + start, nums + end, target2 + 0, target2 + 2 ); if( *result == nums[end] ) { cout << "Did not find any subsequence matching { 3, 2, 3 }" << endl; } else { cout << "The last matching subsequence is at: " << *result << 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 <algorithm> 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 + -