map::upper_bound
public member function
iterator upper_bound ( const key_type& x );
const_iterator upper_bound ( const key_type& x ) const;
Return iterator to upper bound
Returns an iterator pointing to the first element in the container whose key compares greater than x (using the container's comparison object).
Unlike lower_bound, this member function does not return an iterator to the element if its key compares equal to x, but only if it compares strictly greater.
Notice that, internally, all the elements in a map container are always ordered by their keys following the criterion defined by its comparison object, therefore all the elements that follow the one returned by this function will also compare greater than x.
Parameters
- x
- Key value to be compared.
key_type is a member type defined in map 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
An iterator to the the first element in the container whose key compares greater than x.
Both iterator and const_iterator are member types. In the map class template, these are bidirectional iterators.
Dereferencing this iterator 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
|
// map::lower_bound/upper_bound
#include <iostream>
#include <map>
using namespace std;
int main ()
{
map<char,int> mymap;
map<char,int>::iterator it,itlow,itup;
mymap['a']=20;
mymap['b']=40;
mymap['c']=60;
mymap['d']=80;
mymap['e']=100;
itlow=mymap.lower_bound ('b'); // itlow points to b
itup=mymap.upper_bound ('d'); // itup points to e (not d)
mymap.erase(itlow,itup); // erases [itlow,itup)
// print content:
for ( it=mymap.begin() ; it != mymap.end(); it++ )
cout << (*it).first << " => " << (*it).second << endl;
return 0;
}
|
Complexity
Logarithmic in size.
See also
map::find | Get iterator to element (public member function) |
map::count | Count elements with a specific key (public member function) |
|