本文介绍了查找集合中最近的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一套,例如:
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?
推荐答案
set::lower_bound
就是你要找的函数.
set::lower_bound
is the function you are looking for.
这篇关于查找集合中最近的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!