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 that lower_bound is almost the same as upper_bound. The only difference is strict inequality in the case of upper_bound. Is there a true lower bound selection function in stl that agrees with the normal definition of lower bound.
EDIT: It was too many negations in the docs which made me confused. The problem was that I got the same iterator. I solved it by subtracting 1 from lower_bound return value. I use it 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; }
I am sorry for this confusion :-(
The lower_bound() method in C++ is used to return an iterator pointing to the first element in the range [first, last) which has a value not less than val. This means that the function returns an iterator pointing to the next smallest number just greater than or equal to that number.
std::lower_boundReturns an iterator pointing to the first element in the range [first,last) which does not compare less than val . The elements are compared using operator< for the first version, and comp for the second.
upper_bound() is a standard library function in C++ defined in the header . It returns an iterator pointing to the first element in the range [first, last) that is greater than value, or last if no such element is found. The elements in the range shall already be sorted or at least partitioned with respect to val.
Upper bound and Lower bound for non increasing vector in C++ In a Vector, lower bound returns an iterator pointing to the first element in the range that does not compare the given value. Upper Bound returns an iterator pointing element in the range that smaller than given value.
Lower bound: first element that is greater-or-equal.
Upper bound: first element that is strictly greater.
Example:
+- 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) -|
As you can see, the half-open equal-range for n is [lb(n), ub(n)).
Note that both bounds give you meaningful insertion locations for an element of the desired value so that the ordering is maintained, but lower_bound
has the distinguishing feature that if the element already exists, then you get an iterator which actually points to that element. Thus you can use lower_bound
on an ordered range to implement your own unique-membership or multiple-membership container.
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); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With