![]() |
VOOZH | about |
In C++, std::map::lower_bound() is a built-in method used to find the first element in the map whose key is either equal to or greater than the given key. In this article, we will learn about std::map::lower_bound() function in C++.
Example:
2: GeeksforGeeks
map::lower_bound() is a member function of std::map class defined inside <map> header file.
m.lower_bound(k);
The std::map::lower_bound() function uses the search operation of std::map container which is internally implemented as self-balancing binary search tree. So, for searching the lower bound of any value, we move from the root node of the tree using the path that may contain the lower bound.
That is why, the complexity of the map::lower_bound() function is same as the search operation of self-balancing binary search tree.
The map::lower_bound() function is an interesting function that can be used for number of applications. The below examples demonstrate some of its common uses.
20 is present.
Time Complexity: O(log n), where n is the number of elements in the set.
Auxiliary Space: O(1)
No. of Smaller Elements: 2 No. of Larger Elements: 2
Time Complexity: O(n), due to std::distance(). map::lower_bound() still takes O(log n).
Auxiliary Space: O(1)