public member function
<map>
std::multimap::lower_bound
iterator lower_bound (const key_type& k);
const_iterator lower_bound (const key_type& k) const;
Return iterator to lower bound
Returns an iterator pointing to the first element in the container whose key is not considered to go before k (i.e., either it is equivalent or goes after).
The function uses its internal comparison object (key_comp) to determine this, returning an iterator to the first element for which key_comp(element_key,k) would return false.
If the multimap class is instantiated with the default comparison type (less), the function returns an iterator to the first element whose key is not less than k.
A similar member function, upper_bound, has the same behavior as lower_bound, except in the case that the multimap contains elements with keys equivalent to k: In this case, lower_bound returns an iterator pointing to the first of such elements, whereas upper_bound returns an iterator pointing to the element following the last.
Parameters
- k
- Key to search for.
Member type key_type is the type of the elements in the container, defined in map as an alias of its first template parameter (Key).
Return value
An iterator to the the first element in the container whose key is not considered to go before k, or multimap::end if all keys are considered to go before k.
If the multimap object is const-qualified, the function returns a const_iterator. Otherwise, it returns an iterator.
Member types iterator and const_iterator are bidirectional iterator types pointing to elements (of type value_type).
Notice that value_type in multimap containers is itself also a pair type: pair<const key_type, mapped_type>.
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
|
// multimap::lower_bound/upper_bound
#include <iostream>
#include <map>
int main ()
{
std::multimap<char,int> mymultimap;
std::multimap<char,int>::iterator it,itlow,itup;
mymultimap.insert(std::make_pair('a',10));
mymultimap.insert(std::make_pair('b',121));
mymultimap.insert(std::make_pair('c',1001));
mymultimap.insert(std::make_pair('c',2002));
mymultimap.insert(std::make_pair('d',11011));
mymultimap.insert(std::make_pair('e',44));
itlow = mymultimap.lower_bound ('b'); // itlow points to b
itup = mymultimap.upper_bound ('d'); // itup points to e (not d)
// print range [itlow,itup):
for (it=itlow; it!=itup; ++it)
std::cout << (*it).first << " => " << (*it).second << '\n';
return 0;
}
| |
b => 121
c => 1001
c => 2002
d => 11011
|
Complexity
Logarithmic in size.
Iterator validity
No changes.
Data races
The container is accessed (neither the const nor the non-const versions modify the container).
No mapped values are accessed: concurrently accessing or modifying elements is safe.
Exception safety
Strong guarantee: if an exception is thrown, there are no changes in the container.