Open In App

map::lower_bound() in C++ STL

Last Updated : 04 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

C++
// C++ Program to illustrate the use of
// std::map::lower_bound() method
#include <bits/stdc++.h>
using namespace std;

int main() {
    map<int, string> m = {{1, "Geeks"},
    {3, "Geeksfor"}, {2, "GeeksforGeeks"}};
  
    auto it = m.lower_bound(2);

    cout << it->first << ": " << it->second;
    return 0;
}

Output
2: GeeksforGeeks

map::lower_bound() Syntax

map::lower_bound() is a member function of std::map class defined inside <map> header file.

m.lower_bound(k);

Parameters

  • k: The key for which the lower bound is to be found.

Return Value

  • Returns an iterator to the element with smallest key greater than or equal to k.
  • If all the keys in the map are less than k, returns iterator to the end of the map.
  • If all the keys in the map are greater than k, returns iterator to the beginning of the range.

Working of map::lower_bound()

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.

  • If we find the lower bound in this path, we quit the search and return the iterator to it.
  • If we reach the leaf node without finding it, we return iterator to the end.

That is why, the complexity of the map::lower_bound() function is same as the search operation of self-balancing binary search tree.

More Examples of map::lower_bound()

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.

Example 1: Checking if a Key Exists Using map::lower_bound()

C++
// C++ program to check if a key exists using
// map::lower_bound
#include <bits/stdc++.h>
using namespace std;

int main() {
    map<int, char> m = {{10, 'A'}, {20, 'B'},
                        {30, 'C'}};
    int k = 20;

  	// Find the lower bound
    auto it = m.lower_bound(k);

    // Check if the key exists in the map
    if (it != m.end() && it->first == k)
        cout << k << " is present.";
    else
        cout << k << " is NOT present.";

    return 0;
}

Output
20 is present.

Time Complexity: O(log n), where is the number of elements in the set.
Auxiliary Space: O(1)

Example 2: Counting Elements Smaller and Larger Than a Key

C++
// C++ program to count elements smaller and
// larger than a key in map
#include <iostream>
#include <map>
using namespace std;

int main() {
    map<int, char> m = {{10, 'A'}, {20, 'B'},
                        {30, 'C'}, {40, 'D'}};
    int k = 25;

    // Finding lower bound of key
    auto lb = m.lower_bound(k);

    // Counting elements smaller than the key
    cout << "No. of Smaller Elements: " << 
      distance(m.begin(), lb) << endl;

    // Counting elements larger than the key
    cout << "No. of Larger Elements: " << 
      distance(lb, m.end());

    return 0;
}

Output
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)


Next Article

Similar Reads