# Implementation of lower_bound() and upper_bound() on Map of Pairs in C++

In this article, we will discuss the implementation of the lower_bound() and upper_bound() in the Map of pairs.

• lower_bound(): It returns an iterator pointing to the first element in the range [first, last) which has a value greater than or equals to the given value “val”. But in Map of Pairs lower_bound() for pair(x, y) will return an iterator pointing to the pair whose first value is greater than or equals x and second value is greater than equals to y.
If the above-mentioned criteria are not met, then it returns an iterator which points to the pair {Map.size(), 0}.

Syntax:

```mp.lower_bound({a, b})

where,
mp is the map of pairs
and {a, b} whose lower_bound
is to be found
```
• upper_bound(): It returns an iterator pointing to the first element in the range [first, last) which has a value greater than the given value “val”. But in Map of Pairs upper_bound() for pair(x, y) will return an iterator pointing to the pair whose first value is greater than x and second value is greater than y.
If the above-mentioned criteria are not met, then it returns an iterator which points to the pair {Map.size(), 0}.

Syntax:

```mp.upper_bound({a, b})

where,
mp is the map of pairs
and {a, b} whose upper_bound
is to be found
```

Below is the program to demonstrate lower_bound() and upper_bound() in Map of pairs:

Program 1:

 `// C++ program to demonstrate lower_bound() ` `// and upper_bound() in Map of Pairs ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function to implement lower_bound() ` `void` `findLowerBound( ` `    ``map, ``int``>& mp, ` `    ``pair<``int``, ``int``>& p) ` `{ ` ` `  `    ``// This iterator points to the ` `    ``// lower_bound() of given pair ` `    ``auto` `low = mp.lower_bound(p); ` ` `  `    ``cout << ``"lower_bound() for {2, 5}"` `         ``<< ``" is: {"` `         ``<< (*low).first.first << ``", "` `         ``<< (*low).first.second ` `         ``<< ``"}"` `<< endl; ` `} ` ` `  `// Function to implement upper_bound() ` `void` `findUpperBound( ` `    ``map, ``int``>& mp, ` `    ``pair<``int``, ``int``>& p) ` `{ ` ` `  `    ``// This iterator points to the ` `    ``// upper_bound() of given pair ` `    ``auto` `up = mp.upper_bound(p); ` ` `  `    ``cout << ``"upper_bound() for {2, 5}"` `         ``<< ``" is: {"` `         ``<< (*up).first.first << ``", "` `         ``<< (*up).first.second ` `         ``<< ``"}"` `<< endl; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``// Declare map of Pairs ` `    ``map, ``int``> mp; ` ` `  `    ``// Insert Pairs in Map ` `    ``mp.insert({ { 2, 3 }, 8 }); ` `    ``mp.insert({ { 4, 1 }, 5 }); ` `    ``mp.insert({ { 7, 1 }, 3 }); ` `    ``mp.insert({ { 9, 3 }, 1 }); ` `    ``mp.insert({ { 5, 0 }, 3 }); ` ` `  `    ``// Given pair {2, 5} ` `    ``pair<``int``, ``int``> p = { 2, 5 }; ` ` `  `    ``// Function Call to find lower_bound ` `    ``// of pair p in map mp ` `    ``findLowerBound(mp, p); ` ` `  `    ``// Function Call to find upper_bound ` `    ``// of pair p in map mp ` `    ``findUpperBound(mp, p); ` ` `  `    ``return` `0; ` `} `

Output:

```lower_bound() for {2, 5} is: {4, 1}
upper_bound() for {2, 5} is: {4, 1}
```

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next