kal
kal

Reputation: 29381

Returning the greatest key strictly less than the given key in a C++ Map

Is there a way the C++ STL Maps support this, since lower_bound and upper_bound on maps strictly return the value greater than the passed value.

Lower key

Use case I have a map with times as keys in a sorted manner so in a MAP

time t1   = value1
time t2   = value2
time t2.5 = value3

In this case if I pass to this MAP t2.3 then it should give me value2. Does doing a lower_bound on the map and going back one element equivalent to the "returning greatest key strictly less than given key" i.e

iterator = map.upper_bound(2.3)
and then 
iterator--;

Upvotes: 39

Views: 23737

Answers (5)

Jimmy
Jimmy

Reputation: 11

I always find to compute floor(x) and ceil(x) to be patricularly useful

floor(x) -> greatest element <=x 
ceil(x) -> smallest element >= x

floor_it = m.lower_bound(x); 
if (floor_it != m.end() && floor_it->first != x)
      --floor_it;

ceil_it = m.upper_bound(x);
if (ceil_it != m.end() && (ceil_it-1)->first == x)
    --ceil_it; 

Upvotes: -2

Johannes Schaub - litb
Johannes Schaub - litb

Reputation: 507005

Yeah, lower_bound can be used for that, i've seen it before and used it like that.

map_type::iterator it = map.lower_bound(2.3);
if(it != map.begin()) {
    --it;
    // it now points at the right element
}

Would actually return the greatest, yet smaller (if it != map.begin() was true) one. If it was at .begin, then there is no smaller key. Nice idea from the comments is to return .end if there is no element that's less and pack this stuff into a function:

template<typename Map> typename Map::const_iterator 
greatest_less(Map const& m, typename Map::key_type const& k) {
    typename Map::const_iterator it = m.lower_bound(k);
    if(it != m.begin()) {
        return --it;
    }
    return m.end();
}

template<typename Map> typename Map::iterator 
greatest_less(Map & m, typename Map::key_type const& k) {
    typename Map::iterator it = m.lower_bound(k);
    if(it != m.begin()) {
        return --it;
    }
    return m.end();
}

The template should work for std::set too.

Upvotes: 35

KenE
KenE

Reputation: 1805

If you don't care about the ordering, you can use map::upper_bound to get the element you want, but you need to define the map class with std::greater for the traits argument, so that the ordering is high-to-low.

typedef std::map<double,MyClass,std::greater<double> > MyMap;
MyMap myMap;
myMap[1.5] = value1;
myMap[2.0] = value2;
myMap[3.0] = value3;

MyMap::iterator elt = myMap.upper_bound(2.5); // should be pair(2.0,value2)

Upvotes: 9

Mark Ransom
Mark Ransom

Reputation: 308206

Your method will work, but you need to check to make sure you don't back over the beginning of the map. Plus you need lower_bound, not upper_bound.

iterator = map.lower_bound(2.3)
if (iterator != map.begin())
    --iterator;

Upvotes: 4

Rob K
Rob K

Reputation: 8926

I would use std::find_if() with reverse iterators, something like:

find_if( map.rbegin(), map.rend(), is_less_than );

You'll have to define the is_less_than() predicate function.

(cf. http://www.cplusplus.com/reference/algorithm/find_if.html)

Upvotes: 1

Related Questions