运行这个程序,它按预期工作:它返回结束迭代器。
int main (int argc, char** argv) {
std::set<int> si{ 5, 7, 23, 16, 81, 24, 10};
auto it = si.equal_range(99);
if(it.first == std::end(si)) {
std::cout << "Element not found." << std::endl;
}
else {
std::cout << *it.first << ", " << *it.second << std::endl;
}
return 0;
}
但是,如果我要检查 13,我会回来16, 16
的。
根据cppreference.com
Returns a range containing all elements with the given key in the container. The range is defined by two iterators, one pointing to the first element that is not less than key and another pointing to the first element greater than key. Alternatively, the first iterator may be obtained with lower_bound(), and the second with upper_bound().
在您的示例中,16 恰好是第一个不小于且大于 13 的元素。