Wug
Wug

Reputation: 13196

Find Closest Element in a Set

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?

Upvotes: 4

Views: 6192

Answers (1)

nneonneo
nneonneo

Reputation: 179592

set::lower_bound is the function you are looking for.

Upvotes: 10

Related Questions