C++

How to Use Upper_bound() Function in C++

A variety of programs, including games, graphics, web servers, and more, can be made using the C++ programming language.  However, sometimes we may need to perform some operations on the data in our programs, such as searching, sorting, or finding the maximum or minimum value among a range of elements. One of the functions that can be used to find the upper bound of a value in a sorted range of elements is upper_bound().

What is upper_bound() Function in C++

The upper_bound() function in C++ is a function that takes a sorted range of elements and a value as arguments and returns an iterator pointing to the first element in the range that is greater than the value.

It has two different types of arguments:

num upper_bound (num.first, num.last, value)

Iterators that specify the range of elements to be examined are first and last. The interval utilized contains all elements from the first element up to the end but does not include the element indicated by last. Value is the value to compare the elements to.

num upper_bound (num.first, num.last,  value, compare comp)

In this case, the binary function comp produces a value that can be converted to a bool and accepts two parameters of the same type as the range’s items. If a certain condition dictates that the first argument is not higher than the second one, the function must return true result and if not, it should return false.

How to Use upper_bound() Function in C++

The upper_bound() function can be used to find the upper bound of a value in a sorted range of elements in various situations. For example, we can use it to find the position of an element in a sorted array or vector, or to find the next greater element in a set or map. Here are some examples of how to use upper_bound() function in C++:

Example 1: Using upper_bound() Function to Find the Position of an Element in a Sorted Array

Here is an example, that uses the upper_bound() function to find the position of an element in a sorted array of integers and display it on the screen:

#include <iostream>

#include <bits/stdc++.h>

using namespace std;

int main()

{

    int array[] = {15, 35, 45, 55, 65};

    int a = sizeof(array) / sizeof(array[0]);

    cout << "Array contains: ";

    for (int i = 0; i < a; i++)

        cout << array[i] << " ";

    cout << "\n";

    int b = 45; // declare and initialize a value

    int* p = upper_bound(array, array + a, b);

    cout << "Upper bound of " << b << " is at position: " << (p - array) << "\n"; // display the position using pointer arithmetic

    return 0;

}

First the program defines the necessary header files and array containing numbers and then uses the sizeof() function to get the size of the array. Next a for loop is used to display the elements of the array and then an integer is declared whose position in the array is determined using the pointer and it is displayed in the output:

Example 2: Using upper_bound() Function to Find the Next Greater Element in a Set

Here is an example code that uses the upper_bound() function to find the next greater element than a given value in a set of integers and display it on the screen:

#include <iostream>

#include <bits/stdc++.h>

#include <vector>

using namespace std;

int main()

{

    set<int> num = {15, 25, 35, 45, 55}; // declare and initialize a set of integers

    cout << "Given Numbers: ";

    for (auto a : num) // display the set elements using range-based for loop

        cout << a << " ";

    cout << "\n";

    int a = 45; // declare and initialize a value

    auto it = upper_bound(num.begin(), num.end(), a); // find the upper bound of x in the set using upper_bound()

    if (it != num.end()) // check if the iterator is valid

        cout << "The Next Higher Number Than " << a << " is " << *it << "\n"; // display the element using dereference operator

    else

        cout << "There is no Higher Number than " << a << "\n"; // display a message if no such element is found

    return 0;

}

First the code defines the necessary header files and then a vector of five elements is defined, next the vector is displayed using the auto keyword as it can automatically set the data type of the elements. Next a variable having a value of 45 is declared which is then compared to the vector defined using the upper_bound() function and afterwards it displays the comparison result:

Conclusion

The upper_bound() function is a function that returns an iterator pointing to the first element in a sorted range that is greater than a given value. To locate the first number in an interval that is greater than or equal to a specified number, use the upper_bound() function in C++. This can be useful for tasks such as finding the next highest number in a list or finding the first element in a sorted array that is greater than a given threshold.

About the author

Aaliyan Javaid

I am an electrical engineer and a technical blogger. My keen interest in embedded systems has led me to write and share my knowledge about them.