命名空间
变体
操作

std::unordered_map<Key,T,Hash,KeyEqual,Allocator>::at

来自 cppreference.cn
< cpp‎ | 容器‎ | 无序映射
 
 
 
 
T& at( const Key& key );
(1) (C++11 起)
const T& at( const Key& key ) const;
(2) (C++11 起)
template< class K >
T& at( const K& x );
(3) (C++26 起)
template< class K >
const T& at( const K& x ) const;
(4) (C++26 起)

返回具有指定键的元素的映射值的引用。如果不存在此类元素,则抛出 std::out_of_range 类型的异常。

1,2) 键等价于 key
3,4) 键与值 x 比较等价。映射值的引用通过表达式 this->find(x)->second 获取。
表达式 this->find(x) 必须是格式良好的并具有良好定义的行为,否则行为是未定义的。
这些重载仅在 Hash::is_transparentKeyEqual::is_transparent 有效且每个都表示一个类型时才参与重载决议。这假定此类 Hash 可以与 KKey 类型一起调用,并且 KeyEqual 是透明的,这共同允许在不构造 Key 实例的情况下调用此函数。

目录

[edit] 参数

key - 要查找的元素的键
x - 可与键透明比较的任何类型的值

[edit] 返回值

对所请求元素的映射值的引用。

[edit] 异常

1,2) 如果容器中没有具有指定 key 的元素,则抛出 std::out_of_range
3,4) 如果容器中没有指定的元素,即 find(x) == end()true,则抛出 std::out_of_range

[edit] 复杂度

平均情况:常数时间,最坏情况:线性于大小。

注意

特性测试 标准 特性
__cpp_lib_associative_heterogeneous_insertion 202311L (C++26) 有序无序关联容器中其余成员函数的异构重载。(3,4)

[edit] 示例

[edit] 参阅

访问或插入指定元素
(public member function) [edit]
查找具有特定键的元素
(public member function) [edit]