# map lower_bound() function in C++ STL

C++Server Side ProgrammingProgramming

In this article we will be discussing the working, syntax and examples of map::lower_bound() function in C++ STL.

## What is a Map in C++ STL?

Maps are the associative container, which facilitates to store the elements formed by a combination of key value and mapped value in a specific order. In a map container the data is internally always sorted with the help of its associated keys. The values in the map container are accessed by its unique keys.

## What is a map::lower_bound()?

map::lower_bound() function is an inbuilt function in C++ STL, which is defined in  header file. lower_bound() returns an iterator to the lower bound of the map container. This function returns an iterator which points to the first element which is considered to go before the key k.

## Syntax

Map_name.lower_bound(key& k);

## Parameter

This function accepts only 1 parameter −

• k − The key which we want to search.

## Return value

This function returns the iterator which is pointing to the first element of the key ‘k’ which is to be considered to go before the key k.

## Input

map<char, int> newmap;
newmap[‘a’] = 1;
newmap[‘b’] = 2;
newmap[‘c’] = 3;
newmap.lower_bound(b);

## Output

a:1

## Example

Live Demo

#include <bits/stdc++.h>
using namespace std;
int main() {
map<int, int> TP_Map;
TP_Map.insert({5, 50});
TP_Map.insert({2, 30});
TP_Map.insert({1, 10});
TP_Map.insert({4, 70});
cout<<"\nTP Map is : \n";
cout << "MAP_KEY\tMAP_ELEMENT\n";
for (auto i = TP_Map.rbegin(); i!= TP_Map.rend(); i++) {
cout << i->first << "\t" << i->second << endl;
}
auto i = TP_Map.lower_bound(2);
cout << "The lower bound of key 2 is ";
cout << i->first << ": " << i->second << endl;
auto i_1 = TP_Map.lower_bound(3);
cout << "The lower bound of key 3 is ";
cout << i_1->first << " :" << i_1->second << endl;
return 0;
}

## Output

TP Map is:
MAP_KEY    MAP_ELEMENT
5             50
4             70
2             30
1             10
The lower bound of key 2 is 2 :30
The lower bound of key 3 is 4 :70
Published on 15-Apr-2020 12:25:57