|
|
老实的松鼠 · Android模拟器不再考虑在笔记本触摸屏上 ...· 1 年前 · |
|
|
含蓄的啄木鸟 · 疑难解答 - Tableau· 2 年前 · |
|
|
骑白马的菠萝 · DEM地形操作(geotools方式与nga ...· 2 年前 · |
|
|
欢快的香烟 · 密码学实验_11_DH密钥交换算法(基于椭圆 ...· 2 年前 · |
hash_map
索引鍵資料類型。
要存放在
hash_map
中的項目資料類型。
Traits
包含兩個函式物件的型別,其中一個類別比較能夠將兩個元素值做為排序索引鍵,以判斷其相對順序,而雜湊函式則是一元述詞,會將元素的索引鍵值對應至類型的
size_t
不帶正負號整數。 這個引數是選擇性的,而且
hash_compare<Key, less<Key>>
是預設值。
Allocator
表示預存配置器物件的型別,該物件會封裝記憶體
hash_map
配置和解除配置的詳細資料。 這個引數是選用引數,且預設值是
allocator<pair <const Key, Type>>
。
hash_map
是:
關聯的容器,為可變大小容器,支援項目值以關聯的索引鍵值為基礎、有效率的擷取。
可逆轉的,因為它提供雙向的迭代器以存取其項目。
已經過雜湊處理,因為其項目會依據套用至該項目索引鍵值之雜湊函式的值,分組到不同值區。
唯一,因為它的每個項目都必須具有唯一索引鍵。
一個成對關聯的容器,因為其項目資料值和其索引鍵值是不同的。
類別範本,因為它提供的功能是泛型的,因此與包含為元素或索引鍵的特定資料類型無關。 用於項目或索引鍵的資料類型是在類別樣板中指定為參數 (和比較函式與配置器一起指定)。
透過排序進行雜湊的主要優點是效率更佳;成功的雜湊能執行插入、刪除,相較於和排序技術容器中項目數目對數值成比例的時間,它會以常數平均時間進行搜尋。 中元素
hash_map
的值,但無法直接變更其相關聯的索引鍵值。 相反地,與舊項目相關聯的索引鍵值必須刪除,然後插入與新項目相關聯的新索引鍵值。
選擇容器類型時,通常應根據應用程式所需的搜尋和插入類型。 經過雜湊處理的相關聯容器,已針對查閱、插入及移除作業進行過最佳化。 搭配設計良好的雜湊函式使用時,明確支援這些作業的成員函式,效率相當良好,其會以平均而言為常數的時間執行作業,而與此容器中的項目數目無關。 設計良好的雜湊函式會產生雜湊值的均勻分佈,並盡可能減少衝突發生,當相異索引鍵值對應到相同的雜湊值時,就會發生所謂的衝突。 使用最糟的雜湊函式的最壞情況下,作業數目與序列中的項目數目成正比 (線性時間)。
hash_map
當應用程式滿足將值與其索引鍵建立關聯的條件時,應該是所選的關聯容器。 這個類型結構的模型是已排序的唯一關鍵字清單,舉例而言,其中關聯的字串值可提供定義。 如果字組有一個以上的正確定義,因此索引鍵不是唯一的,則
hash_multimap
會是您選擇的容器。 如果另一方面,只儲存單字清單,則
hash_set
為正確的容器。 如果允許多次出現單字,則
hash_multiset
會是適當的容器結構。
藉
hash_map
由呼叫 類別的預存雜湊
Traits
物件來排序它所控制的順序
value_compare
。 這個預存物件可以藉由呼叫成員函式
key_comp
來存取。 這類函式物件的行為必須與 類別
hash_compare<Key, less<Key>>
的物件相同。 更明確地說,針對
Key
類型的所有
Key
值,
Traits
(
Key
) 呼叫會產生
size_t
類型值的分佈。 如需詳細資訊,請參閱
hash_compare
。
一般而言,元素只需要小於可比較才能建立這個順序:因此,假設有任兩個元素,就可以判斷它們相等的 (,也就是說,兩者都不小於另一個) ,或是另一個元素小於另一個元素。 這會導致非對等元件之間的排序。 一個技術提示,比較函式是在標準數學概念上產生嚴格弱式順序的二元述詞。 二元述詞 f(x y) 是有兩個引數物件
x
和
y
以及傳回值
true
或
false
的函式物件。 如果二元述詞不具彈性、反對稱性和可轉移性,而且如果等價是可轉移的,則加在 上的
hash_map
排序是嚴格的弱式順序,其中兩個 物件
x
和
y
定義為對等,當 f (x、y) 和 f (y、x) 為 時,即為
false
相等。 如果更強的索引鍵相等條件取代等價條件,順序會變成總計 (也就是所有項目彼此相關的排序),因此相符的索引鍵之間將難以辨別。
受控制序列中實際的項目順序取決於雜湊函式、排序函式以及儲存於此容器物件中雜湊資料表目前的大小。 您無法判斷雜湊表的目前大小,因此您無法一般預測受控制序列中的元素順序。 插入項目不會使任何迭代器無效,移除項目則僅會使特別指向被移除項目的迭代器無效。
類別提供的
hash_map
反覆運算器是雙向反覆運算器,但類別成員函
insert
式和
hash_map
版本會採用較弱的輸入反覆運算器作為範本參數,其功能需求比雙向反覆運算器類別所保證的功能需求還要少。 不同的迭代器概念因其功能的修改而形成關聯的系列。 每個迭代器概念有自己的一組需求,因此使用它們的演算法必須將其假設限制為該迭代器類型的需求。 可假設輸入迭代器可能已取值來參考某個物件,而且可能會遞增為序列中的下一個迭代器。 這是一組最基本的功能,但足以在類別成員函式的內容中有意義地討論一系列反覆運算器
[First, Last)
。
一種類型,代表 hash_map 物件的配置器類別。
typedef list<typename Traits::value_type, typename Traits::allocator_type>::allocator_type allocator_type;
get_allocator如需使用 allocator_type 的範例,請參閱 範例。
hash_map::at
這個 API 已經過時。 替代專案是unordered_map Class。
在 hash_map 尋找具有指定索引鍵值的項目。
Type& at(const Key& key);
const Type& at(const Key& key) const;
所要尋找之元素的索引鍵值。
所找到項目之資料值的參考。
如果找不到引數索引鍵值,則函式會擲回 Class 類別out_of_range的物件。
// hash_map_at.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
typedef pair <const int, int> cInt2Int;
hash_map <int, int> hm1;
// Insert data values
hm1.insert ( cInt2Int ( 1, 10 ) );
hm1.insert ( cInt2Int ( 2, 20 ) );
hm1.insert ( cInt2Int ( 3, 30 ) );
cout << "The values of the mapped elements are:";
for ( int i = 1 ; i <= hm1.size() ; i++ )
cout << " " << hm1.at(i);
cout << "." << endl;
hash_map::begin
這個 API 已經過時。 替代專案是unordered_map Class。
傳回迭代器,為 hash_map 中的第一個項目定址。
const_iterator begin() const;
iterator begin();
雙向反覆運算器,定址物件是 中 hash_map 第一個專案,或位置成功是空 hash_map 的 。
// hash_map_begin.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int> hm1;
hash_map <int, int> :: iterator hm1_Iter;
hash_map <int, int> :: const_iterator hm1_cIter;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 0, 0 ) );
hm1.insert ( Int_Pair ( 1, 1 ) );
hm1.insert ( Int_Pair ( 2, 4 ) );
hm1_cIter = hm1.begin ( );
cout << "The first element of hm1 is "
<< hm1_cIter -> first << "." << endl;
hm1_Iter = hm1.begin ( );
hm1.erase ( hm1_Iter );
// The following 2 lines would err because the iterator is const
// hm1_cIter = hm1.begin ( );
// hm1.erase ( hm1_cIter );
hm1_cIter = hm1.begin( );
cout << "The first element of hm1 is now "
<< hm1_cIter -> first << "." << endl;
The first element of hm1 is 0.
The first element of hm1 is now 1.
hash_map::cbegin
這個 API 已經過時。 替代專案是unordered_map Class。
傳回常數迭代器,為 hash_map 中的第一個項目定址。
const_iterator cbegin() const;
常數雙向反覆運算器,定址物件是 中 hash_map 第一個元素,或是空 的 後置 hash_map 位置。
// hash_map_cbegin.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int> hm1;
hash_map <int, int> :: const_iterator hm1_cIter;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 2, 4 ) );
hm1_cIter = hm1.cbegin ( );
cout << "The first element of hm1 is "
<< hm1_cIter -> first << "." << endl;
The first element of hm1 is 2.
hash_map::cend
這個 API 已經過時。 替代專案是unordered_map Class。
傳回常數迭代器,為 hash_map 中最後一個項目的下一個位置定址。
const_iterator cend() const;
const 雙向反覆運算器,定址物件是 中最後一個專案 hash_map 成功的位置。 如果 hash_map 是空的,則 hash_map::cend == hash_map::begin。
cend 是用來測試迭代器是否已到達其 hash_map 的結尾。
所 cend 傳回的值不應被取值。
// hash_map_cend.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int> hm1;
hash_map <int, int> :: const_iterator hm1_cIter;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 3, 30 ) );
hm1_cIter = hm1.cend( );
hm1_cIter--;
cout << "The value of last element of hm1 is "
<< hm1_cIter -> second << "." << endl;
The value of last element of hm1 is 30.
hash_map::clear
這個 API 已經過時。 替代專案是unordered_map Class。
清除 hash_map 的所有項目。
void clear();
下列範例示範如何使用 hash_map::clear 成員函式。
// hash_map_clear.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map<int, int> hm1;
hash_map<int, int>::size_type i;
typedef pair<int, int> Int_Pair;
hm1.insert(Int_Pair(1, 1));
hm1.insert(Int_Pair(2, 4));
i = hm1.size();
cout << "The size of the hash_map is initially "
<< i << "." << endl;
hm1.clear();
i = hm1.size();
cout << "The size of the hash_map after clearing is "
<< i << "." << endl;
The size of the hash_map is initially 2.
The size of the hash_map after clearing is 0.
hash_map::const_iterator
這個 API 已經過時。 替代專案是unordered_map Class。
提供雙向迭代器的類型,這個迭代器可以讀取 const 中的 hash_map 項目。
typedef list<typename Traits::value_type, typename Traits::allocator_type>::const_iterator const_iterator;
類型 const_iterator 無法用來修改元素的值。
所 const_iteratorhash_map 定義的 ,指向 屬於 的物件 value_typepair< const Key, Type > ,其類型為 ,其第一個成員是元素的索引鍵,而第二個成員是元素所持有的對應資料。
若要取值 const_iteratorcIter 指向 中的 hash_map 專案,請使用 -> 運算子。
若要存取 專案的索引鍵值,請使用 cIter->first ,這相當於 (*cIter).first 。 若要存取專案的對應資料值,請使用 cIter->second ,這相當於 (*cIter).second 。
begin如需使用 const_iterator 的範例,請參閱 範例。
hash_map::const_pointer
這個 API 已經過時。 替代專案是unordered_map Class。
類型,提供 const 中 hash_map 項目之指標。
typedef list<typename _Traits::value_type, typename _Traits::allocator_type>::const_pointer const_pointer;
類型 const_pointer 無法用來修改元素的值。
在大部分情況下, iterator 應該使用 來存取 物件中的 hash_map 專案。
hash_map::const_reference
這個 API 已經過時。 替代專案是unordered_map Class。
類型,提供儲存在 const 中供讀取和執行 hash_map 作業之 const 項目的參考。
typedef list<typename _Traits::value_type, typename _Traits::allocator_type>::const_reference const_reference;
// hash_map_const_ref.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map<int, int> hm1;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 1, 10 ) );
hm1.insert ( Int_Pair ( 2, 20 ) );
// Declare and initialize a const_reference &Ref1
// to the key of the first element
const int &Ref1 = ( hm1.begin( ) -> first );
// The following line would cause an error because the
// non-const_reference cannot be used to access the key
// int &Ref1 = ( hm1.begin( ) -> first );
cout << "The key of the first element in the hash_map is "
<< Ref1 << "." << endl;
// Declare and initialize a reference &Ref2
// to the data value of the first element
int &Ref2 = ( hm1.begin( ) -> second );
cout << "The data value of the first element in the hash_map is "
<< Ref2 << "." << endl;
The key of the first element in the hash_map is 1.
The data value of the first element in the hash_map is 10.
hash_map::const_reverse_iterator
這個 API 已經過時。 替代專案是unordered_map Class。
提供雙向迭代器的類型,這個迭代器可以讀取 const 中的任何 hash_map 項目。
typedef list<typename Traits::value_type, typename Traits::allocator_type>::const_reverse)iterator const_reverse_iterator;
類型 const_reverse_iterator 無法修改元素的值,並用來反向逐一查看 hash_map 。
所 const_reverse_iteratorhash_map 定義的 ,指向 屬於 的物件 value_typepair< const Key, Type > ,其類型為 ,其第一個成員是元素的索引鍵,而第二個成員是元素所持有的對應資料。
若要取值 const_reverse_iteratorcrIter 指向 中的 hash_map 專案,請使用 -> 運算子。
若要存取 專案的索引鍵值,請使用 crIter->first ,這相當於 (*crIter).first 。 若要存取專案的對應資料值,請使用 crIter->second ,這相當於 (*crIter).first 。
如需如何宣告和使用 的 const_reverse_iterator 範例,請參閱 範例 rend 。
hash_map::count
這個 API 已經過時。 替代方式為unordered_map Class。
傳回 hash_map 中索引鍵符合參數指定之索引鍵的項目數目。
size_type count(const Key& key) const;
要從 hash_map 比對之專案的索引鍵值。
如果 hash_map 包含排序索引鍵符合參數索引鍵的專案,則為 1;如果 hash_map 不包含具有相符索引鍵的專案,則為 0。
成員函式會傳回範圍中的專案 x 數目
lower_bound(key), upper_bound(key)
在 案例 hash_map 中為 0 或 1,這是唯一的關聯容器。
下列範例示範成員函式的使用 hash_map::count 。
// hash_map_count.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main()
using namespace std;
using namespace stdext;
hash_map<int, int> hm1;
hash_map<int, int>::size_type i;
typedef pair<int, int> Int_Pair;
hm1.insert(Int_Pair (1, 1));
hm1.insert(Int_Pair (2, 1));
hm1.insert(Int_Pair (1, 4));
hm1.insert(Int_Pair (2, 1));
// Keys must be unique in hash_map, so duplicates are ignored
i = hm1.count(1);
cout << "The number of elements in hm1 with a sort key of 1 is: "
<< i << "." << endl;
i = hm1.count(2);
cout << "The number of elements in hm1 with a sort key of 2 is: "
<< i << "." << endl;
i = hm1.count(3);
cout << "The number of elements in hm1 with a sort key of 3 is: "
<< i << "." << endl;
The number of elements in hm1 with a sort key of 1 is: 1.
The number of elements in hm1 with a sort key of 2 is: 1.
The number of elements in hm1 with a sort key of 3 is: 0.
hash_map::crbegin
這個 API 已經過時。 替代方式為unordered_map Class。
const傳回反覆運算器,定址反轉 hash_map 中的第一個專案。
const_reverse_iterator crbegin() const;
const反向雙向反覆運算器,定址反轉 hash_map 中的第一個專案,或定址未反轉 hash_map 中最後一個專案。
crbegin與反轉 hash_map 搭配使用,就像 與 搭配使用一 hash_map 樣 begin 。
使用 的傳回值 crbegin , hash_map 就無法修改物件。
crbegin 可用來向後逐一查看 hash_map。
// hash_map_crbegin.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int> hm1;
hash_map <int, int> :: const_reverse_iterator hm1_crIter;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 3, 30 ) );
hm1_crIter = hm1.crbegin( );
cout << "The first element of the reversed hash_map hm1 is "
<< hm1_crIter -> first << "." << endl;
The first element of the reversed hash_map hm1 is 3.
hash_map::crend
這個 API 已經過時。 替代方式為unordered_map Class。
const傳回反覆運算器,定址物件是反轉 hash_map 中最後一個專案之後的位置。
const_reverse_iterator crend() const;
反 const 轉雙向反覆運算器,定址物件是反轉 hash_map (未反轉 hash_map) 中第一個元素之前的位置。
crend與反轉 hash_map 搭配使用,就像 與 搭配使用一 hash_map 樣 hash_map::end 。
使用 的傳回值 crend , hash_map 就無法修改物件。
crend 可以用來測試反轉迭代器是否已到達其 hash_map 的結尾。
所 crend 傳回的值不應取值。
// hash_map_crend.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int> hm1;
hash_map <int, int> :: const_reverse_iterator hm1_crIter;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 3, 30 ) );
hm1_crIter = hm1.crend( );
hm1_crIter--;
cout << "The last element of the reversed hash_map hm1 is "
<< hm1_crIter -> first << "." << endl;
The last element of the reversed hash_map hm1 is 3.
hash_map::difference_type
這個 API 已經過時。 替代方式為unordered_map Class。
帶正負號的整數類型,可以用來表示範圍 (介於迭代器所指的項目) 中 hash_map 的項目數。
typedef list<typename _Traits::value_type, typename _Traits::allocator_type>::difference_type difference_type;
// hash_map_diff_type.cpp
// compile with: /EHsc
#include <iostream>
#include <hash_map>
#include <algorithm>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int> hm1;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 2, 20 ) );
hm1.insert ( Int_Pair ( 1, 10 ) );
hm1.insert ( Int_Pair ( 3, 20 ) );
// The following won't insert, because map keys are unique
hm1.insert ( Int_Pair ( 2, 30 ) );
hash_map <int, int>::iterator hm1_Iter, hm1_bIter, hm1_eIter;
hm1_bIter = hm1.begin( );
hm1_eIter = hm1.end( );
// Count the number of elements in a hash_map
hash_map <int, int>::difference_type df_count = 0;
hm1_Iter = hm1.begin( );
while ( hm1_Iter != hm1_eIter)
df_count++;
hm1_Iter++;
cout << "The number of elements in the hash_map hm1 is: "
<< df_count << "." << endl;
cout << "The keys of the mapped elements are:";
for ( hm1_Iter= hm1.begin( ) ; hm1_Iter!= hm1.end( ) ;
hm1_Iter++)
cout << " " << hm1_Iter-> first;
cout << "." << endl;
cout << "The values of the mapped elements are:";
for ( hm1_Iter= hm1.begin( ) ; hm1_Iter!= hm1.end( ) ;
hm1_Iter++)
cout << " " << hm1_Iter-> second;
cout << "." << endl;
The number of elements in the hash_map hm1 is: 3.
The keys of the mapped elements are: 1 2 3.
The values of the mapped elements are: 10 20 20.
hash_map::emplace
這個 API 已經過時。 替代方式為unordered_map Class。
將就地建構的項目插入 hash_map 中。
template <class ValTy>
pair <iterator, bool>
emplace(
ValTy&& val);
值,用來移動要插入 hash_map 的專案,除非 hash_map 已經包含該元素 (,或更一般而言,索引鍵相等排序的專案) 。
成員 emplace 函式會傳回 true 一對 bool ,如果已建立插入,而且 false 如果 hash_map 已經包含索引鍵具有對等值的元素,而且 iterator 其元件會傳回插入新元素的位址,或是元素已位於何處,則傳回其元件。
若要存取此成員函式所傳回之配對 pr 的 iterator 元件,請使用 pr.first,若要取其值,請使用 *(pr.first)。 若要存取此成員函式所傳回之配對 pr 的 bool 元件,請使用 pr.second,若要取其值,請使用 *(pr.second)。
hash_map::value_type專案的 是配對,因此元素的值會是排序的配對,第一個元件等於索引鍵值,而第二個元件等於元素的資料值。
// hash_map_emplace.cpp
// compile with: /EHsc
#include<hash_map>
#include<iostream>
#include <string>
int main()
using namespace std;
using namespace stdext;
hash_map<int, string> hm1;
typedef pair<int, string> is1(1, "a");
hm1.emplace(move(is1));
cout << "After the emplace insertion, hm1 contains:" << endl
<< " " << hm1.begin()->first
<< " => " << hm1.begin()->second
<< endl;
After the emplace insertion, hm1 contains:
1 => a
hash_map::emplace_hint
這個 API 已經過時。 替代方式為unordered_map Class。
將就地建構的項目 (含位置提示) 插入 hash_map 中。
template <class ValTy>
iterator emplace_hint(
const_iterator _Where,
ValTy&& val);
值,用來移動要插入 hash_map 的專案,除非 hash_map 已經包含該元素 (,或更一般而言,索引鍵相等排序的專案) 。
一個有關要從哪裡開始搜尋正確插入點的提示。
成員函式會 hash_multimap::emplace 傳回反覆運算器,指向新專案插入 至 hash_map 的位置,或是具有對等順序的現有專案所在的位置。
hash_map::value_type專案的 是配對,因此元素的值會是排序的配對,第一個元件等於索引鍵值,而第二個元件等於元素的資料值。
如果插入點緊接在 _Where 之後,便可以分攤的常數時間 (而不是對數時間) 進行插入。
// hash_map_emplace_hint.cpp
// compile with: /EHsc
#include<hash_map>
#include<iostream>
#include <string>
int main()
using namespace std;
using namespace stdext;
hash_map<int, string> hm1;
typedef pair<int, string> is1(1, "a");
hm1.emplace(hm1.begin(), move(is1));
cout << "After the emplace, hm1 contains:" << endl
<< " " << hm1.begin()->first
<< " => " << hm1.begin()->second
<< endl;
After the emplace insertion, hm1 contains:
1 => a
hash_map::empty
這個 API 已經過時。 替代方式為unordered_map Class。
測試 hash_map 是否為空白。
bool empty() const;
truehash_map如果 是空的, falsehash_map 則為 ,如果 為 nonempty,則為 。
// hash_map_empty.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int> hm1, hm2;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 1, 1 ) );
if ( hm1.empty( ) )
cout << "The hash_map hm1 is empty." << endl;
cout << "The hash_map hm1 is not empty." << endl;
if ( hm2.empty( ) )
cout << "The hash_map hm2 is empty." << endl;
cout << "The hash_map hm2 is not empty." << endl;
The hash_map hm1 is not empty.
The hash_map hm2 is empty.
hash_map::end
這個 API 已經過時。 替代方式為unordered_map Class。
傳回迭代器,為 hash_map 中最後一個項目的下一個位置定址。
const_iterator end() const;
iterator end();
雙向反覆運算器,定址物件是 中最後一個 hash_map 元素後置的位置。 如果 hash_map 是空的,則 hash_map::end == hash_map::begin。
end 是用來測試迭代器是否已到達其 hash_map 的結尾。
所 end 傳回的值不應取值。
// hash_map_end.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int> hm1;
hash_map <int, int> :: iterator hm1_Iter;
hash_map <int, int> :: const_iterator hm1_cIter;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 1, 10 ) );
hm1.insert ( Int_Pair ( 2, 20 ) );
hm1.insert ( Int_Pair ( 3, 30 ) );
hm1_cIter = hm1.end( );
hm1_cIter--;
cout << "The value of last element of hm1 is "
<< hm1_cIter -> second << "." << endl;
hm1_Iter = hm1.end( );
hm1_Iter--;
hm1.erase ( hm1_Iter );
// The following 2 lines would err because the iterator is const
// hm1_cIter = hm1.end ( );
// hm1_cIter--;
// hm1.erase ( hm1_cIter );
hm1_cIter = hm1.end( );
hm1_cIter--;
cout << "The value of last element of hm1 is now "
<< hm1_cIter -> second << "." << endl;
The value of last element of hm1 is 30.
The value of last element of hm1 is now 20.
hash_map::equal_range
這個 API 已經過時。 替代方式為unordered_map Class。
傳回成對的迭代器,分別指向 hash_map 中索引鍵大於特定索引鍵的第一個項目,以及指向 hash_map 中索引鍵等於或大於該索引鍵的第一個項目。
pair <const_iterator, const_iterator> equal_range (const Key& key) const;
pair <iterator, iterator> equal_range (const Key& key);
要與所搜尋之 專案的排序索引鍵比較的 hash_map 引數索引鍵值。
一對反覆運算器,讓第一個是 lower_bound 索引鍵的 ,而第二個則是 upper_bound 索引鍵的 。
若要存取成員函式所傳回之配對 pr 的第一個反覆運算器,請使用 pr.first 和 來取值下限反覆運算器,請使用 *(pr.first) 。 若要存取成員函式所傳回之配對 pr 的第二個反覆運算器,請使用 pr.second 和 來取值上限反覆運算器,請使用 *(pr.second) 。
// hash_map_equal_range.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
typedef hash_map <int, int> IntMap;
IntMap hm1;
hash_map <int, int> :: const_iterator hm1_RcIter;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 1, 10 ) );
hm1.insert ( Int_Pair ( 2, 20 ) );
hm1.insert ( Int_Pair ( 3, 30 ) );
pair <IntMap::const_iterator, IntMap::const_iterator> p1, p2;
p1 = hm1.equal_range( 2 );
cout << "The lower bound of the element with "
<< "a key of 2 in the hash_map hm1 is: "
<< p1.first -> second << "." << endl;
cout << "The upper bound of the element with "
<< "a key of 2 in the hash_map hm1 is: "
<< p1.second -> second << "." << endl;
// Compare the upper_bound called directly
hm1_RcIter = hm1.upper_bound( 2 );
cout << "A direct call of upper_bound( 2 ) gives "
<< hm1_RcIter -> second << "," << endl
<< "matching the 2nd element of the pair"
<< " returned by equal_range( 2 )." << endl;
p2 = hm1.equal_range( 4 );
// If no match is found for the key,
// both elements of the pair return end( )
if ( ( p2.first == hm1.end( ) ) && ( p2.second == hm1.end( ) ) )
cout << "The hash_map hm1 doesn't have an element "
<< "with a key less than 40." << endl;
cout << "The element of hash_map hm1 with a key >= 40 is: "
<< p1.first -> first << "." << endl;
The lower bound of the element with a key of 2 in the hash_map hm1 is: 20.
The upper bound of the element with a key of 2 in the hash_map hm1 is: 30.
A direct call of upper_bound( 2 ) gives 30,
matching the 2nd element of the pair returned by equal_range( 2 ).
The hash_map hm1 doesn't have an element with a key less than 40.
hash_map::erase
這個 API 已經過時。 替代專案是unordered_map Class。
從指定的位置移除 hash_map 中的項目或項目範圍,或移除符合指定之索引鍵的項目。
iterator erase(iterator _Where);
iterator erase(iterator first, iterator last);
size_type erase(const key_type& key);
_Where
要從 hash_map 移除的專案位置。
first
從 hash_map 移除的第一個專案位置。
位置剛好超出從 hash_map 中移除的最後一個專案。
要從 hash_map 中移除之專案的索引鍵值。
在前兩個成員函式中,雙向反覆運算器會指定已移除之任何元素以外的第一個元素,或如果沒有任何這類專案存在,則指定 結尾的 hash_map 指標。
針對第三個成員函式,傳回已從 hash_map 移除的專案數目。
成員函式永遠不會擲回例外狀況。
下列範例示範如何使用 hash_map::erase 成員函式。
// hash_map_erase.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main()
using namespace std;
using namespace stdext;
hash_map<int, int> hm1, hm2, hm3;
hash_map<int, int> :: iterator pIter, Iter1, Iter2;
int i;
hash_map<int, int>::size_type n;
typedef pair<int, int> Int_Pair;
for (i = 1; i < 5; i++)
hm1.insert(Int_Pair (i, i));
hm2.insert(Int_Pair (i, i*i));
hm3.insert(Int_Pair (i, i-1));
// The 1st member function removes an element at a given position
Iter1 = ++hm1.begin();
hm1.erase(Iter1);
cout << "After the 2nd element is deleted, the hash_map hm1 is:";
for (pIter = hm1.begin(); pIter != hm1.end(); pIter++)
cout << " " << pIter -> second;
cout << "." << endl;
// The 2nd member function removes elements
// in the range [ first, last)
Iter1 = ++hm2.begin();
Iter2 = --hm2.end();
hm2.erase(Iter1, Iter2);
cout << "After the middle two elements are deleted, "
<< "the hash_map hm2 is:";
for (pIter = hm2.begin(); pIter != hm2.end(); pIter++)
cout << " " << pIter -> second;
cout << "." << endl;
// The 3rd member function removes elements with a given key
n = hm3.erase(2);
cout << "After the element with a key of 2 is deleted,\n"
<< "the hash_map hm3 is:";
for (pIter = hm3.begin(); pIter != hm3.end(); pIter++)
cout << " " << pIter -> second;
cout << "." << endl;
// The 3rd member function returns the number of elements removed
cout << "The number of elements removed from hm3 is: "
<< n << "." << endl;
// The dereferenced iterator can also be used to specify a key
Iter1 = ++hm3.begin();
hm3.erase(Iter1);
cout << "After another element with a key equal to that"
<< endl;
cout << "of the 2nd element is deleted, "
<< "the hash_map hm3 is:";
for (pIter = hm3.begin(); pIter != hm3.end(); pIter++)
cout << " " << pIter -> second;
cout << "." << endl;
After the 2nd element is deleted, the hash_map hm1 is: 1 3 4.
After the middle two elements are deleted, the hash_map hm2 is: 1 16.
After the element with a key of 2 is deleted,
the hash_map hm3 is: 0 2 3.
The number of elements removed from hm3 is: 1.
After another element with a key equal to that
of the 2nd element is deleted, the hash_map hm3 is: 0 3.
hash_map::find
這個 API 已經過時。 替代專案是unordered_map Class。
傳回迭代器,為 hash_map 中索引鍵等於指定索引鍵項目位置定址。
iterator find(const Key& key);
const_iterator find(const Key& key) const;
要與所搜尋專案的排序索引鍵相符的 hash_map 索引鍵值。
反覆運算器,定址物件為具有指定索引鍵的專案位置,如果找不到索引鍵相符專案,則為 中最後 hash_map 一個專案的位置。
find 會傳回反覆運算器,其 hash_map 排序索引鍵相當於二進位述詞下的引數索引鍵,其會根據小於比較性關聯性引發排序。
如果 的傳回值 find 指派給 const_iterator , hash_map 則無法修改 物件。 如果 的傳回值 find 指派給 iterator , hash_map 則可以修改 物件
// hash_map_find.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int> hm1;
hash_map <int, int> :: const_iterator hm1_AcIter, hm1_RcIter;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 1, 10 ) );
hm1.insert ( Int_Pair ( 2, 20 ) );
hm1.insert ( Int_Pair ( 3, 30 ) );
hm1_RcIter = hm1.find( 2 );
cout << "The element of hash_map hm1 with a key of 2 is: "
<< hm1_RcIter -> second << "." << endl;
// If no match is found for the key, end( ) is returned
hm1_RcIter = hm1.find( 4 );
if ( hm1_RcIter == hm1.end( ) )
cout << "The hash_map hm1 doesn't have an element "
<< "with a key of 4." << endl;
cout << "The element of hash_map hm1 with a key of 4 is: "
<< hm1_RcIter -> second << "." << endl;
// The element at a specific location in the hash_map can be found
// using a dereferenced iterator addressing the location
hm1_AcIter = hm1.end( );
hm1_AcIter--;
hm1_RcIter = hm1.find( hm1_AcIter -> first );
cout << "The element of hm1 with a key matching "
<< "that of the last element is: "
<< hm1_RcIter -> second << "." << endl;
The element of hash_map hm1 with a key of 2 is: 20.
The hash_map hm1 doesn't have an element with a key of 4.
The element of hm1 with a key matching that of the last element is: 30.
hash_map::get_allocator
這個 API 已經過時。 替代專案是unordered_map Class。
傳回用來建構 hash_map 之配置器物件的複本。
Allocator get_allocator() const;
所使用的 hash_map 配置器。
類別的配置 hash_map 器會指定類別管理儲存體的方式。 C++ 標準程式庫容器類別隨附的預設配置器即足以滿足大多數程式設計需求。 撰寫和使用您自己的配置器類別是進階 C++ 主題。
// hash_map_get_allocator.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int>::allocator_type hm1_Alloc;
hash_map <int, int>::allocator_type hm2_Alloc;
hash_map <int, double>::allocator_type hm3_Alloc;
hash_map <int, int>::allocator_type hm4_Alloc;
// The following lines declare objects
// that use the default allocator.
hash_map <int, int> hm1;
hash_map <int, int> hm2;
hash_map <int, double> hm3;
hm1_Alloc = hm1.get_allocator( );
hm2_Alloc = hm2.get_allocator( );
hm3_Alloc = hm3.get_allocator( );
cout << "The number of integers that can be allocated"
<< endl << "before free memory is exhausted: "
<< hm2.max_size( ) << "." << endl;
cout << "The number of doubles that can be allocated"
<< endl << "before free memory is exhausted: "
<< hm3.max_size( ) << "." << endl;
// The following line creates a hash_map hm4
// with the allocator of hash_map hm1.
hash_map <int, int> hm4( less<int>( ), hm1_Alloc );
hm4_Alloc = hm4.get_allocator( );
// Two allocators are interchangeable if
// storage allocated from each can be
// deallocated with the other
if( hm1_Alloc == hm4_Alloc )
cout << "The allocators are interchangeable."
<< endl;
cout << "The allocators are not interchangeable."
<< endl;
hash_map::hash_map
這個 API 已經過時。 替代專案是unordered_map Class。
hash_map建構空的 ,或是其他 hash_map 所有或部分的複本。
hash_map();
explicit hash_map(
const Traits& Comp);
hash_map(
const Traits& Comp,
const Allocator& Al);
hash_map(
const hash_map& Right);
hash_map(
hash_map&& Right);
hash_map(
initializer_list<Type> IList);hash_map(initializer_list<Type> IList,
const key_compare& Comp);
hash_map(
initializer_list<Type> IList,
const key_compare& Comp,
const allocator_type& Al);
template <class InputIterator>
hash_map(
InputIterator First,
InputIterator Last);
template <class InputIterator>
hash_map(
InputIterator First,
InputIterator Last,
const Traits& Comp);
template <class InputIterator>
hash_map(
InputIterator First,
InputIterator Last,
const Traits& Comp,
const Allocator& Al
要用於此 hash_map 物件的儲存體配置器類別,預設為 Allocator。
const Traits 類型的比較函式,用來排序 中的 hash_map 專案,其預設為 hash_compare 。
Right
hash_map建構的對應為複本的 。
First
要複製的元素範圍中第一個元素的位置。
超出要複製之元素範圍的第一個元素的位置。
IList
initializer_list
所有建構函式都會儲存一種配置器物件,其可管理 的 hash_map 記憶體儲存體,稍後可以呼叫 get_allocator 來傳回 。 在類別宣告以及用來取代替代配置器的前置處理巨集中,經常會省略 allocator 參數。
所有建構函式都會初始化其 hash_map 。
所有建構函式都會儲存型 Traits 別的函式物件,用來在 的 hash_map 索引鍵之間建立順序,稍後可以呼叫 key_comp 來傳回。
前三個建構函式會指定空的初始 hash_map ,此外,第二個建構函式會指定比較函式的類型, Comp () 用來建立元素的順序,而第三個建構函式會明確指定配置器類型 (Al) 使用。 關鍵字 explicit 會隱藏某些類型的自動類型轉換。
第四個建構函式會指定 的 hash_mapRight 複本。
接下來的三個建構函式會複製 [First, Last)hash_map 範圍,並在指定 類別 Traits 和配置器的比較函式類型時增加明確性。
最後一個建構函式會移動 hash_mapRight 。
hash_map::insert
這個 API 已經過時。 替代專案是unordered_map Class。
將項目或項目範圍插入至 hash_map。
pair <iterator, bool> insert(
const value_type& val);
iterator insert(
const_iterator _Where,
const value_type& val);
template <class InputIterator>
void insert(
InputIterator first,
InputIterator last);
template <class ValTy>
pair <iterator, bool>
insert(
ValTy&& val);
template <class ValTy>
iterator insert(
const_iterator _Where,
ValTy&& val);
要插入 hash_map 的專案值,除非 hash_map 已經包含該元素 (,或更一般地,其索引鍵相當於排序) 的專案。
_Where
一個有關要從哪裡開始搜尋正確插入點的提示。
first
要從 hash_map 複製之第一個元素的位置。
緊接在要從 hash_map 複製之最後一個元素後面的位置。
第一個 insert 成員函式會傳回元件傳回 true 的配對 bool ,如果已建立插入,而且 false 如果 hash_map 已經包含索引鍵具有對等值的專案,且反覆運算器元件會傳回插入新元素的位址,或元素已位於何處。
若要存取此成員函式所傳回之配對 pr 的 iterator 元件,請使用 pr.first,若要取其值,請使用 (pr.first)。 若要存取此成員函式所傳回之配對 pr 的 bool 元件,請使用 pr.second,若要取其值,請使用 \(pr.second)。
第二 insert 個成員函式提示版本會傳回反覆運算器,指向插入 hash_map 新元素的位置。
最後兩個成員函式的行為與前兩 insert 個函式相同,不同之處在于它們移動建構插入的值。
value_type元素的 是一組,因此元素的值會是一個排序的配對,其中第一個元件等於索引鍵值,而第二個元件等於元素的資料值。
針對插入的提示版本,如果插入點緊接在 _Where 之後,便可以分攤的常數時間 (而不是對數時間) 進行插入。
第三個成員函式會將專案值序列插入對應至 hash_map 指定集合範圍內 *[First, Last)* 反覆運算器所定址的每個元素。
// hash_map_insert.cpp
// compile with: /EHsc
#include<hash_map>
#include<iostream>
#include <string>
int main()
using namespace std;
using namespace stdext;
hash_map<int, int>::iterator hm1_pIter, hm2_pIter;
hash_map<int, int> hm1, hm2;
typedef pair<int, int> Int_Pair;
hm1.insert(Int_Pair(1, 10));
hm1.insert(Int_Pair(2, 20));
hm1.insert(Int_Pair(3, 30));
hm1.insert(Int_Pair(4, 40));
cout << "The original elements (Key => Value) of hm1 are:";
for (hm1_pIter = hm1.begin(); hm1_pIter != hm1.end(); hm1_pIter++)
cout << endl << " " << hm1_pIter -> first << " => "
<< hm1_pIter->second;
cout << endl;
pair< hash_map<int,int>::iterator, bool > pr;
pr = hm1.insert(Int_Pair(1, 10));
if (pr.second == true)
cout << "The element 10 was inserted in hm1 successfully."
<< endl;
cout << "The element 10 already exists in hm1\n"
<< "with a key value of "
<< "((pr.first) -> first) = " << (pr.first)->first
<< "." << endl;
// The hint version of insert
hm1.insert(--hm1.end(), Int_Pair(5, 50));
cout << "After the insertions, the elements of hm1 are:";
for (hm1_pIter = hm1.begin(); hm1_pIter != hm1.end(); hm1_pIter++)
cout << endl << hm1_pIter -> first << " => "
<< hm1_pIter->second;
cout << endl;
hm2.insert(Int_Pair(10, 100));
// The templatized version inserting a range
hm2.insert( ++hm1.begin(), --hm1.end() );
cout << "After the insertions, the elements of hm2 are:";
for (hm2_pIter = hm2.begin(); hm2_pIter != hm2.end(); hm2_pIter++)
cout << endl << hm2_pIter -> first << " => "
<< hm2_pIter->second;
cout << endl;
// The templatized versions move constructing elements
hash_map<int, string> hm3, hm4;
pair<int, string> is1(1, "a"), is2(2, "b");
hm3.insert(move(is1));
cout << "After the move insertion, hm3 contains:" << endl
<< hm3.begin()->first
<< " => " << hm3.begin()->second
<< endl;
hm4.insert(hm4.begin(), move(is2));
cout << "After the move insertion, hm4 contains:" << endl
<< hm4.begin()->first
<< " => " << hm4.begin()->second
<< endl;
The original elements (Key => Value) of hm1 are:
1 => 10
2 => 20
3 => 30
4 => 40
The element 10 already exists in hm1
with a key value of ((pr.first) -> first) = 1.
After the insertions, the elements of hm1 are:
1 => 10
2 => 20
3 => 30
4 => 40
5 => 50
After the insertions, the elements of hm2 are:
2 => 20
10 => 100
3 => 30
4 => 40
After the move insertion, hm3 contains:
1 => a
After the move insertion, hm4 contains:
2 => b
hash_map::iterator
這個 API 已經過時。 替代專案是unordered_map Class。
類型,其提供可讀取或修改 hash_map 中任何項目的雙向迭代器。
typedef list<typename Traits::value_type, typename Traits::allocator_type>::iterator iterator;
所 iteratorhash_map 定義的 ,指向 屬於 的物件 value_typepair<const Key, Type> ,其類型為 ,其第一個成員是元素的索引鍵,而第二個成員是元素所持有的對應資料。
若要取值指向 multimap 中元素的 Iter 反覆運算器,請使用 -> 運算子。
若要存取 專案的索引鍵值,請使用 Iter->first ,這相當於 (*Iter).first 。 若要存取專案的對應資料值,請使用 Iter->second ,這相當於 (*Iter).second 。
型 iterator 別可用來修改元素的值。
begin如需如何宣告和使用 的範例, iterator 請參閱 範例。
hash_map::key_comp
這個 API 已經過時。 替代專案是unordered_map Class。
擷取 hash_map 中用來排序索引鍵的比較物件之複本。
key_compare key_comp() const;
傳回 用來排序其專案的函式物件 hash_map 。
預存物件會定義成員函式
bool operator( const Key& left, const Key&right );
,如果 left 之前和 不等於 right 排序次序,則會傳回 true 。
// hash_map_key_comp.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int, hash_compare<int, less<int> > > hm1;
hash_map <int, int, hash_compare<int, less<int> > >::key_compare
kc1 = hm1.key_comp( ) ;
// Operator stored in kc1 tests order & returns bool value
bool result1 = kc1( 2, 3 ) ;
if( result1 == true )
cout << "kc1( 2,3 ) returns value of true,"
<< "\n where kc1 is the function object of hm1"
<< " of type key_compare." << endl;
cout << "kc1( 2,3 ) returns value of false"
<< "\n where kc1 is the function object of hm1"
<< " of type key_compare." << endl;
hash_map <int, int, hash_compare<int, greater<int> > > hm2;
hash_map <int, int, hash_compare<int, greater<int> > >
::key_compare kc2 = hm2.key_comp( );
// Operator stored in kc2 tests order & returns bool value
bool result2 = kc2( 2, 3 ) ;
if( result2 == true )
cout << "kc2( 2,3 ) returns value of true,"
<< "\n where kc2 is the function object of hm2"
<< " of type key_compare." << endl;
cout << "kc2( 2,3 ) returns value of false,"
<< "\n where kc2 is the function object of hm2"
<< " of type key_compare." << endl;
hash_map::key_compare
這個 API 已經過時。 替代方式為unordered_map Class。
一種提供函式物件的類型,該函式物件可比較兩個排序鍵來判斷對應中兩個元素的相對順序。
typedef Traits key_compare;
key_compare 與樣板參數 Traits 同義。
如需詳細資訊 Traits ,hash_map 請參閱類別主題。
如需如何宣告和使用 key_compare 的範例,請參閱 範例 key_comp 。
hash_map::key_type
這個 API 已經過時。 替代方式為unordered_map Class。
類型,描述構成 hash_map 每個項目的排序鍵物件。
typedef Key key_type;
key_type 與樣板參數 Key 同義。
如需 的詳細資訊 Key ,請參閱 類別主題的hash_map一節。
如需如何宣告和使用 key_type 的範例,請參閱 範例 value_type 。
hash_map::lower_bound
這個 API 已經過時。 替代方式為unordered_map Class。
傳回迭代器,指向 hash_map 中索引鍵值等於或大於特定索引鍵值的第一個項目。
iterator lower_bound(const Key& key);
const_iterator lower_bound(const Key& key) const;
要與所搜尋之 專案的排序索引鍵比較的 hash_map 引數索引鍵值。
或 iteratorconst_iterator ,其可解決索引鍵等於或大於引數索引鍵之 中 hash_map 專案的位置,如果找不到索引鍵相符的專案,則為 中最後一個專案 hash_map 成功的位置。
如果將 的 lower_bound 傳回值指派給 const_iterator , hash_map 則無法修改 物件。 如果將 的 lower_bound 傳回值指派給 iterator , hash_map 則可以修改 物件。
// hash_map_lower_bound.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int> hm1;
hash_map <int, int> :: const_iterator hm1_AcIter, hm1_RcIter;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 1, 10 ) );
hm1.insert ( Int_Pair ( 2, 20 ) );
hm1.insert ( Int_Pair ( 3, 30 ) );
hm1_RcIter = hm1.lower_bound( 2 );
cout << "The first element of hash_map hm1 with a key of 2 is: "
<< hm1_RcIter -> second << "." << endl;
// If no match is found for the key, end( ) is returned
hm1_RcIter = hm1. lower_bound ( 4 );
if ( hm1_RcIter == hm1.end( ) )
cout << "The hash_map hm1 doesn't have an element "
<< "with a key of 4." << endl;
cout << "The element of hash_map hm1 with a key of 4 is: "
<< hm1_RcIter -> second << "." << endl;
// An element at a specific location in the hash_map can be
// found using a dereferenced iterator addressing the location
hm1_AcIter = hm1.end( );
hm1_AcIter--;
hm1_RcIter = hm1. lower_bound ( hm1_AcIter -> first );
cout << "The element of hm1 with a key matching "
<< "that of the last element is: "
<< hm1_RcIter -> second << "." << endl;
The first element of hash_map hm1 with a key of 2 is: 20.
The hash_map hm1 doesn't have an element with a key of 4.
The element of hm1 with a key matching that of the last element is: 30.
hash_map::mapped_type
這個 API 已經過時。 替代方式為unordered_map Class。
類型,表示儲存在 hash_map 中的資料類型。
typedef Type mapped_type;
類型 mapped_type 與範本參數 Type 同義。
如需詳細資訊 Type ,hash_map 請參閱類別主題。
如需如何宣告和使用 key_type 的範例,請參閱 範例 value_type 。
hash_map::max_size
這個 API 已經過時。 替代方式為unordered_map Class。
傳回 hash_map 的最大長度。
size_type max_size() const;
的最大可能長度 hash_map 。
// hash_map_max_size.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int> hm1;
hash_map <int, int> :: size_type i;
i = hm1.max_size( );
cout << "The maximum possible length "
<< "of the hash_map is " << i << "."
<< endl << "(Magnitude is machine specific.)";
hash_map::operator[]
這個 API 已經過時。 替代方式為unordered_map Class。
將具有特定索引鍵值的項目插入 hash_map 中。
Type& operator[](const Key& key);
Type& operator[](Key&& key);
所要插入之元素的索引鍵值。
所插入項目之資料值的參考。
如果找不到引數索引鍵值,則會與資料類型的預設值一起插入。
operator[] 可用來將元素插入到 hash_map m,方法是使用
m[ key] = DataValue;
其中 DataValue 是索引鍵值為 key 之元素的 mapped_type 值。
使用 operator[] 插入專案時,傳回的參考不會指出插入是變更既有的元素還是建立新的專案。 成員函式 find 和 insert 可用來判斷具有指定索引鍵的專案是否已經在插入之前存在。
// hash_map_op_ref.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
#include <string>
int main( )
using namespace std;
using namespace stdext;
typedef pair <const int, int> cInt2Int;
hash_map <int, int> hm1;
hash_map <int, int> :: iterator pIter;
// Insert a data value of 10 with a key of 1
// into a hash_map using the operator[] member function
hm1[ 1 ] = 10;
// Compare other ways to insert objects into a hash_map
hm1.insert ( hash_map <int, int> :: value_type ( 2, 20 ) );
hm1.insert ( cInt2Int ( 3, 30 ) );
cout << "The keys of the mapped elements are:";
for ( pIter = hm1.begin( ) ; pIter != hm1.end( ) ; pIter++ )
cout << " " << pIter -> first;
cout << "." << endl;
cout << "The values of the mapped elements are:";
for ( pIter = hm1.begin( ) ; pIter != hm1.end( ) ; pIter++ )
cout << " " << pIter -> second;
cout << "." << endl;
// If the key already exists, operator[]
// changes the value of the datum in the element
hm1[ 2 ] = 40;
// operator[] will also insert the value of the data
// type's default constructor if the value is unspecified
hm1[5];
cout << "The keys of the mapped elements are now:";
for ( pIter = hm1.begin( ) ; pIter != hm1.end( ) ; pIter++ )
cout << " " << pIter -> first;
cout << "." << endl;
cout << "The values of the mapped elements are now:";
for ( pIter = hm1.begin( ) ; pIter != hm1.end( ) ; pIter++ )
cout << " " << pIter -> second;
cout << "." << endl;
// operator[] will also insert by moving a key
hash_map <string, int> hm2;
string str("a");
hm2[move(str)] = 1;
cout << "The moved key is " << hm2.begin()->first
<< ", with value " << hm2.begin()->second << endl;
hash_map::operator=
這個 API 已經過時。 替代方式為unordered_map Class。
用另一個 hash_map 的複本取代 hash_map 的項目。
hash_map& operator=(const hash_map& right);
hash_map& operator=(hash_map&& right);
right
要right複製到 的 hash_map 類別。
清除 hash_map 中的任何現有項目之後,operator= 會將 right 的內容複製或移到 hash_map 中。
// hash_map_operator_as.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map<int, int> v1, v2, v3;
hash_map<int, int>::iterator iter;
v1.insert(pair<int, int>(1, 10));
cout << "v1 = " ;
for (iter = v1.begin(); iter != v1.end(); iter++)
cout << iter->second << " ";
cout << endl;
v2 = v1;
cout << "v2 = ";
for (iter = v2.begin(); iter != v2.end(); iter++)
cout << iter->second << " ";
cout << endl;
// move v1 into v2
v2.clear();
v2 = move(v1);
cout << "v2 = ";
for (iter = v2.begin(); iter != v2.end(); iter++)
cout << iter->second << " ";
cout << endl;
hash_map::pointer
這個 API 已經過時。 替代方式為unordered_map Class。
類型,其提供 hash_map 中項目的指標。
typedef list<typename _Traits::value_type, typename _Traits::allocator_type>::pointer pointer;
型 pointer 別可用來修改專案的值。
在大部分情況下, iterator 應該使用 來存取 物件中的 hash_map 專案。
hash_map::rbegin
這個 API 已經過時。 替代方式為unordered_map Class。
傳回迭代器,為反轉 hash_map 中的第一個項目定址。
const_reverse_iterator rbegin() const;
reverse_iterator rbegin();
反向雙向反覆運算器,定址反轉 hash_map 中的第一個專案,或定址未反轉 hash_map 中最後一個專案。
rbegin與反轉 hash_map 搭配使用,就像 與 搭配使用一 hash_map 樣 begin 。
如果 的傳回值 rbegin 指派給 const_reverse_iterator , hash_map 則無法修改物件。 如果 的傳回值 rbegin 指派給 reverse_iterator , hash_map 則可以修改物件。
rbegin 可用來向後逐一查看 hash_map。
// hash_map_rbegin.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int> hm1;
hash_map <int, int> :: iterator hm1_Iter;
hash_map <int, int> :: reverse_iterator hm1_rIter;
hash_map <int, int> :: const_reverse_iterator hm1_crIter;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 1, 10 ) );
hm1.insert ( Int_Pair ( 2, 20 ) );
hm1.insert ( Int_Pair ( 3, 30 ) );
hm1_rIter = hm1.rbegin( );
cout << "The first element of the reversed hash_map hm1 is "
<< hm1_rIter -> first << "." << endl;
// begin can be used to start an iteration
// through a hash_map in a forward order
cout << "The hash_map is: ";
for ( hm1_Iter = hm1.begin( ) ; hm1_Iter != hm1.end( ); hm1_Iter++)
cout << hm1_Iter -> first << " ";
cout << "." << endl;
// rbegin can be used to start an iteration
// through a hash_map in a reverse order
cout << "The reversed hash_map is: ";
for ( hm1_rIter = hm1.rbegin( ) ; hm1_rIter != hm1.rend( ); hm1_rIter++)
cout << hm1_rIter -> first << " ";
cout << "." << endl;
// A hash_map element can be erased by dereferencing to its key
hm1_rIter = hm1.rbegin( );
hm1.erase ( hm1_rIter -> first );
hm1_rIter = hm1.rbegin( );
cout << "After the erasure, the first element "
<< "in the reversed hash_map is "
<< hm1_rIter -> first << "." << endl;
The first element of the reversed hash_map hm1 is 3.
The hash_map is: 1 2 3 .
The reversed hash_map is: 3 2 1 .
After the erasure, the first element in the reversed hash_map is 2.
hash_map::reference
這個 API 已經過時。 替代方式為unordered_map Class。
類型,提供儲存在 hash_map 中之項目的參考。
typedef list<typename _Traits::value_type, typename _Traits::allocator_type>::reference reference;
// hash_map_reference.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int> hm1;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 1, 10 ) );
hm1.insert ( Int_Pair ( 2, 20 ) );
// Declare and initialize a const_reference &Ref1
// to the key of the first element
const int &Ref1 = ( hm1.begin( ) -> first );
// The following line would cause an error as the
// non-const_reference cannot be used to access the key
// int &Ref1 = ( hm1.begin( ) -> first );
cout << "The key of first element in the hash_map is "
<< Ref1 << "." << endl;
// Declare and initialize a reference &Ref2
// to the data value of the first element
int &Ref2 = ( hm1.begin( ) -> second );
cout << "The data value of first element in the hash_map is "
<< Ref2 << "." << endl;
// The non-const_reference can be used to modify the
// data value of the first element
Ref2 = Ref2 + 5;
cout << "The modified data value of first element is "
<< Ref2 << "." << endl;
The key of first element in the hash_map is 1.
The data value of first element in the hash_map is 10.
The modified data value of first element is 15.
hash_map::rend
這個 API 已經過時。 替代專案是unordered_map Class。
傳回迭代器,為反轉 hash_map 中最後一個項目的下一個位置定址。
const_reverse_iterator rend() const;
reverse_iterator rend();
反向雙向反覆運算器,定址物件是反轉 hash_map 中最後一個專案之後的位置, (未反 hash_map 轉) 中第一個元素之前的位置。
rend與 反轉 hash_map 搭配使用,就像 搭配 hash_map 使用一樣 end 。
如果 的傳回值 rend 指派給 const_reverse_iterator , hash_map 則無法修改 物件。 如果 的傳回值 rend 指派給 reverse_iterator , hash_map 則可以修改 物件。
rend 可以用來測試反轉迭代器是否已到達其 hash_map 的結尾。
所 rend 傳回的值不應被取值。
// hash_map_rend.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int> hm1;
hash_map <int, int> :: iterator hm1_Iter;
hash_map <int, int> :: reverse_iterator hm1_rIter;
hash_map <int, int> :: const_reverse_iterator hm1_crIter;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 1, 10 ) );
hm1.insert ( Int_Pair ( 2, 20 ) );
hm1.insert ( Int_Pair ( 3, 30 ) );
hm1_rIter = hm1.rend( );
hm1_rIter--;
cout << "The last element of the reversed hash_map hm1 is "
<< hm1_rIter -> first << "." << endl;
// begin can be used to start an iteration
// through a hash_map in a forward order
cout << "The hash_map is: ";
for ( hm1_Iter = hm1.begin( ) ; hm1_Iter != hm1.end( );
hm1_Iter++)
cout << hm1_Iter -> first << " ";
cout << "." << endl;
// rbegin can be used to start an iteration
// through a hash_map in a reverse order
cout << "The reversed hash_map is: ";
for ( hm1_rIter = hm1.rbegin( ) ; hm1_rIter != hm1.rend( );
hm1_rIter++)
cout << hm1_rIter -> first << " ";
cout << "." << endl;
// A hash_map element can be erased by dereferencing to its key
hm1_rIter = --hm1.rend( );
hm1.erase ( hm1_rIter -> first );
hm1_rIter = hm1.rend( );
hm1_rIter--;
cout << "After the erasure, the last element "
<< "in the reversed hash_map is "
<< hm1_rIter -> first << "." << endl;
The last element of the reversed hash_map hm1 is 1.
The hash_map is: 1 2 3 .
The reversed hash_map is: 3 2 1 .
After the erasure, the last element in the reversed hash_map is 2.
hash_map::reverse_iterator
這個 API 已經過時。 替代專案是unordered_map Class。
類型,提供可以讀取或修改反轉 hash_map 中之項目的雙向迭代器。
typedef list<typename Traits::value_type, typename Traits::allocator_type>::reverse_iterator reverse_iterator;
類型 reverse_iterator 無法修改元素的值,並用來反向逐一查看 hash_map 。
所定義的 ,指向屬於value_type物件的專案,其類型 pair<const Key, Type> 為 ,其第一個成員是元素的索引鍵,而第二個成員是元素所持有的對應資料。 hash_mapreverse_iterator
若要取值 reverse_iteratorrIter 指向 中的 hash_map 專案,請使用 -> 運算子。
若要存取 專案的索引鍵值,請使用 rIter->first ,這相當於 (*rIter).first 。 若要存取專案的對應資料值,請使用 rIter->second ,這相當於 (*rIter).first 。
rbegin如需如何宣告和使用 reverse_iterator 的範例,請參閱範例。
hash_map::size
這個 API 已經過時。 替代專案是unordered_map Class。
傳回 hash_map 中項目的數目。
size_type size() const;
的目前長度 hash_map 。
下列範例示範如何使用 hash_map::size 成員函式。
// hash_map_size.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map<int, int> hm1, hm2;
hash_map<int, int>::size_type i;
typedef pair<int, int> Int_Pair;
hm1.insert(Int_Pair(1, 1));
i = hm1.size();
cout << "The hash_map length is " << i << "." << endl;
hm1.insert(Int_Pair(2, 4));
i = hm1.size();
cout << "The hash_map length is now " << i << "." << endl;
The hash_map length is 1.
The hash_map length is now 2.
hash_map::size_type
這個 API 已經過時。 替代專案是unordered_map Class。
不帶正負號的整數類型,可以表示 hash_map 中的項目數。
typedef list<typename _Traits::value_type, typename _Traits::allocator_type>::size_type size_type;
size如需如何宣告和使用範例,請參閱範例size_type
hash_map::swap
這個 API 已經過時。 替代專案是unordered_map Class。
交換兩個 hash_map 的項目。
void swap(hash_map& right);
right
提供要與目標 hash_map 交換之專案的引數 hash_map 。
成員函式不會使任何參考、指標或反覆運算器失效,這些反覆運算器會指定要交換其元素的兩 hash_map 個 s 中的元素。
// hash_map_swap.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int> hm1, hm2, hm3;
hash_map <int, int>::iterator hm1_Iter;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 1, 10 ) );
hm1.insert ( Int_Pair ( 2, 20 ) );
hm1.insert ( Int_Pair ( 3, 30 ) );
hm2.insert ( Int_Pair ( 10, 100 ) );
hm2.insert ( Int_Pair ( 20, 200 ) );
hm3.insert ( Int_Pair ( 30, 300 ) );
cout << "The original hash_map hm1 is:";
for ( hm1_Iter = hm1.begin( ); hm1_Iter != hm1.end( ); hm1_Iter++ )
cout << " " << hm1_Iter -> second;
cout << "." << endl;
// This is the member function version of swap
// hm2 is said to be the argument hash_map;
// hm1 is said to be the target hash_map
hm1.swap( hm2 );
cout << "After swapping with hm2, hash_map hm1 is:";
for ( hm1_Iter = hm1.begin( ); hm1_Iter != hm1.end( ); hm1_Iter++ )
cout << " " << hm1_Iter -> second;
cout << "." << endl;
// This is the specialized template version of swap
swap( hm1, hm3 );
cout << "After swapping with hm3, hash_map hm1 is:";
for ( hm1_Iter = hm1.begin( ); hm1_Iter != hm1.end( ); hm1_Iter++ )
cout << " " << hm1_Iter -> second;
cout << "." << endl;
The original hash_map hm1 is: 10 20 30.
After swapping with hm2, hash_map hm1 is: 100 200.
After swapping with hm3, hash_map hm1 is: 300.
hash_map::upper_bound
這個 API 已經過時。 替代專案是unordered_map Class。
將反覆運算器傳回至 中 hash_map 具有大於指定索引鍵之值的第一個專案。
iterator upper_bound(const Key& key);
const_iterator upper_bound(const Key& key) const;
要與所搜尋專案的 hash_map 排序索引鍵值比較的引數索引鍵值。
或 iteratorconst_iterator ,其會解決索引鍵大於引數索引鍵之 中 hash_map 專案的位置,如果找不到索引鍵的相符專案,則為 中 hash_map 最後一個專案成功的位置。
如果傳回值指派給 const_iterator , hash_map 則無法修改 物件。 如果傳回值指派給 iterator , hash_map 則可以修改 物件。
// hash_map_upper_bound.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int> hm1;
hash_map <int, int> :: const_iterator hm1_AcIter, hm1_RcIter;
typedef pair <int, int> Int_Pair;
hm1.insert ( Int_Pair ( 1, 10 ) );
hm1.insert ( Int_Pair ( 2, 20 ) );
hm1.insert ( Int_Pair ( 3, 30 ) );
hm1_RcIter = hm1.upper_bound( 2 );
cout << "The first element of hash_map hm1 with a key "
<< "greater than 2 is: "
<< hm1_RcIter -> second << "." << endl;
// If no match is found for the key, end is returned
hm1_RcIter = hm1. upper_bound ( 4 );
if ( hm1_RcIter == hm1.end( ) )
cout << "The hash_map hm1 doesn't have an element "
<< "with a key greater than 4." << endl;
cout << "The element of hash_map hm1 with a key > 4 is: "
<< hm1_RcIter -> second << "." << endl;
// The element at a specific location in the hash_map can be found
// using a dereferenced iterator addressing the location
hm1_AcIter = hm1.begin( );
hm1_RcIter = hm1. upper_bound ( hm1_AcIter -> first );
cout << "The 1st element of hm1 with a key greater than that\n"
<< "of the initial element of hm1 is: "
<< hm1_RcIter -> second << "." << endl;
The first element of hash_map hm1 with a key greater than 2 is: 30.
The hash_map hm1 doesn't have an element with a key greater than 4.
The 1st element of hm1 with a key greater than that
of the initial element of hm1 is: 20.
hash_map::value_comp
這個 API 已經過時。 替代專案是unordered_map Class。
傳回函式物件,藉由比較其索引鍵值來判斷 中的 hash_map 專案順序。
value_compare value_comp() const;
傳回 用來排序其專案的比較函式物件 hash_map 。
hash_mapm 針對 ,如果兩個元素 e1 (k1 、 d1) 和 e2 (k2 ,) 是 類型的 value_type 物件, d2 其中 k1 和 k2 是 類型的索引鍵 key_type , d1 而且 d2 是 其資料類型 mapped_type ,則 m.value_comp()(e1, e2)m.key_comp()(k1, k2) 相當於 。 預存物件會定義成員函式
bool operator(value_type& left, value_type& right);
如果 的 left 索引鍵值在 之前,且不等於 排序次序中的 索引鍵值 right ,則會傳回 true 。
// hash_map_value_comp.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
hash_map <int, int, hash_compare<int, less<int> > > hm1;
hash_map <int, int, hash_compare<int, less<int> > >
::value_compare vc1 = hm1.value_comp( );
pair< hash_map<int,int>::iterator, bool > pr1, pr2;
pr1= hm1.insert ( hash_map <int, int> :: value_type ( 1, 10 ) );
pr2= hm1.insert ( hash_map <int, int> :: value_type ( 2, 5 ) );
if( vc1( *pr1.first, *pr2.first ) == true )
cout << "The element ( 1,10 ) precedes the element ( 2,5 )."
<< endl;
cout << "The element ( 1,10 ) does not precede the element ( 2,5 )."
<< endl;
if( vc1 ( *pr2.first, *pr1.first ) == true )
cout << "The element ( 2,5 ) precedes the element ( 1,10 )."
<< endl;
cout << "The element ( 2,5 ) does not precede the element ( 1,10 )."
<< endl;
hash_map::value_type
這個 API 已經過時。 替代專案是unordered_map Class。
型別,表示儲存在 中的 hash_map 物件型別。
typedef pair<const Key, Type> value_type;
value_type 宣告為 , pair<const key_type, mapped_type> 而不是 pair<key_type, mapped_type> ,因為關聯容器的索引鍵可能無法使用非常數反覆運算器或參考來變更。
// hash_map_value_type.cpp
// compile with: /EHsc
#include <hash_map>
#include <iostream>
int main( )
using namespace std;
using namespace stdext;
typedef pair <const int, int> cInt2Int;
hash_map <int, int> hm1;
hash_map <int, int> :: key_type key1;
hash_map <int, int> :: mapped_type mapped1;
hash_map <int, int> :: value_type value1;
hash_map <int, int> :: iterator pIter;
// value_type can be used to pass the correct type
// explicitly to avoid implicit type conversion
hm1.insert ( hash_map <int, int> :: value_type ( 1, 10 ) );
// Compare other ways to insert objects into a hash_map
hm1.insert ( cInt2Int ( 2, 20 ) );
hm1[ 3 ] = 30;
// Initializing key1 and mapped1
key1 = ( hm1.begin( ) -> first );
mapped1 = ( hm1.begin( ) -> second );
cout << "The key of first element in the hash_map is "
<< key1 << "." << endl;
cout << "The data value of first element in the hash_map is "
<< mapped1 << "." << endl;
// The following line would cause an error because
// the value_type is not assignable
// value1 = cInt2Int ( 4, 40 );
cout << "The keys of the mapped elements are:";
for ( pIter = hm1.begin( ) ; pIter != hm1.end( ) ; pIter++ )
cout << " " << pIter -> first;
cout << "." << endl;
cout << "The values of the mapped elements are:";
for ( pIter = hm1.begin( ) ; pIter != hm1.end( ) ; pIter++ )
cout << " " << pIter -> second;
cout << "." << endl;
The key of first element in the hash_map is 1.
The data value of first element in the hash_map is 10.
The keys of the mapped elements are: 1 2 3.
The values of the mapped elements are: 10 20 30.
|
|
含蓄的啄木鸟 · 疑难解答 - Tableau 2 年前 |