Find Closest Element in a Set
c++, set
Solution
`set::lower_bound` is the function you are looking for.
Problem
Say I have a set, like such: ``` my_set = {"aaron", "cathy", "john", "stewie", "xavier"}; ``` Say I want a function like such: ``` FindFirst(my_set, "a") // returns an iterator pointing to "aaron" FindFirst(my_set, "aaron") // returns an iterator pointing to "aaron" FindFirst(my_set, "bill") // returns an iterator pointing to "cathy" FindFirst(my_set, "zzzzz") // returns past-the-end iterator ``` Basically, it takes a value and returns an iterator either to that element, or the first element after it (picking the past-the-end iterator if the value provided would lie after the end of the set). Does any function like this exist in the standard library, or am I going to have to write one myself?