命名空间
变体
操作

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 比较。仅当限定标识 Compare::is_transparent 有效并表示类型时,此重载才参与重载决议。它允许在不构造 Key 实例的情况下调用此函数。

内容

[编辑] 参数

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

[编辑] 返回值

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

如果没有不小于 key 的元素,则将 past-the-end 迭代器(参见 end())作为第一个元素返回。 同样,如果没有大于 key 的元素,则将 past-the-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

[编辑] 参见

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