What exactly does lower bound imply?

If I had to guess, I'd say this function returns the iterator at the last element with a value less than the one requested.

However, I notice that lower bound is nearly identical to upper bound.

In the case of upper bound, the only difference is strict inequality.

Is there a true lower bound selection function in stl that agrees with the traditional definition of the term?

EDIT: I was perplexed by the docs because there were too many negations.

The issue was that I kept getting the same iterator.

By subtracting 1 from the lower bound return value, I was able to solve the problem.

It's what I use for interpolation:

float operator()(double f) { SpectrumPoint* l=std::lower_bound(beginGet(),endGet(),(SpectrumPoint){float(f),0.0f} ,SpectrumPoint::CompareFreqLessThan); if(l>beginGet()) {--l;} SpectrumPoint* u=std::lower_bound(beginGet(),endGet(),(SpectrumPoint){float(f),0.0f} ,SpectrumPoint::CompareFreqLessThan); if(u==endGet()) {u=beginGet();} if(l==u) { if(u==endGet()) {return u->amp;} return l->amp; } double f_min=l->freq; double A_min=l->amp; double f_max=u->freq; double A_max=u->amp; double delta_f=f_max-f_min; double delta_A=A_max-A_min; return A_min + delta_A*(f-f_min)/delta_f; }

Jun 6, 2022 in C++ 263 views

## 1 answer to this question.

Lower bound: the initial greater-or-equal element.

Upper bound: first element that is strictly greater.

```+- lb(2) == ub(2)       +- lb(6)        +- lb(8)
|        == begin()     |  == ub(6)     |   +- ub(8) == end()
V                       V               V   V
+---+---+---+---+---+---+---+---+---+---+---+
| 3 | 4 | 4 | 4 | 4 | 5 | 7 | 7 | 7 | 7 | 8 |
+---+---+---+---+---+---+---+---+---+---+---+
^               ^                       ^
|               |                       |
+- lb(4)        +- ub(4)                +- lb(9) == ub(9) == end()

|- eq-range(4) -|```

The half-open equal-range for n is [lb(n), ub(n)], as you can see.

Note that both limits provide appropriate insertion places for an element of the required value so that the ordering is preserved, but lower bound has the advantage of providing an iterator that really leads to the element if it already exists.

As a result, you may create your own unique-membership or multiple-membership container by using lower bound on an ordered range.

```void insert(Container & c, T const & t)
{
auto it = std::lower_bound(c.begin(), c.end(), t);

// if unique container:
if (it != c.end() && *it == t) { /* error, element exists! */ return; }

c.insert(it, t);
}```
• 4,960 points

## Difference between upper_bound and lower_bound in stl

I was looking at how the upper bound and lower bound algorithms operate in stl on these pages: lower bound, upper bound, and it's documented the same way on these pages: lower bound, upper bound, and it's documented the same way on these pages: lower bound, upper bound, and it'  upper bound, lower bound Looking at the code from the links, they appear to perform the same thing to me, with the exception of the following lines  lower_bound (line 10): if (*it<val) { ...READ MORE

## Difference between set.upper_bound() and upper_bound(set.begin(), set.end()) stl

I discovered that set.upper bound() was quicker ...READ MORE

## Syntax of priority queue

We must first include the queue header file in order to establish a priority queue in C++. #include <queue> Once we import this file, we ...READ MORE

## Sorting a vector of custom objects

A simple example using std::sort struct MyStruct { ...READ MORE

## What is the C++ function to raise a number to a power?

What's the best way to raise a n ...READ MORE