c++ - why std::max_element over std::map within some lower_bound not working? -
i not sure why following snippet not working: lower_bound
call return key 7, expected. std::max_element
between begin()
, lower_bound()
iterator should return 6, should search between key 4 , key 7 , max value 6 key 7. it's returning last pair(15, 12)
reason can't figure out.
bool cmp(const std::pair<t, t>& p1, const std::pair<t, t>& p2) { return p1.second < p2.second; } int main() { std::map< t, t, std::greater<t> > store; std::map< t, t, std::greater<t> >::iterator found_max, lower; store[ 4 ] = 2; store[ 7 ] = 6; store[ 10 ] = 2; store[ 15 ] = 12; lower = store.lower_bound( 8 ); printf("%ld %ld\n", lower->first,lower->second); found_max = std::max_element(store.begin(), lower, cmp); printf("%ld %ld\n", found_max->first,found_max->second); return 0; }
std::map< t, t, std::greater<t> > store;
store
has key sorted in descending order. thus, [store.begin(), lower)
contains (15, 22)
, (10, 2)
. element maximum value (15, 22)
.
Comments
Post a Comment