Find Closest Element in a Set

邮差的信 提交于 2019-12-12 12:05:42

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/15582893/find-closest-element-in-a-set

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!