set::equal_range
public member function
pair<iterator,iterator> equal_range ( const key_type& x ) const;
Get range of equal elements
Returns the bounds of a range that includes all the elements in the container with a key that compares equal to x. In set containers, where no duplicate keys are allowed, the range will include one element at most.
If x does not match any key in the container, the range returned has a length of zero, with both iterators pointing to the nearest value greater than x, if any, or to set::end if x is greater than all the elements in the container.
Parameters
- x
- Key value to be compared.
key_type is a member type defined in set containers as an alias of Key, which is the first template parameter and the type of the elements stored in the container.
Return value
The function returns a pair, where its member pair::first is an iterator to the lower bound of the range with the same value as the one that would be returned by lower_bound(x), and pair::second is an iterator to the upper bound of the range with the same value as the one that would be returned by upper_bound(x).
iterator is a member type, defined in set as a bidirectional iterator type.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
// set::equal_elements
#include <iostream>
#include <set>
using namespace std;
int main ()
{
set<int> myset;
pair<set<int>::iterator,set<int>::iterator> ret;
for (int i=1; i<=5; i++) myset.insert(i*10); // set: 10 20 30 40 50
ret = myset.equal_range(30);
cout << "lower bound points to: " << *ret.first << endl;
cout << "upper bound points to: " << *ret.second << endl;
return 0;
}
|
lower bound points to: 30
upper bound points to: 40
|
Complexity
Logarithmic in size.
See also
set::count | Count elements with a specific key (public member function) |
set::find | Get iterator to element (public member function) |
|