The iterator returned by std::ranges::lower_bound()
is significant because it points to the first element in the range that is not less than the specified value. This makes it useful for various operations in sorted containers.
lower_bound()
IteratorIf the element is found, lower_bound()
returns an iterator to the element. If not, it returns an iterator to the first element greater than the value or the end of the container.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> Numbers{1, 2, 3, 4, 5};
auto it = std::ranges::lower_bound(Numbers, 3);
if (it != Numbers.end() && *it == 3) {
std::cout << "Element found: " << *it;
} else {
std::cout << "Element not found";
}
}
Element found: 3
If you want to insert a new element while maintaining the sorted order, lower_bound()
gives you the exact position:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> Numbers{1, 2, 3, 5};
int newElement = 4;
auto it = std::ranges::lower_bound(
Numbers, newElement);
Numbers.insert(it, newElement);
for (const auto& num : Numbers) {
std::cout << num << " ";
}
}
1 2 3 4 5
lower_bound()
is also useful for range queries when combined with upper_bound()
. Together, they can find all elements equal to a certain value.
lower_bound()
in Practicelower_bound()
points to it.lower_bound()
points to where it would be inserted.Always check if the returned iterator is the end of the container before dereferencing it to avoid errors.
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> Numbers{1, 2, 3, 5};
int searchValue = 6;
auto it = std::ranges::lower_bound(
Numbers, searchValue);
if (it != Numbers.end()) {
std::cout << "The number " << searchValue
<< " would be in position "
<< std::distance(Numbers.begin(), it);
} else {
std::cout << "The number " << searchValue
<< " is greater than all elements\n";
}
}
The number 6 is greater than all elements
The iterator from std::ranges::lower_bound()
provides a powerful way to manage sorted containers, facilitating efficient searches, insertions, and range-based operations.
Answers to questions are automatically generated and may not have been reviewed.
An introduction to the advantages of binary search, and how to use it with the C++ standard library algorithms binary_search()
, lower_bound()
, upper_bound()
, and equal_range()