目录

unordered_map::equal

描述 (Description)

C ++函数std::unordered_map::equal()返回与特定键匹配的元素范围。

在unordered_map容器中,键是唯一的,范围最多包含一个元素。 如果k与容器中的任何键都不匹配,则返回的范围以其下限和上限范围结束。

声明 (Declaration)

以下是std :: unordered_map :: equal()函数形式std :: unordered_map头的声明。

C++11

pair<iterator,iterator> equal_range(const key_type& k);
pair<const_iterator,const_iterator> equal_range(const key_type& k) const;

参数 (Parameters)

k - 要搜索的密钥。

返回值

如果object是常量限定的,则method返回一对常量迭代器,否则返回一对非常量迭代器。

时间复杂

常数,即平均情况下为O(1)。

在最坏的情况下线性即O(n)。

例子 (Example)

以下示例显示了std :: unordered_map :: equal()函数的用法。

#include <iostream>
#include <unordered_map>
using namespace std;
int main(void) {
   unordered_map<char, int> um = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5}
            };
   auto ret = um.equal_range('b');
   cout << "Lower bound is " << ret.first->first 
       << " = "<< ret.first->second << endl;
   cout << "Upper bound is " << ret.second->first
       << " = " << ret.second->second << endl;
   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

Lower bound is b = 2
Upper bound is c = 3
↑回到顶部↑
WIKI教程 @2018