In this tutorial, we are going to explore the variance between the std::upperbound and std::set::upperbound functions in C++. Prior to delving into their distinctions, it is essential to understand the std::upperbound and std::set::upperbound functions along with their syntax and usage examples.
What is the std::set::upper_bound?
It is a member method of the std::set container class. It provides an iterator that points to the initial element in the set exceeding a given value.
Syntax:
It has the following syntax:
std::set<T>::iterator std::set<T>::upper_bound(const T& value);
where "T" represents the element contained within the set data structure
"value" is the value that is searched for.
Example:
Let's consider a scenario to illustrate the functionality of the std::set::upper_bound method 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 exist two functions within the aforementioned program. The variables utilized in the findNextAvailableSlot function are bookedSlots, denoting the reserved time slots, and proposedStartTime indicating the meeting's starting time. This particular function identifies the initial booked slot in bookedSlots that commences post the proposedStartTime. In case a suitable slot is identified, it provides the starting time; if not, it returns -1.
The primary function consists of three variables: reservedSlots, indicating the time slots already booked; suggestedStartTime, denoting the starting time of the upcoming meeting; and followingVacantSlot, designated for storing the function's output. Its purpose is to generate a collection of reserved time intervals. Following the evaluation, it will display a relevant message depending on the slot's availability.
What is the set::upper_bound?
It is a common algorithm within the C++ standard template library. The <algorithm> header is required to incorporate this functionality into our program. The algorithm is designed to provide an iterator that points to the initial element within a range that surpasses 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.
The "first" and "last" iterators establish the range within which the search is conducted.
"value" is the value to search for.
Example:
Let's consider a C++ code example to showcase the functionality of the std::upper_bound method:
#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 software provides identical functionality to the prior version, although the approach taken here is distinct. In this case, the findNextAvailableSlot function leverages std::upper_bound alongside an iterator derived from bookedSlots.begin and bookedSlots.end. The remaining program logic mirrors that of the earlier version.
Main difference between the std::upper_bound and std::set::upper_bound:
There exist various distinctions between the std::upperbound function and the std::set::upperbound method in the C++ programming language. A few key variances between these functions include:
| 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 | _PRESERVE5__ header is used | _PRESERVE6__ 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. |