📄 equal_range.html
字号:
<!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>equal_range</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> > <a href= "index.html">C++ Algorithms</a> > <a href= "equal_range.html">equal_range</a> </div> <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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -