.lower_bound()
The .lower_bound()
method in C++ is a member function of the std::map
and std::multimap
containers. It is used to find the first element whose key is not less than a specified key (i.e., greater than or equal to the given key). This method returns an iterator pointing to that element. If no such element exists—meaning all keys are less than the specified key—it returns an iterator to the container’s end()
.
Since std::map
and std::multimap
store elements in sorted order based on their keys, .lower_bound()
efficiently utilizes this ordering to perform fast lookups. It is particularly useful in range queries, binary search-like operations, and algorithms that require locating boundary elements. The time complexity of .lower_bound()
is O(log n) due to the underlying Red-Black Tree structure.
Syntax
map_name.lower_bound(const Key& k);
Parameters:
map_name
: The name of thestd::map
orstd::multimap
container on which the method is called.k
: A constant reference to the key value. This specifies the key for which the function will find the lower bound.
Return value:
- Returns an iterator pointing to the first element in the map whose key is greater than or equal to
k
. - If no element is found (all keys are less than
k
), it returnsmap_name.end()
. - If the map is const-qualified, it returns a
const_iterator
; otherwise, it returns aniterator
.
Example 1: Finding the First Element Greater Than or Equal to a Key
This example demonstrates how to use the .lower_bound()
method to find the first element in a map that is greater than or equal to a specified key:
#include <iostream>#include <map>using namespace std;int main() {// Creating a map with character keys and integer valuesmap<char, int> charMap;// Inserting elements into the mapcharMap['a'] = 1;charMap['b'] = 2;charMap['c'] = 3;charMap['e'] = 5;// Displaying the original mapcout << "Map contents:" << endl;for (auto& pair : charMap) {cout << pair.first << " : " << pair.second << endl;}// Finding the lower bound for key 'c'auto it = charMap.lower_bound('c');if (it != charMap.end()) {cout << "Lower bound for 'c': " << it->first << " : " << it->second << endl;} else {cout << "'c' not found in the map." << endl;}return 0;}
The output generated by this code is:
Map contents:a : 1b : 2c : 3e : 5Lower bound for 'c': c : 3
In this example, the .lower_bound()
method is used to find the first element in the map that is greater than or equal to the key 'c'
. The iterator it
points to the element with key 'c'
, and its value is displayed. If the key 'c'
were not present in the map, the program would indicate that the key was not found.
Example 2: Finding the Lower Bound for a Non-Existent Key
This example demonstrates how to use .lower_bound()
to find the lower bound for a non-existent key:
#include <iostream>#include <map>using namespace std;int main() {// Creating a map with character keys and integer valuesmap<char, int> charMap;// Inserting elements into the mapcharMap['a'] = 1;charMap['b'] = 2;charMap['c'] = 3;charMap['e'] = 5;// Displaying the original mapcout << "Map contents:" << endl;for (auto& pair : charMap) {cout << pair.first << " : " << pair.second << endl;}// Finding the lower bound for key 'd'auto it = charMap.lower_bound('d');if (it != charMap.end()) {cout << "Lower bound for 'd': " << it->first << " : " << it->second << endl;} else {cout << "'d' not found in the map." << endl;}return 0;}
The output generated by this code is:
Map contents:a : 1b : 2c : 3e : 5Lower bound for 'd': e : 5
In this example, the .lower_bound()
method is used to find the first element in the map that is greater than or equal to the key 'd'
. Since 'd'
is not present, the method returns an iterator to 'e'
, the first key greater than 'd'
.
Codebyte Example: Using .lower_bound()
with a Custom Comparator
This example demonstrates how to use .lower_bound()
with a custom comparator to find the lower bound for a key in a map with a custom sorting order:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C++ on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn C++
Learn C++ — a versatile programming language that’s important for developing software, games, databases, and more.Beginner Friendly11 hours