c++stl

Naming of lower_bound, upper_bound c++


Does any one know why they where given these names? Comming from a maths backgound they always left my mind in tangles since they are both mathematical lower bounds i.e. minimums in the finite world. Also the natural language definition given in the stl is a bad mental model imo.

Does anyone use mental synonyms to be able to work with them, or do they just remember the naïve implementations?

lower_bound(rng, x) = get_iter_to(mathematical_lower_bound(rng | filter([](auto y)
                                  {return x<=y;}))
upper_bound(rng, x) = get_iter_to(mathematical_lower_bound(rng | filter([](auto y)
                                  {return x<y;})))


Solution

  • This example from std::ranges::lower_bound (link) is more helpful

    std::vector data = { 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5 };
     
        auto lower = ranges::lower_bound(data, 4);
        auto upper = ranges::upper_bound(data, 4);
     
        ranges::copy(lower, upper, std::ostream_iterator<int>(std::cout, " "));
     
        std::cout << '\n'; // 4 4 4 4