名稱空間
變體
操作

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

來自 cppreference.com
< 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]