Difference between std::set::upper_bound and std::upper_bound in C++

In this article, we will discuss the difference between the std::upper_bound and std::set::upper_bound methods in C++. But before discussing their differences, we must know about std::upper_bound and std::set::upper_bound 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:

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++.

Output:

Difference between std::set::upper_bound and std::upper_bound in C++

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:

"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:

Output:

Difference between std::set::upper_bound and std::upper_bound in C++

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:

Difference between std::set::upper_bound and std::upper_bound in C++

There are several differences between the std::upper_bound and the std::set::upper_bound methods in C++. Some main differences between these methods are as follows:

Featuresstd::set::upper_boundstd::upper_bound
ContextIt is a member function of the std::set container class.Is generic algorithm
Container dependencyIt is specific to container.It can be applied to any sorted arrange not necessarily to a set.
Invoking syntaxThis 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.
NamespaceIt 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 typeIt returns an iterator to the element found.It also returns an iterator but depends on the type of iterator used.
Container modificationIt does not modify the set.It also does not modify the set.
Accessing elementsIt has bidirectional iterators.It has Random Access Iterators.
Run time complexityIt takes O(log2N)It takes O(log2N) for random-access iterators, but for non-random-access iterators, it is O(N).
Iterator requirementIt takes input from a key and returns an iterator based on the key.It operators directly on iterators, not keys.