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

后端 未结 5 1888
伪装坚强ぢ
伪装坚强ぢ 2020-12-16 10:54

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

U

5条回答
  •  情歌与酒
    2020-12-16 11:30

    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;
    

提交回复
热议问题