Home C++ Maps Functions lower_bound() C++ std::map lower_bound() Function Definition In C++, std::map::lower_bound() returns an iterator to the first pair whose key is not less than the key you give. In plain words: the first key that is greater than or equal to it.
Because a std::map keeps its keys sorted, this is the tool for ordered searches : find the next key at or after a value, or select a range of keys. It takes logarithmic time. Include <map> to use it.
This is a member of std::map<Key, T>, a sorted container of key and value pairs . Each key is unique and the pairs are always kept in key order, so a map can find, insert, and erase a key in logarithmic time. Read a value with at() or find(). The m[key] operator also works, but it adds a default value when the key is missing.
Syntax Pass a key. The result is an iterator that may be end().
auto it = m.lower_bound(25); // first key >= 25
if (it != m.end()) {
std::cout << it->first;
} Call What it does m.lower_bound(key)Returns an iterator to the first pair with a key >= key, or end() if there is none.
If key exists, lower_bound(key) points right at it. If not, it points at the next bigger key. Compare the result with end() before you read it.
Library declarations iterator lower_bound(const Key& key);
const_iterator lower_bound(const Key& key) const;
template<class K> iterator lower_bound(const K& x); // transparent comparator Parameters Parameter Description keyThe value to compare keys against.
Return Value Return Value Description iteratorThe first pair whose key is not less than key, or end() if every key is smaller.
C++ Examples 1. At or after a key 2. Past the last key 3. Select a range of keys Example 1 — lower_bound() Finds the First Key Not Smaller Search for a key that exists and one that does not.
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> m = {
{10, "ten"}, {20, "twenty"}, {30, "thirty"}, {40, "forty"}, {50, "fifty"}
};
auto a = m.lower_bound(30);
auto b = m.lower_bound(25);
std::cout << "lower_bound(30): " << a->first << " " << a->second << '\n';
std::cout << "lower_bound(25): " << b->first << " " << b->second << '\n';
return 0;
} lower_bound(30): 30 thirty
lower_bound(25): 30 thirty How It Works The map holds the keys 10, 20, 30, 40, 50 in sorted order. lower_bound(30) looks for the first key that is >= 30. Key 30 exists, so it points right at it.lower_bound(25) finds no 25. The first key >= 25 is 30, so both searches end on the same pair.Example 2 — lower_bound() and end() Handle the case where every key is smaller than the one you search for.
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> m = {{10, "ten"}, {20, "twenty"}, {30, "thirty"}};
for (int k : {1, 55}) {
auto it = m.lower_bound(k);
if (it == m.end()) {
std::cout << "no key >= " << k << '\n';
} else {
std::cout << "first key >= " << k << " is " << it->first << '\n';
}
}
return 0;
} first key >= 1 is 10
no key >= 55 How It Works The loop searches twice: once with 1 and once with 55. For 1, every key is bigger, so the first key in the map, 10, is the answer. For 55, every key is smaller, so lower_bound() returns end(). The check prevents a read from end() and prints a message instead. Example 3 — lower_bound() for a Range Query Print all keys from 20 up to but not including 50, using two lower_bound() calls.
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<int, std::string> m = {
{10, "ten"}, {20, "twenty"}, {30, "thirty"}, {40, "forty"}, {50, "fifty"}
};
auto first = m.lower_bound(20);
auto last = m.lower_bound(50);
for (auto it = first; it != last; ++it) {
std::cout << it->first << ' ';
}
std::cout << '\n';
return 0;
} How It Works lower_bound(20) points at the pair with key 20. That is where the range starts.lower_bound(50) points at key 50. Iterator ranges stop before their end, so 50 is not included.The loop walks from first until it reaches last, and prints 20 30 40. This is the usual half-open range [20, 50). Frequently Asked Questions What does std::map::lower_bound() return? An iterator to the first pair whose key is greater than or equal to the key you give. It is end() if all keys are smaller.
What is the difference between lower_bound() and find()? find() returns end() unless the exact key exists. lower_bound() also works when the key is missing: it gives the next bigger key.
What is the difference between lower_bound() and upper_bound()? lower_bound(k) finds the first key >= k. upper_bound(k) finds the first key > k. They differ only when k itself exists in the map.
How do I select all keys between two values? Use lower_bound(low) as the start and lower_bound(high) or upper_bound(high) as the end, depending on whether high should be included.
Can I use std::lower_bound() on a map instead? You can, but it is slow because map iterators are not random access. The member function lower_bound() walks the tree and is much faster.
Next: max_size() Keep going through the std::map functions. Get the largest number of pairs the map could ever hold.
max_size() tutorial → About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion