lower_bound == upper_bound

后端 未结 7 1028
陌清茗
陌清茗 2020-12-02 08:51

What does lower_bound mean. If I had to guess I would answer that this function returns the iterator at the last element that is less than the value asked for. But I see tha

7条回答
  •  佛祖请我去吃肉
    2020-12-02 09:10

    Another usage of lower_bound and upper_bound is to find a range of equal elements in a container, e.g.

    std::vector data = { 1, 1, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6 };
    
    auto lower = std::lower_bound(data.begin(), data.end(), 4);
    auto upper = std::upper_bound(lower, data.end(), 4);
    
    std::copy(lower, upper, std::ostream_iterator(std::cout, " "));
    

提交回复
热议问题