#include#include int main() { std::set mySet = {1, 3, 5, 7, 9}; auto it = mySet.upper_bound(4); std::cout << *it << '\n'; // prints 5 return 0; }
#includeIn this example, we create a set of custom objects `Person` with a comparison operator that compares based on age. We then find the upper bound of a `Person` object with age 30. Since `Charlie` has an age of 35, it is the first object greater than the given `Person`, so the iterator returned points to `Charlie`. Package/library: Standard C++ Library#include #include #include struct Person { std::string name; int age; bool operator<(const Person& other) const { return age < other.age; } }; int main() { std::set mySet = { {"Alice", 25}, {"Bob", 30}, {"Charlie", 35} }; auto it = mySet.upper_bound({"", 30}); std::cout << it->name << '\n'; // prints "Charlie" return 0; }