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:
std::upper_bound and std::lower_bound for Vector in C++ STL
Next article icon

Implementation of lower_bound() and upper_bound() in Vector of Pairs in C++

Last Updated : 21 Jul, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Here we will discuss the implementation of the lower_bound() and upper_bound() in vector 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 equal to the given value “val”. But in Vector of Pairs lower_bound() for pair(x, y) will return an iterator pointing to the position of pair whose  

  • first value is greater than x 
  • first value is equal to  x and second value is greater than equals to y.

If the above-mentioned criteria are not met, then it returns an iterator to the index which is out of the vectors of pairs. 

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 Vector of Pairs upper_bound() for pair(x, y) will return an iterator pointing to the position of the pair whose

  • First value is equal to x and the second value is greater than y, or
  • Whose first value is greater than x.

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

Program 1:  

CPP




// C++ program to demonstrate lower_bound()
// and upper_bound() in Vectors of Pairs
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to implement lower_bound()
void findLowerBound(vector<pair<int, int> >& arr,
                    pair<int, int>& p)
{
    // Given iterator points to the
    // lower_bound() of given pair
    auto low = lower_bound(arr.begin(), arr.end(), p);
 
    cout << "lower_bound() for {2, 5}"
         << " is at index: " << low - arr.begin() << endl;
}
 
// Function to implement upper_bound()
void findUpperBound(vector<pair<int, int> >& arr,
                    pair<int, int>& p)
{
    // Given iterator points to the
    // upper_bound() of given pair
    auto up = upper_bound(arr.begin(), arr.end(), p);
 
    cout << "upper_bound() for {2, 5}"
         << " is at index: " << up - arr.begin() << endl;
}
 
// Driver Code
int main()
{
    // Given sorted vector of Pairs
    vector<pair<int, int> > arr;
    arr = { { 1, 3 }, { 1, 7 }, { 2, 4 },
            { 2, 5 }, { 3, 8 }, { 8, 6 } };
 
    // Given pair {2, 5}
    pair<int, int> p = { 2, 5 };
 
    // Function Call to find lower_bound
    // of pair p in arr
    findLowerBound(arr, p);
 
    // Function Call to find upper_bound
    // of pair p in arr
    findUpperBound(arr, p);
 
    return 0;
}
 
 
Output
lower_bound() for {2, 5} is at index: 3 upper_bound() for {2, 5} is at index: 4 


Next Article
std::upper_bound and std::lower_bound for Vector in C++ STL

D

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

Similar Reads

  • Implementation of lower_bound() and upper_bound() in List of Pairs in C++
    In this article, we will discuss the implementation of the lower_bound() and upper_bound() in a list 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 List of Pairs lower_
    3 min read
  • Implementation of lower_bound and upper_bound on Set of Pairs in C++
    Prerequisite: set lower_bound() function in C++ STL, set upper_bound() function in C++ STL lower_bound() 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 set of Pairs lower_bound() for pair(x, y) wi
    3 min read
  • std::upper_bound and std::lower_bound for Vector in C++ STL
    The std::upper_bound() and std::lower_bound() functions are used for binary search operations STL containers that provide random access. They both are defined inside <algorithm> header file. In this article, we will learn how to use the std::upper_bound and std::lower_bound for vector in C++ S
    5 min read
  • Binary Search functions in C++ STL (binary_search, lower_bound and upper_bound)
    In C++, STL provide various functions like std::binary_search(), std::lower_bound(), and std::upper_bound() which uses the the binary search algorithm for different purposes. These function will only work on the sorted data. There are the 3 binary search function in C++ STL: Table of Content binary_
    3 min read
  • Difference between std::set::upper_bound and std::upper_bound in C++
    Prerequisites: Random-access Iterators, Bidirectional Iterators Sets are a type of associative container in which each element has to be unique because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and
    4 min read
  • Difference between std::set::lower_bound and std::lower_bound in C++
    Prerequisite: Random-access Iterators in C++, Bidirectional Iterators in C++. std::lower_bound in C++: The lower_bound() method in C++ is used to return an iterator pointing to the first element in the range [first, last) which has a value not less than the given value. This means that the function
    4 min read
  • Sort Vector of Pairs in descending order in C++
    We have discussed some of the cases of sorting vector of pairs in below set 1. Sorting Vector of Pairs in C++ | Set 1 (Sort by first and second) More cases are discussed in this article. Sometimes we require to sort the vector in reverse order. In those instances, rather than first sorting the vecto
    5 min read
  • Sorting Vector of Pairs by 1st element in ascending and 2nd element in descending
    A pair is a container that stores two values mapped to each other, and a vector containing multiple numbers of such pairs is called a vector of pairs. While solving problems there come many instances where there is a need to sort the elements of vector on the basis of both the first and second eleme
    8 min read
  • Sorting 2D Vector in C++ | Set 2 (In descending order by row and column)
    We have discussed some of the cases of sorting 2D vector in below set 1. Sorting 2D Vector in C++ | Set 1 (By row and column) More cases are discussed in this article Case 3 : To sort a particular row of 2D vector in descending order This type of sorting arranges a selected row of 2D vector in desce
    4 min read
  • set::lower_bound() Function in C++ STL
    The std::set::lower_bound() method is used to find the first element in the set that is equal to or greater than the given value. It is a member function of std::set class and is defined inside <set> header file. In this article, we will learn about std::set::lower_bound() function in C++. Exa
    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