In C++, upper_bound() is a built-in function used to find the first element in a sorted range that is strictly greater than a given value. It implements binary search algorithm so it requires that the range is either sorted or at least partitioned with respect to the given element.
Let's take a look at an example:
C++
// C++ program to illustrate the use of std::upper_bound
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {10, 20, 30, 40, 50};
// Finding upper bound for value 30 in vector v
cout << *upper_bound(v.begin(), v.end(), 30);
return 0;
}
Syntax of upper_bound()
The upper_bound() function is defined inside the <algorithm> header file.
upper_bound (first, last, val, comp);
Parameters:
- first: Iterator to the first element in the range.
- last: Iterator to the theoretical element just after the last element in the range.
- val: Value to be compared.
- comp (optional): Binary function that accepts and compares val with an element in the range. By default, it returns true if the val is smaller than the element in the range, false otherwise.
Return Value:
- Returns an iterator to the smallest number greater than val.
- Returns iterator to the end of the range if all the elements are smaller than or equal to val.
Note: The behavior of the upper_bound() is undefined if the array is not sorted or at least partitioned with respect to the given value.
The upper_bound function is useful for finding elements in sorted ranges. The C++ Course offers detailed lessons on STL algorithms, including how to use upper_bound effectively in your projects.
Examples of upper_bound()
The upper_bound() function is an interesting function that can be used for number of applications. The below examples demonstrate some of its common uses.
Find Upper Bound in an Array
C++
// C++ program to find the lower bound of a value in a
// vector using std::upper_bound()
#include <bits/stdc++.h>
using namespace std;
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int n = sizeof(arr)/sizeof(arr[0]);
// Finding upper bound for value 30 in array arr
cout << *upper_bound(arr, arr + n, 30);
return 0;
}
Time Complexity: O(log n), where n
is the number of elements in the array.
Auxiliary Space: O(1).
Use upper_bound() with Custom Comparator
C++
// C++ program to illustrate how to use custom
// comparator with std::upper_bound() function
#include <bits/stdc++.h>
using namespace std;
bool comp(const string &a, const string &b) {
return lexicographical_compare(a.begin(), a.end(),
b.begin(), b.end(), [](char c1, char c2) {
return tolower(c1) < tolower(c2);
});
}
int main() {
vector<string> v = {"Apple", "banana", "Cherry",
"date", "Elderberry"};
// Finding upper bound of "Avocado"
auto ub = upper_bound(v.begin(), v.end(), "Avocado",
comp);
if (ub != v.end())
cout << *ub;
else
cout << "Upper bound not found!";
return 0;
}
Time Complexity: O(log n), where n is the number of elements in the vector.
Auxiliary Space: O(1)
Explanation: We need to use the custom comparator function to perform the case insensitive search as the default comparator treats the uppercase and lowercase differently.
Check for an Element in a Sorted Vector
C++
// C++ Program to check if the element exists in a
// vector using upper_bound()
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {10, 20, 30, 40, 50};
int val = 40;
// Finding the upper bound
auto it = upper_bound(v.begin(), v.end(), val);
// Chekcing if val exists or not
if (it != v.begin() && *(--it) == val)
cout << val << " is found.";
else
cout << val << " is NOT found.";
return 0;
}
Time Complexity: O(log n), where n is the number of elements in vector.
Auxiliary Space: O(1)
Explanation: Unlike lower_bound() function, the upper_bound() function never returns the iterator to the matching value if found, it always returns the iterator to the value just after the matching value if it exists. So, to check if the given value exists, we need to decrement the iterator return by the upper_bound() and then compare.
Count Elements Smaller and Larger than a Value
C++
// C++ Program to count the elements smaller and larger
// than a value in a vector using upper_bound()
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v = {10, 20, 30, 40, 50};
int val = 30;
// Finding the upper bound of val in v
auto ub = upper_bound(v.begin(), v.end(), val);
// Finding the number of smaller elements
cout << "No. of Smaller Elements: " << ub - v.begin()
<< endl;
// Finding the number of larger elements
cout << "No. of Larger Elements: " << v.end() - ub;
return 0;
}
OutputNo. of Smaller Elements: 3
No. of Larger Elements: 2
Time Complexity: O(log n), where n is the number of elements in vector.
Auxiliary Space: O(1)
Explanation: Counts elements based on the position returned by upper_bound().
Finding Upper Bound in a Set
C++
// C++ program to find the upper bound of a value in
// a set using std::upper_bound()
#include <bits/stdc++.h>
using namespace std;
int main() {
set<int> s = {10, 20, 30, 40, 50};
// Finding upper bound for value 30 in set s
auto it = upper_bound(s.begin(), s.end(), 30);
cout << *it;
return 0;
}
Time Complexity: O(n)
Space Complexity: O(1)
Explanation: The time complexity is O(n) for the set is because it doesn't provide random access to its elements. So, the upper_bound() function have to increment it sequentially to find the middle element (part of binary search algorithm) each time leading to increased time complexity. However, set have their own specialized version named as set::upper_bound() method.
Similar Reads
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
TCP/IP Model The TCP/IP model is a framework that is used to model the communication in a network. It is mainly a collection of network protocols and organization of these protocols in different layers for modeling the network.It has four layers, Application, Transport, Network/Internet and Network Access.While
7 min read
Basics of Computer Networking A computer network is a collection of interconnected devices that share resources and information. These devices can include computers, servers, printers, and other hardware. Networks allow for the efficient exchange of data, enabling various applications such as email, file sharing, and internet br
14 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Unified Modeling Language (UML) Diagrams Unified Modeling Language (UML) is a general-purpose modeling language. The main aim of UML is to define a standard way to visualize the way a system has been designed. It is quite similar to blueprints used in other fields of engineering. UML is not a programming language, it is rather a visual lan
14 min read
Second Largest Element in an Array Given an array of positive integers arr[] of size n, the task is to find second largest distinct element in the array.Note: If the second largest element does not exist, return -1. Examples:Input: arr[] = [12, 35, 1, 10, 34, 1]Output: 34Explanation: The largest element of the array is 35 and the sec
14 min read
Python Lists In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
6 min read
Introduction to Java Java is a high-level, object-oriented programming language developed by Sun Microsystems in 1995. It is platform-independent, which means we can write code once and run it anywhere using the Java Virtual Machine (JVM). Java is mostly used for building desktop applications, web applications, Android
4 min read