C++ set lower_bound() Function
Example
Find the first number that is equal to or greater than 4:
set<int> numbers = {1, 7, 3, 2, 5, 9};
auto it = numbers.lower_bound(4);
cout << *it;
Definition and Usage
The lower_bound() function returns an iterator (a pointer-like object that points to an element) to the first element that is equal to or greater than the given value.
The elements in a set are sorted, so this is the smallest element that is not less than the value. In the example, 4 is not in the set, so the result is 5. If there is no such element, it returns end().
The lower_bound function is defined in the <set> header file.
Tip: Use upper_bound() to find the first element that is greater than (not equal to) the value.
Syntax
set.lower_bound(type value)
type is the type of the elements in the set, like int in the example.
Parameter Values
| Parameter | Description |
|---|---|
| value | Required. The value to compare with. |
Technical Details
| Returns: | An iterator pointing to the first element that is equal to or greater than value. If there is no such element, it returns set.end(). |
|---|
Related Pages
Read more about sets in our Sets Tutorial.