std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>:: equal_range
From cppreference.net
|
std::
pair
<
iterator, iterator
>
equal_range
(
const
Key
&
key
)
;
|
(1) |
(自 C++23 起)
(自 C++26 起为 constexpr) |
|
std::
pair
<
const_iterator, const_iterator
>
equal_range ( const Key & key ) const ; |
(2) |
(自 C++23 起)
(自 C++26 起为 constexpr) |
|
template
<
class
K
>
std:: pair < iterator, iterator > equal_range ( const K & x ) ; |
(3) |
(自 C++23 起)
(自 C++26 起为 constexpr) |
|
template
<
class
K
>
std::
pair
<
const_iterator, const_iterator
>
|
(4) |
(自 C++23 起)
(自 C++26 起为 constexpr) |
返回一个包含容器中所有具有给定键的元素的区间。该区间由两个迭代器定义,一个指向第一个 不小于 给定键的元素,另一个指向第一个大于给定键的元素。
或者,第一个迭代器可以通过 lower_bound() 获取,第二个迭代器可以通过 upper_bound() 获取。
1,2)
将键与
key
进行比较。
3,4)
将键与值
x
进行比较。
目录 |
参数
| key | - | 用于比较元素的键值 |
| x | - |
可与
Key
进行比较的替代值
|
返回值
std::pair 包含一对定义所需范围的迭代器:
- 第一个迭代器指向第一个不小于给定键的元素,若不存在此类元素则指向 end ( ) 。
- 第二个迭代器指向第一个大于给定键的元素,若不存在此类元素则指向 end ( ) 。
复杂度
与容器大小呈对数关系。
示例
运行此代码
#include <iostream> #include <flat_map> int main() { const std::flat_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
参见
|
查找具有特定键的元素
(公开成员函数) |
|
|
检查容器是否包含具有特定键的元素
(公开成员函数) |
|
|
返回匹配特定键的元素数量
(公开成员函数) |
|
|
返回指向第一个
大于
给定键的元素的迭代器
(公开成员函数) |
|
|
返回指向第一个
不小于
给定键的元素的迭代器
(公开成员函数) |
|
|
返回匹配特定键的元素范围
(函数模板) |