## Return Type

When you use the upper bound function in C++, it's helpful to know what you'll get back from it. This is called the "return type." For the upper bound, the return type is a bit like a finger pointing to a spot in your list of numbers. It doesn't give you the number itself but shows you where the number would go if it was in the list.

In technical terms, the upper bound function returns an iterator. Think of an iterator as a marker or a pointer that shows a specific position in your sorted sequence, like a bookmark in your notebook. If the upper bound finds a number greater than the one you're looking for, the iterator will point right there. If there's no number bigger than yours in the list, the iterator will point to the end, signaling there's no spot for your number.

Here's a quick example to make it clearer:

### C++

`#include <iostream>`

#include <vector>

#include <algorithm>

using namespace std;

int main() {

vector<int> numbers = {2, 4, 6, 8, 10};

int value = 7;

auto it = upper_bound(numbers.begin(), numbers.end(), value);

if (it != numbers.end()) {

cout << "The first number greater than " << value << " is: " << *it << endl;

} else {

cout << "There's no number greater than " << value << " in the list." << endl;

}

return 0;

}

Output

`The first number greater than 7 is: 8`

In this code, we're looking for the first number greater than 7. The upper bound function tells us the position of this number. We use *it to get the number at this position. If it is the same as numbers.end(), it means our number is bigger than any in the list, so there's no upper bound in the sequence.

## Examples

Understanding the upper bound with examples can make things a lot clearer. Let's walk through a couple of scenarios where the upper bound function in C++ comes in handy. This will help you see how you might use it in your own coding projects.

### Example 1: Finding the Upper Bound in a List of Numbers

Imagine you have a list of numbers sorted in ascending order, like this: 1, 2, 4, 4, 5, 7, 8. You want to find the first number that is greater than 4. Using the upper bound function, you can easily locate this number.

Here's how you could write your code:

### C++

`#include <iostream>`

#include <vector>

#include <algorithm>

using namespace std;

int main() {

vector<int> numbers = {1, 2, 4, 4, 5, 7, 8};

int value = 4;

auto it = upper_bound(numbers.begin(), numbers.end(), value);

if (it != numbers.end()) {

cout << "The first number greater than " << value << " is: " << *it << endl;

} else {

cout << "No number is greater than " << value << " in the list." << endl;

}

return 0;

}

Output

`The first number greater than 4 is: 5`

In this example, the upper bound function will find the number 5, because it's the first number greater than 4 in the list.

### Example 2: Using Upper Bound with Custom Objects

Let's say you're working with a list of books, and each book has a page count. You want to find the first book with more than 300 pages. First, you'll need to sort your books by page count. Then, you can use the upper bound function to find the first book that meets your criteria.

Here's an example of how this could look in code:

### C++

`#include <iostream>`

#include <vector>

#include <algorithm>

using namespace std;

class Book {

public:

string title;

int pageCount;

Book(string t, int p) : title(t), pageCount(p) {}

};

bool compareByPageCount(const Book& a, const Book& b) {

return a.pageCount < b.pageCount;

}

int main() {

vector<Book> books = {

Book("Book A", 250),

Book("Book B", 320),

Book("Book C", 150),

Book("Book D", 500),

};

sort(books.begin(), books.end(), compareByPageCount);

int targetPageCount = 300;

auto it = upper_bound(books.begin(), books.end(), targetPageCount, [](int pageCount, const Book& book) {

return pageCount < book.pageCount;

});

if (it != books.end()) {

cout << "The first book with more than " << targetPageCount << " pages is: " << it->title << endl;

} else {

cout << "No book has more than " << targetPageCount << " pages." << endl;

}

return 0;

}

Output

```
The first book with more than 300 pages is: Book B
```

In this scenario, the upper bound function helps you quickly find the first book with more than 300 pages, which is "Book B" with 320 pages.

## Frequently Asked Questions

### What happens if all elements in the sequence are less than the value?

If every element in your sequence is less than the value you're comparing with, the upper bound function will point to the end of the sequence. This means there's no element greater than your specified value.

### Can upper bound be used with non-numeric data, like strings?

Yes, the upper bound function works with any data type that can be sorted and compared, including strings. It's great for finding the insertion point for a specific value in a sorted list of strings.

### Is upper bound different from lower bound?

Yes, there's a difference. While the upper bound finds the first element greater than a given value, the lower bound finds the first element not less than (i.e., greater or equal to) the given value. They are similar but used for slightly different purposes.

## Conclusion

In this article, we have learned about the upper bound function in C++ & its practical applications. Starting with the basics, we explored the syntax, which shows how to use the function in your code. We then looked into the return type, explaining that upper bound returns an iterator pointing to the first element greater than a given value in a sorted sequence.

You can refer to our __guided paths__ on the Coding Ninjas. You can check our course to learn more about __DSA__, __DBMS__, __Competitive Programming__, __Python__, __Java__, __JavaScript,__ etc. Also, check out some of the __Guided Paths__ on topics such as __Data Structure and Algorithms__, __Competitive Programming__, __Operating Systems__, __Computer Networks,__ __DBMS__, __System Design__, etc., as well as some __Contests, ____Test Series__, and __Interview Experiences__ curated by top Industry Experts.