upper_bound in C++

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

The upper_bound() C++ is a standard library function that is used to find the upper bound of the provided in the given range parameter of the function. It returns an iteration referring to the range parameter of the function and if there are no such elements found, then it returns an end(). There are various parameters such as val, comp, first, and last of upper_bound() function C++.

Build an AI-First Career, Master the Complete Skillset

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program
NSDC Certified

AI Forward Deployed Engineer Program

Full-stack engineering, production AI and client-facing consulting

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program

std::upper_bound()

The upper_bound C++ is a function that is used to find an iteration referring to the first element from a provided specific range. The range is defined as [first, last]. This function returns the value after the comparison from a parameter namely val. Upper_bound() C++ is a standard library function that is defined in the header file of the C++ program. If there is no value found compared to the parameter val from the given range, then the function returns an iteration whose value is "end value + 1". This function works internally based on a binary search algorithm that helps the program find the defined iteration in the val and range.

Syntax

There are two types of the syntax of the upper_bound() C++ function which are as follows:

  • Syntax 1:
  • Syntax 2:
Sharpen Your Fundamentals with Free Learning

Parameters

The parameters of the upper_bound() C++ function is as follows:

  • first: It is the forward iteration point that is used to define the first element of the range that will be defined as the searching range of the function.
  • last: It is a forward iteration that represents the last element of the searching range of the function.
  • val: This parameter is the value of the upper bound that needs to be searched in the given range of the upper_bound C++ function.
  • comp: This is an optional parameter of the upper_bound() C++ function. It is used to provide a customized range for the function. By following the strict weak ordering of the elements, it returns true and false on the basis or order of the elements.

How Scaler Transformed Careers in Different Fields

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Return Value

The return value of the upper_bound() C++ function is as follows:

  • Return value: It returns an iteration that refers to the first element from the provided range. This value is greater than val otherwise the function returns the last element from the range if no such val is found.

Exceptions

The exception of the upper_bound() C++ function is as follows:

  • The upper_bound() C++ function will throw an error if the element comparison of the function will throw an error.
  • If you give any invalid parameter in this function, it will affect the functioning of the function, causing undefined behavior.

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

Examples

Now, let us see some examples of upper_bound() C++ to understand how this function works.

Example 1. Simple use of upper_bound() C++

  • Output:

Example 2. Upper_bound() C++ with Arrays

  • Output:

Complexity

The complexity of the upper_bound() C ++ function is as follows:

  • The complexity of this function is the distance between the first and last elements given in the range. So it will be like (at most log2(last – first) + O(1) comparisons).
  • On non-random-access iterators, the iterator advances produce themselves an additional linear complexity in N on average.

Important Points to Remember

While using the upper_bound() C ++ function, some important points need to be kept in mind. These are as follows:

  • upper_bound() C++ returns an iteration that refers to the upper bound of the value provided in the range.
  • This function works with the sorted sequence of elements only.
  • Unlike lower_bound, the value pointed by the iterator returned by this function cannot be equivalent to the parameter val, it should be only greater.

Conclusion

  • The upper_bound C++ is a function that is used to find an iteration referring to the first element from a provided specific range.
  • The parameters of the upper_bound() C++ function are first, last, val, and comp, and the return value of the upper_bound() C++ function is an iteration that refers to the first element from the provided range.
  • The upper_bound() C++ function will throw an error if the element comparison of the function will throw an error and this function works with the sorted elements of an array only.