std::map<Key,T,Compare,Allocator>:: equal_range
From zh.cppreference.net
|
std::
pair
<
iterator, iterator
>
equal_range
(
const
Key
&
key
)
;
|
(1) | (自 C++26 起为 constexpr) |
|
std::
pair
<
const_iterator, const_iterator
>
equal_range ( const Key & key ) const ; |
(2) | (自 C++26 起为 constexpr) |
|
template
<
class
K
>
std:: pair < iterator, iterator > equal_range ( const K & x ) ; |
(3) |
(自 C++14 起)
(自 C++26 起为 constexpr) |
|
template
<
class
K
>
std::
pair
<
const_iterator, const_iterator
>
|
(4) |
(自 C++14 起)
(自 C++26 起为 constexpr) |
返回一个包含容器中所有具有给定键的元素的区间。该区间由两个迭代器定义,一个指向第一个 不小于 给定键的元素,另一个指向第一个大于给定键的元素。
或者,第一个迭代器可以通过 lower_bound() 获得,第二个迭代器可以通过 upper_bound() 获得。
1,2)
将键与
key
进行比较。
3,4)
将键与值
x
进行比较。
目录参数
返回值std::pair 包含一对定义所需范围的迭代器:
复杂度与容器大小呈对数关系。 注释
示例
运行此代码
#include <iostream> #include <map> int main() { const std::map<int, const char*> m { {0, "zero"}, {1, "one"}, {2, "two"} }; auto p = m.equal_range(1); for (auto& q = p.first; q != p.second; ++q) std::cout << "m[" << q->first << "] = " << q->second << '\n'; if (p.second == m.find(2)) std::cout << "end of equal_range (p.second) is one-past p.first\n"; else std::cout << "unexpected; p.second expected to be one-past p.first\n"; auto pp = m.equal_range(-1); if (pp.first == m.begin()) std::cout << "pp.first is iterator to first not-less than -1\n"; else std::cout << "unexpected pp.first\n"; if (pp.second == m.begin()) std::cout << "pp.second is iterator to first element greater-than -1\n"; else std::cout << "unexpected pp.second\n"; auto ppp = m.equal_range(3); if (ppp.first == m.end()) std::cout << "ppp.first is iterator to first not-less than 3\n"; else std::cout << "unexpected ppp.first\n"; if (ppp.second == m.end()) std::cout << "ppp.second is iterator to first element greater-than 3\n"; else std::cout << "unexpected ppp.second\n"; } 输出: m[1] = one end of equal_range (p.second) is one-past p.first pp.first is iterator to first not-less than -1 pp.second is iterator to first element greater-than -1 ppp.first is iterator to first not-less than 3 ppp.second is iterator to first element greater-than 3 另见
|