Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Closest Element in a Set

Tags:

c++

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?

like image 957
Wug Avatar asked Dec 27 '22 07:12

Wug


1 Answers

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

like image 156
nneonneo Avatar answered Jan 03 '23 10:01

nneonneo