Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
How to Convert a Map of Set of Pairs in C++?
Next article icon

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

Last Updated : 31 May, 2020
Comments
Improve
Suggest changes
Like Article
Like
Report
Prerequisite: map lower_bound() function in C++ STL, map upper_bound() function in C++ STL 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: CPP
// C++ program to demonstrate lower_bound() // and upper_bound() in Map of Pairs  #include <bits/stdc++.h> using namespace std;  // Function to implement lower_bound() void findLowerBound(     map<pair<int, int>, 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<pair<int, int>, 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<pair<int, int>, 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}  

Next Article
How to Convert a Map of Set of Pairs in C++?

D

dreamer07
Improve
Article Tags :
  • C++ Programs
  • C++
  • cpp-map
  • cpp-pair
Practice Tags :
  • CPP

Similar Reads

  • Implementation of lower_bound() and upper_bound() in Array of Pairs in C++
    In this article we will discuss the implementation of the lower_bound() and upper_bound() in an array 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 Array of Pairs lowe
    3 min read
  • Implementing upper_bound() and lower_bound() for Ordered Set in C++
    Prerequisites: Ordered Set and GNU C++ PBDS Given an ordered set set and a key K, the task is to find the upper bound and lower bound of the element K in the set in C++. If the element is not present or either of the bounds could not be calculated, then print -1. Ordered set is a policy based data s
    3 min read
  • How to Convert a Map of Set of Pairs in C++?
    In C++, a map can be converted to a set of pairs. This can be useful when you want to create a set of pairs from a map, where each pair contains a key and a value. In this article, we will learn how to convert a map to a set of pairs in C++. Example Input: myMap = {{1, “one”}, {2, “two”}, {3, “three
    2 min read
  • How to Add Elements to a Map in C++?
    In C++, a map is an associative container that stores the elements as key-value pairs where each element has a key and a mapped value. In this article, we will learn how to add elements to a map in C++ STL. For Example, Input: myMap = {{1, "Ram"}, {2, "Mohit"}}; Output: myMap = {{1, "Ram"}, {2, "Moh
    2 min read
  • How to Access Elements of Set of Maps in C++?
    In C++, a set of maps can be used to store multiple maps of key-value pairs sorted according to their number and values of elements such as graphs. In this article, we will explore how we can access the elements of a set of maps in C++. Example: Input: { { {1: "C++"}, {2:" Python"} }, { {3:" Java"},
    2 min read
  • How to Insert Elements from Vectors to a Map in C++?
    In C++, vectors are the dynamic array that stores data in contiguous memory locations while maps are the data containers that store the key-value pair and are sorted on the basis of the key. In this article, we will learn how to efficiently map elements from vectors to a map in C++. Example Input: v
    2 min read
  • How to Sort a Vector of Pairs Based on the Second Element of the Pair in C++?
    In C++, a vector of pairs is a common data structure used to store pairs of values. Sometimes, when dealing with pairs we need to sort such vectors of pairs based on a specific element of the pair like sorting it based on the second element of each pair. In this article, we will learn how to sort a
    3 min read
  • Difference between pair in Multiset and Multimap in C++ STL
    Pairs in C++: The pair container is a simple container defined in <utility> header consisting of two data elements or objects. The first element is referenced as ‘first’ and the second element as ‘second’ and the order is fixed (first, second). Pair is used to combine together two values which
    5 min read
  • How to Find the Minimum and Maximum Element of a Vector Using STL in C++?
    In this article, we will learn how to find the minimum and maximum element in vector in C++. The simplest method to find the minimum and maximum element in vector is by using min_element() and max_element(). Let’s take a look at a simple example: [GFGTABS] C++ #include <bits/stdc++.h> using na
    2 min read
  • How to Implement Custom Hash Functions for User-Defined Types in std::unordered_map?
    In C++ std::unordered_map is a data structure that implements a hash table and allows fast access to each element based on its key. However, when we want to use user-defined types as keys, we need to provide a custom hash function. In this article, we will learn how to implement a custom hash functi
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences