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

In this article, we will discuss the differences between the std::lower_bound and std::set::lower_bound function in C++. But before discussing their differences, we must know about the std::lower_bound and std::set::lower_bound function.

What is std::lower_bound in C++?

The std::lower_bound function detects the first place in a sorted range where a given value could be inserted without causing the ordering to be broken by using a binary search. The Output is an iterator that shows the first value larger than or equal to the one provided in the range [first, last]. Stated otherwise, the function std::lower_bound determines the index of the least element in the sorted range greater than or equal to the input value. All elements preceding this returned iterator must be less than the value. The elements after std::lower_bound may or may not be more than or equal to the value. It enables the use of std::lower_bound for efficient lookups and insertions into stored data

What is std::set::lower_bound in C++?

The lower_bound() method of std::set does a binary search to find where a given key would be added to keep the Set's ordering. It returns an iterator that points to the first entry greater than or equal to the searched key.

If the key is not present in the Set, lower_bound returns an iterator to the next most significant element that sorts after the searched key. It finds that the key's "lower boundary" is in the Ordered Set. It allows efficient lookups, even for keys not already in the Set.

Lower_bound will provide an iterator pointing to the end of the set container if the searched key is greater than the largest element in the Set.

What is the difference between std::lower_bound and set::lower_bound?

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

The following table compares std::lower_bound and std::set::lower_bound in a tabular format.

Featurestd::lower_boundstd::set::lower_bound
Defined in<algorithm> headerSet class definition
Inputs- Iterator range<br>- Value to search for- Key to search for
ReturnsIterator pointing to the first element not less than the value.Iterator pointing to the first element not less than the key.
Container TypeIt works on any sorted container/range e.g. vector, array.It is specifically for std::set containers.
Time ComplexityO(log n)O(log n)
Key ComparisonsUses user-provided comparison e.g. <, ==Uses std::set's comparator
Insertion PositionReturns insertion point to maintain ordering.Returns insertion point to maintain set ordering.
Common UsageGeneral binary search on sorted dataLookup/insertion into a sorted std::set
FlexibilityIt works with custom data types via custom comparators.It only works with data types that support < operator.
IteratorsReturns regular iterator into rangeReturns iterator aware of set's tree structure
PerformanceBinary search algorithmLeverages tree structure, may have faster constants
Use CasesCommonly used with vectors, arraysUseful when data needs ordering and uniqueness
ImplementationBinary search algorithmUtilizes set's underlying tree structure
Element UniquenessDoes not guarantee uniquenessElements are unique in set
Container RequirementsAny sorted rangeRestricted to set containers
Custom SortingCustom comparator can define sort orderFixed sorting based on < operator
Insertion RulesInsertion keeps sorted orderInsertion maintains set ordering rules

while std::set::lower_bound is a set member function that utilizes its ordering specifically for lookup/insertion in a set. But both return the "lower boundary" for a value in O(log n) time.

Example:

Let us take a program to illustrate the use of std::lower_bound() in C++:

Output:

Vector contains: 1 2 4 4 5 7 8 10 
First element not less than 6 is at position: 5

In this example, the target value is 35, and std::lower_bound() is used to find the position where 35 could be inserted in the vector to maintain the sorted order. The Output indicates that element 35 could be inserted at position 3 to keep the sorted order.

Explanation:

  • The program creates a sorted vector of integers.
  • It defines a target value to search for (in this case, target = 6).
  • It uses std::lower_bound() to find the iterator pointing to the first element not less than the target.
  • After that, the program outputs the original vector and the position of the first element, which is not less than the target.

Example:

Let us take a program to illustrate the use of std::set::lower_bound() in C++:

Output:

Set contains: 1 2 4 5 7 8 10 
First element not less than 6 is: 7

In this example, the std::set::lower_bound() function is used to see where 35 could be inserted in the Set to maintain the sorted order. The Output indicates that element 35 could be inserted at position 4 to keep the sorted order. If the component is not found, it returns an iterator pointing to the first element more significant than the target.

Explanation:

  • The program creates a set of integers. The set is always sorted by default.
  • It defines a target value to search for (in this case, target = 6).
  • It uses std::set::lower_bound() to find the iterator pointing to the first element that is not less than the target.
  • After that, the program outputs the original set and the first element, which is not less than the target.

Conclusion:

The std::lower_bound is a generic algorithm that performs binary search to find the insertion point of a key in any sorted container, while std::set::lower_bound is a std::set member function that leverages the sets ordering to efficiently locate a key position or the next most significant element in logarithmic time. The former works universally on sorted ranges; the latter is optimized for operations on std::set containers.


Next Topic#




Latest Courses