In this article, we will discuss the difference between the std::upperbound and std::set::upperbound methods in C++. But before discussing their differences, we must know about std::upperbound and std::set::upperbound methods with their syntax and examples.
What is the std::set::upper_bound?
It is a member function of the std::set container class. It returns an iterator pointing to the first element in the set that is greater than a specified value.
Syntax:
It has the following syntax:
std::set<T>::iterator std::set<T>::upper_bound(const T& value);
where "T" is the element stored in the set container
"value" is the value that is searched for.
Example:
Let us take an example to demonstrate the std::set::upper_bound function in C++.
#include <iostream>
#include <set>
int findNextAvailableSlot(const std::set<int>& bookedSlots, int proposedStartTime) {
auto it = bookedSlots.upper_bound(proposedStartTime);
if (it != bookedSlots.end()) {
return *it;
} else {
return -1;
}
}
int main() {
std::set<int> bookedSlots = {1, 3, 5, 7, 9};
int proposedStartTime = 5;
int nextAvailableSlot = findNextAvailableSlot(bookedSlots, proposedStartTime);
if (nextAvailableSlot != -1) {
std::cout << "Next available slot starts at: " << nextAvailableSlot << std::endl;
} else {
std::cout << "All slots are booked." << std::endl;
}
return 0;
}
Output:
Explanation:
There are two functions in the above program. The variables in the findNextAvailableSlot function are bookedSlots , which represent the booked time slots, and proposedStartTime will mean the start time of a meeting. This function uses the first booked slot in bookedSlots that starts after the proposedStartTime. If a slot is found, it returns the start time; otherwise, it returns -1.
The main function has three variables: bookedSlots , which represent the already booked time slots; proposedStartTime , which means the start time of the new meeting; and nextAvailableSlot, which is used to store the result of the function. This function will create a set of booked times. It checks the result and prints the appropriate message based on whether a slot is available.
What is the set::upper_bound?
It is a generic algorithm in the C++ standard template library. We use the <algorithm> header to include this in our program. It will return an iterator pointing to the first element in the range greater than a specified value.
Syntax:
It has the following syntax:
template<class ForwardIt, class T>
ForwardIt std::upper_bound(ForwardIt first, ForwardIt last, const T& value);
"ForwardIt" is a iterator type.
"first" and "last" iterators define the range to search in.
"value" is the value to search for.
Example:
Let us take a C++ program to demonstrate the std::upper_bound function:
#include <iostream>
#include <set>
#include <algorithm>
int findNextAvailableSlot(const std::set<int>& bookedSlots, int proposedStartTime) {
auto it = std::upper_bound(bookedSlots.begin(), bookedSlots.end(), proposedStartTime);
if (it != bookedSlots.end()) {
return *it;
} else {
return -1;
}
}
int main() {
std::set<int> bookedSlots = {1, 3, 5, 7, 9};
int proposedStartTime = 5;
int nextAvailableSlot = findNextAvailableSlot(bookedSlots, proposedStartTime);
if (nextAvailableSlot != -1) {
std::cout << "Next available slot starts at: " << nextAvailableSlot << std::endl;
} else {
std::cout << "All slots are booked." << std::endl;
}
return 0;
}
Output:
Explanation:
This program has the same functionality as the previous program, but the implementation is different here. The findNextAvailableSlot function uses the std::upper_bound with an iterator obtained from bookedSlots.begin and bookedSlots.end . The rest of the program logic is the same as the previous program.
Main difference between the std::upper_bound and std::set::upper_bound:
There are several differences between the std::upperbound and the std::set::upperbound methods in C++. Some main differences between these methods are as follows:
| Features | std::set::upper_bound | std::upper_bound |
|---|---|---|
| Context | It is a member function of the std::set container class. | Is generic algorithm |
| Container dependency | It is specific to container. | It can be applied to any sorted arrange not necessarily to a set. |
| Invoking syntax | This function is invoked by using the member function on a set object. | It is the template function. It takes the iterators to the beginning and end of a range, along with the search value. |
| Namespace | It is part of the std namespace and specific to the set container. | It also having the std namespace but applicable to many containers. |
| Header | <set> header is used | <algorithm> header is used |
| Return type | It returns an iterator to the element found. | It also returns an iterator but depends on the type of iterator used. |
| Container modification | It does not modify the set. | It also does not modify the set. |
| Accessing elements | It has bidirectional iterators. | It has Random Access Iterators. |
| Run time complexity | It takes O(log2N) | It takes O(log2N) for random-access iterators, but for non-random-access iterators, it is O(N). |
| Iterator requirement | It takes input from a key and returns an iterator based on the key. | It operators directly on iterators, not keys. |