Searching a map with upper bound and lower bound

后端 未结 1 457
庸人自扰
庸人自扰 2021-02-12 11:42

STL newbie question:

Regarding functions std::map::upper_bound and std::map::lower_bound is it valid to specify a key that is not actually pres

1条回答
  •  醉话见心
    2021-02-12 12:00

    Yes, they are both valid.

    map::lower_bound returns an iterator pointing to the first element that is not less than key.

    map::upper_bound returns an iterator pointing to the first element that is greater than key.

    intmap[1]=10;
    intmap[2]=20;
    intmap[4]=40;   // <<---both lower_bound(3)/upper_bound(3) will points to here
    intmap[5]=50;
    

    lower_bound/upper_bound return the position where value would get inserted.

    Note, If you want to check the value key is map or not, you could use std::map::find

    0 讨论(0)
提交回复
热议问题