std::
set::equal_range
公众成员函数 <set>
C++98 |
pair<iterator,iterator> equal_range (const value_type& val) const; |
C++11 |
pair<const_iterator,const_iterator> equal_range (const value_type& val) const;
pair<iterator,iterator> equal_range (const value_type& val); |
获取相等元素的范围
返回包含容器中与val相等的所有元素的范围的边界。
因为set容器中的所有元素都是唯一的,所以返回的范围最多只包含一个元素。
根据容器的内部比较对象(key_comp),如果没有找到匹配,返回的范围的长度为0,
两个迭代器都指向val之后的第一个元素。
如果容器的比较对象返回false(即,无论元素作为参数以何种顺序传递),
则认为set中的两个元素是等效的。
☲ 参数
-
val
-
要比较的值。
成员类型value_type是容器中元素的类型,在set中定义为它的第一个模板形参(T)的别名。
☉ 返回值
该函数返回一个pair,其成员pair::first是范围的下界
(与lower_bound相同),pair::second是范围的上界(与upper_bound相同)。
成员类型iterator和const_iterator是指向元素的双向迭代器类型。
☣ 示例
// set::equal_elements
#include <iostream>
#include <set>
int main ()
{
std::set<int> myset;
for (int i=1; i<=5; i++) myset.insert(i*10); // myset: 10 20 30 40 50
std::pair<std::set<int>::const_iterator,std::set<int>::const_iterator> ret;
ret = myset.equal_range(30);
std::cout << "the lower bound points to: " << *ret.first << '\n';
std::cout << "the upper bound points to: " << *ret.second << '\n';
return 0;
} |
输出:
the lower bound points to: 30
the upper bound points to: 40
✥ 复杂度
集合大小呈对数.
☣ 迭代器的有效性
不变.
⇄ 数据竞争
访问容器(const和非const版本都不会修改容器)。
并发地访问集合的元素是安全的。
☂ 异常安全性
强保证:如果抛出异常,容器中不会有任何变化。
🍄 另请参阅
set::lower_bound |
返回迭代器下界(公众成员函数) |
set::upper_bound |
返回迭代器上界(公众成员函数) |
set::find |
获取元素迭代器(公众成员函数) |
set::count |
具有特定值的元素数量(公众成员函数) |