Map lower_bound() function in C++ STL

Welcome learners!! In this tutorial, you will be learning about the lower_bound function in Map in C++ STL. Before diving into the code let’s discuss it briefly.

Map lower_bound function

lower_bound is a built-in function of Maps in STL(Standard Template Library). It returns the iterator of that value that is being passed in the parameters of the lower-bound function. If that value is present it’s great it will return its iterator but in case it is not present inside our map-elements then it will return the iterator of a greater value.


For example, suppose you have a map and inside your map you have [1,2,33,4,5] as your respective keys corresponding to different values [22,56,3,5,78].

if you use the lower_bound function and pass 2 as a parameter, it will return that key’s iterator (i.e 56). But if you pass 17 as a parameter then what will it return?? It’s crystal clear it will return the iterator of a value greater than that which is 33 in our case.

Remember in Maps the lower_bound applies to only keys.

Explanation

  • As you all know if we want to use maps in C++ then Firstly we have to include its header file. let’s do that.

 

#include <map>

if you are like me who forgets where to use which header file then <bits/stdc++.h> will be a blessing for you. you can use that instead of <iostream>. it contains all libraries.

  •   After that we will create a map where keys will act as integers and values are characters. Additionally, we will insert elements inside our map using the built-in insert() function.

 

map<int,char> map1;

map1.insert({ 1,'P'  });
map1.insert({ 2,'Q'  });
map1.insert({ 34,'R'  });
map1.insert({ 78,'S'  });
map1.insert({ 66,'T'  });
  • Before using  the lower_bound function lets print our map elements. In order to do so you first need to create an iterator we call it iter. Now we  will be using for loop which starts from begin till end and prints the key as well as values.
map <int,char> :: iterator iter;

for (iter = map1.begin(); iter != map1.end(); iter++)
{
   cout<<(*iter).first<<"    "<<(*iter).second<<"\n";
}
  •  Now we will be using the lower_bound function and pass the value as a parameter for which we need to find lower_bound. With the help of .first, we are accessing the key similarly with the help of .second we are accessing the value.

 

iter = map1.lower_bound(2);
cout << (*iter).first << " " << (*iter).second << endl;
  •  let’s say we are finding the lower_bound of a element that is not mapped inside map1.Then in this situation as discussed above it will return the iterator of a greater value.

 

iter = map1.lower_bound(17);
cout << (*iter).first << " " << (*iter).second << endl;

Now we understood the overview Lets code it guys!

Implementation of Map lower_bound() function in C++

#include <iostream>
#include <map>
using namespace std;
int main()
{
    //map lower_bound() function in C++ STL

    map<int,char> map1;

    map1.insert({ 1,'P'  });
    map1.insert({ 2,'Q'  });
    map1.insert({ 34,'R'  });
    map1.insert({ 78,'S'  });
    map1.insert({ 66,'T'  });


    map <int,char> :: iterator iter;


    for (iter = map1.begin(); iter != map1.end(); iter++)
    {
      cout<<(*iter).first<<"    "<<(*iter).second<<"\n";
    }

    cout<<endl;

    iter = map1.lower_bound(2);
    cout << (*iter).first << " " << (*iter).second << endl;

    iter = map1.lower_bound(17);
    cout << (*iter).first << " " << (*iter).second << endl;


    return 0;
}

Output:-

1 P
2 Q
34 R
66 T
78 S

2 Q
34 R

That’s it for this tutorial. I hope it will be helpful.

Leave a Reply

Your email address will not be published.