public member function
std::multimap::equal_range
<map>
pair<iterator,iterator>
equal_range ( const key_type& x );
pair<const_iterator,const_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.
If
x does not match any key in the container, the range returned has a length of zero, with both iterators pointing to the element with nearest key greater than
x, if any, or to
multimap::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 multimap containers as an alias of Key, which is the first template parameter and the type of the keys for 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).
Both
iterator and
const_iterator are member types. In the
multimap class template, these are
bidirectional iterators.
Dereferencing any of these iterators accesses the element's value, which is of type
pair<const Key,T>.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
// multimap::equal_elements
#include <iostream>
#include <map>
using namespace std;
int main ()
{
multimap<char,int> mymm;
multimap<char,int>::iterator it;
pair<multimap<char,int>::iterator,multimap<char,int>::iterator> ret;
mymm.insert(pair<char,int>('a',10));
mymm.insert(pair<char,int>('b',20));
mymm.insert(pair<char,int>('b',30));
mymm.insert(pair<char,int>('b',40));
mymm.insert(pair<char,int>('c',50));
mymm.insert(pair<char,int>('c',60));
mymm.insert(pair<char,int>('d',60));
cout << "mymm contains:\n";
for (char ch='a'; ch<='d'; ch++)
{
cout << ch << " =>";
ret = mymm.equal_range(ch);
for (it=ret.first; it!=ret.second; ++it)
cout << " " << (*it).second;
cout << endl;
}
return 0;
}
|
mymm contains:
a => 10
b => 20 30 40
c => 50 60
d => 60
|
Complexity
Logarithmic in
size.
See also
- multimap::count
- Count elements with a specific key (public member function)
- multimap::lower_bound
- Return iterator to lower bound (public member function)
- multimap::upper_bound
- Return iterator to upper bound (public member function)
- multimap::find
- Get iterator to element (public member function)