命名空间
变体
操作

std::flat_map<Key,T,Compare,KeyContainer,MappedContainer>::equal_range

来自 cppreference.cn
< cpp‎ | 容器‎ | flat map
 
 
 
 
std::pair<iterator, iterator> equal_range( const Key& key );
(1) (C++23 起)
std::pair<const_iterator, const_iterator> equal_range( const Key& key ) const;
(2) (C++23 起)
template< class K >
std::pair<iterator, iterator> equal_range( const K& x );
(3) (C++23 起)
template< class K >
std::pair<const_iterator, const_iterator> equal_range( const K& x ) const;
(4) (C++23 起)

返回包含容器中所有给定键的元素的范围。该范围由两个迭代器定义,一个指向不小于 key 的第一个元素,另一个指向大于 key 的第一个元素。或者,第一个迭代器可以通过 lower_bound() 获得,第二个迭代器可以通过 upper_bound() 获得。

1,2) 将键与 key 进行比较。
3,4) 将键与值 x 进行比较。此重载仅在限定 ID Compare::is_transparent 有效并表示一种类型时才参与重载决议。它允许在不构造 Key 实例的情况下调用此函数。

目录

[编辑] 参数

- 用于比较元素的键值
x - 可与Key比较的替代值

[编辑] 返回值

std::pair,包含一对定义所需范围的迭代器:第一个指向不小于 key 的第一个元素,第二个指向大于 key 的第一个元素。

如果没有不小于 key 的元素,则返回末尾(参见 end())迭代器作为第一个元素。类似地,如果没有大于 key 的元素,则返回末尾迭代器作为第二个元素。

[编辑] 复杂度

容器大小的对数级别。

[编辑] 示例

#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

[编辑] 另请参阅

查找具有特定键的元素
(公共成员函数) [编辑]
检查容器是否包含具有特定键的元素
(公共成员函数) [编辑]
返回匹配特定键的元素数量
(公共成员函数) [编辑]
返回指向第一个大于给定键的元素的迭代器
(公共成员函数) [编辑]
返回指向第一个不小于给定键的元素的迭代器
(公共成员函数) [编辑]
返回与特定键匹配的元素范围
(函数模板) [编辑]