lower bound upper bound

0 votes

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++ by Nicholas
• 7,760 points
1,045 views

1 answer to this question.

0 votes

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);
}
answered Jun 21, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

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

Jul 5, 2022 in C++ by Nicholas
• 7,760 points
1,118 views
0 votes
0 answers

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

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

Jun 29, 2022 in C++ by Damon
• 4,960 points
436 views
0 votes
1 answer

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

answered May 31, 2022 in C++ by Damon
• 4,960 points
610 views
0 votes
1 answer

Sorting a vector of custom objects

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

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,264 views
0 votes
0 answers

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

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

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
517 views
0 votes
1 answer

Comparison of C++ STL collections and C# collections

This is what I know Array - C ...READ MORE

answered Jun 9, 2022 in C# by rajiv
• 1,620 points
640 views
0 votes
1 answer

How do i apply lower_bound to a range of unsorted vector elements?

What's the point of sorting your array? ...READ MORE

answered Jun 15, 2022 in C++ by Damon
• 4,960 points
1,764 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP