hash_map (STL/CLR)

樣板類別描述一個物件,該物件控制具有雙向存取權之元素的不同長度序列。 您可以使用容器 hash_map 來管理元素序列做為雜湊表、儲存節點雙向連結清單的每個資料表專案,以及每個儲存一個元素的節點。 元素是由索引鍵所組成,用於排序序列,以及對應的值,其會隨著行程一起執行。

在下列描述中, GValue 與下列相同:

Microsoft::VisualC::StlClr::GenericPair<GKey, GMapped>

其中:

GKey 與 相同 Key ,除非後者是 ref 類型,在此情況下為 Key^

GMapped 與 相同 Mapped ,除非後者是 ref 類型,在此情況下為 Mapped^

語法

template<typename Key,
    typename Mapped>
    ref class hash_map
        :   public
        System::ICloneable,
        System::Collections::IEnumerable,
        System::Collections::ICollection,
        System::Collections::Generic::IEnumerable<GValue>,
        System::Collections::Generic::ICollection<GValue>,
        System::Collections::Generic::IList<GValue>,
        System::Collections::Generic::IDictionary<Gkey, GMapped>,
        Microsoft::VisualC::StlClr::IHash<Gkey, GValue>
    { ..... };

參數

Key
受控制序列中專案之索引鍵元件的型別。

Mapped
受控制序列中專案之其他元件的類型。

需求

標頭: < cliext/hash_map>

命名空間:cliext

宣告

類型定義 描述
hash_map::const_iterator 用於受控制序列的常數迭代器類型。
hash_map::const_reference 項目的常數參考類型。
hash_map::const_reverse_iterator 用於受控制序列的常數反向迭代器類型。
hash_map::difference_type 兩個元素之間的 (可能帶正負號) 距離的類型。
hash_map::generic_container 容器之泛型介面的類型。
hash_map::generic_iterator 容器之泛型介面的反覆運算器類型。
hash_map::generic_reverse_iterator 容器之泛型介面的反向反覆運算器類型。
hash_map::generic_value 容器之泛型介面的專案型別。
hash_map::hasher 索引鍵的雜湊委派。
hash_map::iterator 受控制序列之迭代器的類型。
hash_map::key_compare 兩個索引鍵的排序委派。
hash_map::key_type 排序索引鍵的類型。
hash_map::mapped_type 與每個索引鍵相關聯的對應值型別。
hash_map::reference 項目的參考類型。
hash_map::reverse_iterator 受控制序列的反向迭代器類型。
hash_map::size_type 兩個專案之間 (非負數) 距離的類型。
hash_map::value_compare 兩個元素值的排序委派。
hash_map::value_type 元素的類型。
成員函數 描述
hash_map::begin 指定受控制序列的開頭。
hash_map::bucket_count 計算貯體數目。
hash_map::clear 移除所有項目。
hash_map::count 計算符合指定索引鍵的專案。
hash_map::empty 測試項目是否不存在。
hash_map::end 指定受控制序列的結尾。
hash_map::equal_range 尋找符合指定之索引鍵的範圍。
hash_map::erase 移除位於指定位置的項目。
hash_map::find 尋找符合指定之索引鍵的元素。
hash_map::hash_delegate 複製金鑰的雜湊委派。
hash_map::hash_map 建構容器物件。
hash_map::insert 加入項目。
hash_map::key_comp 複製兩個索引鍵的排序委派。
hash_map::load_factor 計算每個值區的平均項目數。
hash_map::lower_bound 尋找符合指定索引鍵的範圍開頭。
hash_map::make_value 建構值物件。
hash_map::max_load_factor 取得或設定每個 Bucket 最大項目數。
hash_map::rbegin 指定反向受控制序列的開頭。
hash_map::rehash 重建雜湊資料表。
hash_map::rend 指定反向受控制序列的結尾。
hash_map::size 計算元素的數目。
hash_map::swap 交換兩個容器的內容。
hash_map::to_array 將受控制序列複製到新的陣列。
hash_map::upper_bound 尋找符合指定索引鍵的範圍結尾。
hash_map::value_comp 複製兩個元素值的排序委派。
運算子 描述
hash_map::operator= 取代受控制的序列。
hash_map::operator[] 地圖索引鍵與其相關聯的對應值。

介面

介面 描述
ICloneable 複製 物件。
IEnumerable 循序通過元素。
ICollection 維護專案群組。
IEnumerable<T> 透過具型別的元素進行排序。
ICollection<T> 維護具型別專案的群組。
IDictionary<TKey,TValue> 維護 {key, value} 組的群組。
IHash<Key, Value> 維護泛型容器。

備註

物件會為其控制項的序列配置和釋放儲存體,做為雙向連結清單中的個別節點。 為了加快存取速度,物件也會將不同長度的指標陣列保留在清單中(雜湊表),有效地將整個清單管理為子清單或貯體序列。 它會將元素插入貯體中,它會藉由改變節點之間的連結來保留排序,絕不是將某個節點的內容複寫到另一個節點。 這表示您可以自由插入和移除元素,而不會干擾其餘元素。

物件會呼叫 類型的 hash_set::key_compare 預存委派物件,以排序它所控制的每個貯體。 當您建構hash_set時,可以指定預存的委派物件;如果您未指定委派物件,則預設值為比較 operator<=(key_type, key_type)

您可以藉由呼叫成員函式 hash_set::key_comp() 來存取儲存的委派物件。 這類委派物件必須定義 類型 hash_set::key_type 索引鍵之間的對等順序。 這表示,針對任何兩個索引鍵 XY

key_comp()(X, Y) 會在每個呼叫上傳回相同的布林值結果。

如果 key_comp()(X, Y) && key_comp()(Y, X) 為 true,則 X 表示 Y 具有對等的排序。

任何行為類似 operator<=(key_type, key_type) 的排序規則, operator>=(key_type, key_type)operator==(key_type, key_type) 定義對等的排序。

容器只會確保索引鍵具有對等順序的專案(以及相同整數值的雜湊)在貯體內相鄰。 不同于樣板類別,樣板類別 hash_multimaphash_map 的物件可確保所有元素的索引鍵都是唯一的。 (沒有兩個索引鍵有對等的排序。

物件會藉由呼叫 類型的 hash_set::hasher 預存委派物件,判斷哪一個貯體應該包含指定的排序索引鍵。 您可以藉由呼叫成員函 hash_set::hash_delegate 式來存取這個儲存的物件,以取得相依于索引鍵值的整數值。 當您建構hash_set時,可以指定預存的委派物件;如果您未指定委派物件,則預設值為 函式 System::Object::hash_value(key_type) 。 這表示,針對任何索引鍵 XY

hash_delegate()(X) 在每次呼叫時,都會傳回相同的整數結果。

如果 XY 具有相同的順序,則 hash_delegate()(X) 應該傳回與 hash_delegate()(Y) 相同的整數結果。

每個元素都包含個別的索引鍵和對應的值。 序列是以允許在常數時間中查閱、插入和移除任意元素的方式表示。 也就是說,作業數目與序列中的元素數目無關,至少在最佳情況下。 此外,插入元素不會使反覆運算器失效,而移除元素只會使指向已移除元素的反覆運算器失效。

不過,如果雜湊值未統一散發,雜湊表可能會變質。 在極端中(針對一律傳回相同值的雜湊函式),查閱、插入和移除會與序列中的元素數目成正比(線性時間)。 容器會努力選擇合理的雜湊函式、平均值區大小和雜湊表大小(貯體總數),但您可以覆寫這些選擇的任何或全部。 例如,請參閱 函式 hash_set::max_load_factorhash_set::rehash

hash_map支援雙向反覆運算器,這表示您可以針對指定受控制序列中元素的反覆運算器,逐步執行相鄰元素。 特殊的前端節點會對應至 所 end() 傳回的反覆運算器。 如果存在,您可以遞減此反覆運算器,以到達受控制序列中的最後一個專案。 您可以遞增 hash_map 反覆運算器以到達前端節點,然後它會比較等於 end() 。 但是您無法取值 所 end() 傳回的反覆運算器。

您無法直接參考 hash_map 元素的數值位置;這需要隨機存取反覆運算器。

hash_map反覆運算器會將控制碼儲存至其相關聯的 hash_map 節點,進而將控制碼儲存至其相關聯的容器。 您只能搭配其相關聯的容器物件使用反覆運算器。 hash_map只要其相關聯的節點與某些 hash_map 相關聯 hash_map ,反覆運算器就會保持有效。 此外,有效的反覆運算器是可取值的。 您可以使用它來存取或改變它指定的專案值,只要它不等於 end()

清除或移除專案會呼叫其預存值的解構函式。 終結容器會清除所有元素。 因此,元素類型為 ref 類別的容器可確保容器沒有元素超過該容器。 不過請注意,控制碼的容器不會破壞其元素。

成員

hash_map::begin

指定受控制序列的開頭。

語法

iterator begin();

備註

成員函式會傳回雙向反覆運算器,指定受控制序列的第一個專案,或剛好超出空序列結尾。 您可以使用它來取得指定 current 受控制序列開頭的反覆運算器,但如果受控制序列的長度變更,其狀態可能會變更。

範例

// cliext_hash_map_begin.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // inspect first two items
    Myhash_map::iterator it = c1.begin();
    System::Console::WriteLine("*begin() = [{0} {1}]",
        it->first, it->second);
    ++it;
    System::Console::WriteLine("*++begin() = [{0} {1}]",
        it->first, it->second);
    return (0);
    }
[a 1] [b 2] [c 3]
*begin() = [a 1]
*++begin() = [b 2]

hash_map::bucket_count

計算貯體數目。

語法

int bucket_count();

備註

成員函式會傳回目前的 Bucket 數目。 您可以使用它來判斷雜湊表的大小。

範例

// cliext_hash_map_bucket_count.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1 = gcnew Myhash_map;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // inspect current parameters
    System::Console::WriteLine("bucket_count() = {0}", c1.bucket_count());
    System::Console::WriteLine("load_factor() = {0}", c1.load_factor());
    System::Console::WriteLine("max_load_factor() = {0}",
        c1.max_load_factor());
    System::Console::WriteLine();

    // change max_load_factor and redisplay
    c1.max_load_factor(0.25f);
    System::Console::WriteLine("bucket_count() = {0}", c1.bucket_count());
    System::Console::WriteLine("load_factor() = {0}", c1.load_factor());
    System::Console::WriteLine("max_load_factor() = {0}",
        c1.max_load_factor());
    System::Console::WriteLine();

    // rehash and redisplay
    c1.rehash(100);
    System::Console::WriteLine("bucket_count() = {0}", c1.bucket_count());
    System::Console::WriteLine("load_factor() = {0}", c1.load_factor());
    System::Console::WriteLine("max_load_factor() = {0}",
        c1.max_load_factor());
    return (0);
    }
[a 1] [b 2] [c 3]
bucket_count() = 16
load_factor() = 0.1875
max_load_factor() = 4

bucket_count() = 16
load_factor() = 0.1875
max_load_factor() = 0.25

bucket_count() = 128
load_factor() = 0.0234375
max_load_factor() = 0.25

hash_map::clear

移除所有項目。

語法

void clear();

備註

成員函式實際上會呼叫 erase(begin(), end()) 。 您可以使用它來確保受控制序列是空的。

範例

// cliext_hash_map_clear.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // clear the container and reinspect
    c1.clear();
    System::Console::WriteLine("size() = {0}", c1.size());

    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));

    // display contents " [a 1] [b 2]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    c1.clear();
    System::Console::WriteLine("size() = {0}", c1.size());
    return (0);
    }
[a 1] [b 2] [c 3]
size() = 0
[a 1] [b 2]
size() = 0

hash_map::const_iterator

用於受控制序列的常數迭代器類型。

語法

typedef T2 const_iterator;

備註

此類型描述未指定型 T2 別的物件,該物件可作為受控制序列的常數雙向反覆運算器。

範例

// cliext_hash_map_const_iterator.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    Myhash_map::const_iterator cit = c1.begin();
    for (; cit != c1.end(); ++cit)
        System::Console::Write("[{0} {1}] ", cit->first, cit->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]

hash_map::const_reference

項目的常數參考類型。

語法

typedef value_type% const_reference;

備註

此類型描述專案的常數參考。

範例

// cliext_hash_map_const_reference.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    Myhash_map::const_iterator cit = c1.begin();
    for (; cit != c1.end(); ++cit)
        {   // get a const reference to an element
        Myhash_map::const_reference cref = *cit;
        System::Console::Write("[{0} {1}] ", cref->first, cref->second);
        }
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]

hash_map::const_reverse_iterator

用於受控制序列的常數反向迭代器類型。

語法

typedef T4 const_reverse_iterator;

備註

此類型描述未指定型 T4 別的物件,該物件可作為受控制序列的常數反向反覆運算器。

範例

// cliext_hash_map_const_reverse_iterator.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]" reversed
    Myhash_map::const_reverse_iterator crit = c1.rbegin();
    for (; crit != c1.rend(); ++crit)
        System::Console::Write("[{0} {1}] ", crit->first, crit->second);
    System::Console::WriteLine();
    return (0);
    }
[c 3] [b 2] [a 1]

hash_map::count

尋找符合指定索引鍵的項目數目。

語法

size_type count(key_type key);

參數

key
要搜尋的索引鍵值。

備註

成員函式會傳回受控制序列中具有對等順序 key 的專案數目。 您可以使用它來判斷目前在受控制序列中符合指定索引鍵的專案數目。

範例

// cliext_hash_map_count.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    System::Console::WriteLine("count(L'A') = {0}", c1.count(L'A'));
    System::Console::WriteLine("count(L'b') = {0}", c1.count(L'b'));
    System::Console::WriteLine("count(L'C') = {0}", c1.count(L'C'));
    return (0);
    }
[a 1] [b 2] [c 3]
count(L'A') = 0
count(L'b') = 1
count(L'C') = 0

hash_map::difference_type

兩個專案之間帶正負號距離的類型。

語法

typedef int difference_type;

備註

型別描述可能的負元素計數。

範例

// cliext_hash_map_difference_type.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // compute positive difference
    Myhash_map::difference_type diff = 0;
    for (Myhash_map::iterator it = c1.begin(); it != c1.end(); ++it)
        ++diff;
    System::Console::WriteLine("end()-begin() = {0}", diff);

    // compute negative difference
    diff = 0;
    for (Myhash_map::iterator it = c1.end(); it != c1.begin(); --it)
        --diff;
    System::Console::WriteLine("begin()-end() = {0}", diff);
    return (0);
    }
[a 1] [b 2] [c 3]
end()-begin() = 3
begin()-end() = -3

hash_map::empty

測試項目是否不存在。

語法

bool empty();

備註

成員函式會 true 傳回空的受控制序列。 它相當於 size() == 0 。 您可以使用它來測試 是否 hash_map 為空白。

範例

// cliext_hash_map_empty.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    System::Console::WriteLine("size() = {0}", c1.size());
    System::Console::WriteLine("empty() = {0}", c1.empty());

    // clear the container and reinspect
    c1.clear();
    System::Console::WriteLine("size() = {0}", c1.size());
    System::Console::WriteLine("empty() = {0}", c1.empty());
    return (0);
    }
[a 1] [b 2] [c 3]
size() = 3
empty() = False
size() = 0
empty() = True

hash_map::end

指定受控制序列的結尾。

語法

iterator end();

備註

成員函式會傳回雙向反覆運算器,指向受控制序列結尾以外的位置。 您可以使用它來取得指定受控制序列結尾的反覆運算器;如果受控制序列的長度變更,其狀態不會變更。

範例

// cliext_hash_map_end.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // inspect last two items
    Myhash_map::iterator it = c1.end();
    --it;
    --it;
    System::Console::WriteLine("*-- --end() = [{0} {1}]",
        it->first, it->second);
    ++it;
    System::Console::WriteLine("*--end() = [{0} {1}]",
        it->first, it->second);
    return (0);
    }
[a 1] [b 2] [c 3]
*-- --end() = [b 2]
*--end() = [c 3]

hash_map::equal_range

尋找符合指定之索引鍵的範圍。

語法

cliext::pair<iterator, iterator> equal_range(key_type key);

參數

key
要搜尋的索引鍵值。

備註

成員函式會傳回一對反覆運算器 cliext::pair<iterator, iterator>(lower_bound(key), upper_bound(key)) 。 您可以使用它來判斷目前在受控制序列中符合指定索引鍵的專案範圍。

範例

// cliext_hash_map_equal_range.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
typedef Myhash_map::pair_iter_iter Pairii;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // display results of failed search
    Pairii pair1 = c1.equal_range(L'x');
    System::Console::WriteLine("equal_range(L'x') empty = {0}",
        pair1.first == pair1.second);

    // display results of successful search
    pair1 = c1.equal_range(L'b');
    for (; pair1.first != pair1.second; ++pair1.first)
        System::Console::Write("[{0} {1}] ",
            pair1.first->first, pair1.first->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]
equal_range(L'x') empty = True
[b 2]

hash_map::erase

移除位於指定位置的項目。

語法

iterator erase(iterator where);
iterator erase(iterator first, iterator last);
bool erase(key_type key)

參數

first
要清除的範圍開頭。

key
要清除的索引鍵值。

last
要清除的範圍結尾。

where
要清除的專案。

備註

第一個成員函式會移除 所 where 指向之受控制序列的 元素,並傳回反覆運算器,指定移除專案以外的第一個元素,如果沒有 end() 這類專案,則傳回反覆運算器。 您可以使用它來移除單一元素。

第二個成員函式會移除範圍 [ firstlast ] 中受控制序列的元素,並傳回反覆運算器,指定任何移除之元素以外的第一個元素,如果沒有 end() 這類專案,則傳回反覆運算器。 您可以使用它來移除零個或多個連續元素。

第三個成員函式會移除受控制序列的任何專案,其索引鍵的排序 key 與 相等,並傳回已移除之元素數目的計數。 您可以使用它來移除並計算符合指定索引鍵的所有元素。

每個元素清除需要時間與受控制序列中元素數目的對數成正比。

範例

// cliext_hash_map_erase.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    cliext::hash_map<wchar_t, int> c1;
    c1.insert(cliext::hash_map<wchar_t, int>::make_value(L'a', 1));
    c1.insert(cliext::hash_map<wchar_t, int>::make_value(L'b', 2));
    c1.insert(cliext::hash_map<wchar_t, int>::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (cliext::hash_map<wchar_t, int>::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // erase an element and reinspect
    cliext::hash_map<wchar_t, int>::iterator it =
        c1.erase(c1.begin());
    System::Console::WriteLine("erase(begin()) = [{0} {1}]",
        it->first, it->second);

    // add elements and display " b c d e"
    c1.insert(cliext::hash_map<wchar_t, int>::make_value(L'd', 4));
    c1.insert(cliext::hash_map<wchar_t, int>::make_value(L'e', 5));
    for each (cliext::hash_map<wchar_t, int>::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // erase all but end
    it = c1.end();
    it = c1.erase(c1.begin(), --it);
    System::Console::WriteLine("erase(begin(), end()-1) = [{0} {1}]",
        it->first, it->second);
    System::Console::WriteLine("size() = {0}", c1.size());

    // erase end
    System::Console::WriteLine("erase(L'x') = {0}", c1.erase(L'x'));
    System::Console::WriteLine("erase(L'e') = {0}", c1.erase(L'e'));
    return (0);
    }
[a 1] [b 2] [c 3]
erase(begin()) = [b 2]
[b 2] [c 3] [d 4] [e 5]
erase(begin(), end()-1) = [e 5]
size() = 1
erase(L'x') = 0
erase(L'e') = 1

hash_map::find

尋找符合指定之索引鍵的元素。

語法

iterator find(key_type key);

參數

key
要搜尋的索引鍵值。

備註

如果受控制序列中至少有一個專案具有對等的順序 key ,則成員函式會傳回反覆運算器,指定其中一個專案,否則會傳 end() 回 。 您可以使用它來尋找目前在受控制序列中符合指定索引鍵的專案。

範例

// cliext_hash_map_find.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    System::Console::WriteLine("find {0} = {1}",
        L'A', c1.find(L'A') != c1.end());

    Myhash_map::iterator it = c1.find(L'b');
    System::Console::WriteLine("find {0} = [{1} {2}]",
        L'b', it->first, it->second);

    System::Console::WriteLine("find {0} = {1}",
        L'C', c1.find(L'C') != c1.end());
    return (0);
    }
[a 1] [b 2] [c 3]
find A = False
find b = [b 2]
find C = False

hash_map::generic_container

容器之泛型介面的類型。

語法

typedef Microsoft::VisualC::StlClr::
    IHash<GKey, GValue>
    generic_container;

備註

此類型描述此範本容器類別的泛型介面。

範例

// cliext_hash_map_generic_container.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct a generic container
    Myhash_map::generic_container^ gc1 = %c1;
    for each (Myhash_map::value_type elem in gc1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // modify generic and display original
    gc1->insert(Myhash_map::make_value(L'd', 4));
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // modify original and display generic
    c1.insert(Myhash_map::make_value(L'e', 5));
    for each (Myhash_map::value_type elem in gc1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]
[a 1] [b 2] [c 3]
[a 1] [b 2] [c 3] [d 4]
[a 1] [b 2] [c 3] [d 4] [e 5]

hash_map::generic_iterator

用於容器之泛型介面的反覆運算器類型。

語法

typedef Microsoft::VisualC::StlClr::Generic::
    ContainerBidirectionalIterator<generic_value>
    generic_iterator;

備註

此類型描述可搭配此範本容器類別之泛型介面使用的泛型反覆運算器。

範例

// cliext_hash_map_generic_iterator.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct a generic container
    Myhash_map::generic_container^ gc1 = %c1;
    for each (Myhash_map::value_type elem in gc1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // get an element and display it
    Myhash_map::generic_iterator gcit = gc1->begin();
    Myhash_map::generic_value gcval = *gcit;
    System::Console::Write("[{0} {1}] ", gcval->first, gcval->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]
[a 1] [b 2] [c 3]
[a 1]

hash_map::generic_reverse_iterator

用於容器之泛型介面的反向反覆運算器類型。

語法

typedef Microsoft::VisualC::StlClr::Generic::
    ReverseRandomAccessIterator<generic_value>
    generic_reverse_iterator;

備註

此類型描述可搭配此範本容器類別之泛型介面使用的泛型反向反覆運算器。

範例

// cliext_hash_map_generic_reverse_iterator.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct a generic container
    Myhash_map::generic_container^ gc1 = %c1;
    for each (Myhash_map::value_type elem in gc1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // get an element and display it
    Myhash_map::generic_reverse_iterator gcit = gc1->rbegin();
    Myhash_map::generic_value gcval = *gcit;
    System::Console::WriteLine("[{0} {1}] ", gcval->first, gcval->second);
    return (0);
    }
[a 1] [b 2] [c 3]
[a 1] [b 2] [c 3]
[c 3]

hash_map::generic_value

要與容器之泛型介面搭配使用的元素型別。

語法

typedef GValue generic_value;

備註

此類型描述 型 GValue 別的物件,該物件描述要與這個範本容器類別之泛型介面搭配使用的預存專案值。

範例

// cliext_hash_map_generic_value.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct a generic container
    Myhash_map::generic_container^ gc1 = %c1;
    for each (Myhash_map::value_type elem in gc1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // get an element and display it
    Myhash_map::generic_iterator gcit = gc1->begin();
    Myhash_map::generic_value gcval = *gcit;
    System::Console::WriteLine("[{0} {1}] ", gcval->first, gcval->second);
    return (0);
    }
[a 1] [b 2] [c 3]
[a 1] [b 2] [c 3]
[a 1]

hash_map::hash_delegate

尋找符合指定之索引鍵的元素。

語法

hasher^ hash_delegate();

備註

成員函式會傳回用來將索引鍵值轉換成整數的委派。 您可以使用它來雜湊金鑰。

範例

// cliext_hash_map_hash_delegate.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    Myhash_map::hasher^ myhash = c1.hash_delegate();

    System::Console::WriteLine("hash(L'a') = {0}", myhash(L'a'));
    System::Console::WriteLine("hash(L'b') = {0}", myhash(L'b'));
    return (0);
    }
hash(L'a') = 1616896120
hash(L'b') = 570892832

hash_map::hash_map

建構容器物件。

語法

hash_map();
explicit hash_map(key_compare^ pred);
hash_map(key_compare^ pred, hasher^ hashfn);
hash_map(hash_map<Key, Mapped>% right);
hash_map(hash_map<Key, Mapped>^ right);
template<typename InIter>
    hash_maphash_map(InIter first, InIter last);
template<typename InIter>
    hash_map(InIter first, InIter last,
        key_compare^ pred);
template<typename InIter>
    hash_map(InIter first, InIter last,
        key_compare^ pred, hasher^ hashfn);
hash_map(System::Collections::Generic::IEnumerable<GValue>^ right);
hash_map(System::Collections::Generic::IEnumerable<GValue>^ right,
    key_compare^ pred);
hash_map(System::Collections::Generic::IEnumerable<GValue>^ right,
    key_compare^ pred, hasher^ hashfn);

參數

first
要插入的範圍開頭。

hashfn
將索引鍵對應至貯體的雜湊函式。

last
要插入的範圍結尾。

pred
排序受控制序列的述詞。

right
要插入的物件或範圍。

備註

建構函式:

hash_map();

使用預設順序述詞 key_compare() ,並使用預設雜湊函式,初始化沒有元素的受控制序列。 您可以使用它來指定空的初始受控制序列,並使用預設順序述詞和雜湊函式。

建構函式:

explicit hash_map(key_compare^ pred);

使用排序述 pred 詞 ,並使用預設雜湊函式,初始化沒有元素的受控制序列。 您可以使用它來指定空的初始控制序列,並指定順序述詞和預設雜湊函式。

建構函式:

hash_map(key_compare^ pred, hasher^ hashfn);

使用排序述詞 pred ,以及雜湊函 hashfn 式 ,初始化沒有元素的受控制序列。 您可以使用它來指定空的初始控制序列,並搭配指定的排序述詞和雜湊函式。

建構函式:

hash_map(hash_map<Key, Mapped>% right);

使用序列 [ right.begin()right.end() 使用預設順序述詞以及預設雜湊函式,初始化受控制序列。 您可以使用它來指定一個初始受控制序列,這是由 hash_map 物件 right 所控制之序列的複本,以及預設排序述詞和雜湊函式。

建構函式:

hash_map(hash_map<Key, Mapped>^ right);

使用序列 [ right->begin()right->end() 使用預設順序述詞以及預設雜湊函式,初始化受控制序列。 您可以使用它來指定一個初始受控制序列,這是由 hash_map 物件 right 所控制之序列的複本,以及預設排序述詞和雜湊函式。

建構函式:

template<typename InIter> hash_map(InIter first, InIter last);

使用序列 [ firstlast 使用預設順序述詞以及預設雜湊函式,初始化受控制序列。 您可以使用它讓受控制序列成為另一個序列的複本,並使用預設順序述詞和雜湊函式。

建構函式:

template<typename InIter> hash_map(InIter first, InIter last, key_compare^ pred);

使用順序述 pred 詞 ,並使用預設雜湊函式,使用序列 [ firstlast 初始化受控制序列。 您可以使用它讓受控制序列成為另一個序列的複本,並搭配指定的排序述詞和預設雜湊函式。

建構函式:

template<typename InIter> hash_map(InIter first, InIter last, key_compare^ pred, hasher^ hashfn);

使用序列 [ firstlast 使用排序述詞 pred ,以及雜湊函 hashfn 式 ,初始化受控制序列。 您可以使用它讓受控制序列成為另一個序列的複本,並搭配指定的排序述詞和雜湊函式。

建構函式:

hash_map(System::Collections::Generic::IEnumerable<Key>^ right);

使用列舉值 right 所指定的序列,使用預設順序述詞和預設雜湊函式,初始化受控制序列。 您可以使用它,讓受控制序列成為列舉值所描述之另一個序列的複本,並使用預設順序述詞和雜湊函式。

建構函式:

hash_map(System::Collections::Generic::IEnumerable<Key>^ right, key_compare^ pred);

使用列舉值 right 所指定的序列,使用排序述 pred 詞,以及使用預設雜湊函式,初始化受控制序列。 您可以使用它,讓受控制序列成為列舉值所描述之另一個序列的複本,以及指定的排序述詞和預設雜湊函式。

建構函式:

hash_map(System::Collections::Generic::IEnumerable<Key>^ right, key_compare^ pred, hasher^ hashfn);

使用列舉值 right 所指定的序列,使用排序述 pred 詞,以及雜湊函 hashfn 式 ,初始化受控制序列。 您可以使用它,讓受控制序列成為列舉值所描述之另一個序列的複本,以及指定的排序述詞和雜湊函式。

範例

// cliext_hash_map_construct.cpp
// compile with: /clr
#include <cliext/hash_map>

int myfun(wchar_t key)
    { // hash a key
    return (key ^ 0xdeadbeef);
    }

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
// construct an empty container
    Myhash_map c1;
    System::Console::WriteLine("size() = {0}", c1.size());

    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct with an ordering rule
    Myhash_map c2 = cliext::greater_equal<wchar_t>();
    System::Console::WriteLine("size() = {0}", c2.size());

    c2.insert(c1.begin(), c1.end());
    for each (Myhash_map::value_type elem in c2)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct with an ordering rule and hash function
    Myhash_map c2h(cliext::greater_equal<wchar_t>(),
        gcnew Myhash_map::hasher(&myfun));
    System::Console::WriteLine("size() = {0}", c2h.size());

    c2h.insert(c1.begin(), c1.end());
    for each (Myhash_map::value_type elem in c2h)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    System::Console::WriteLine();

    // construct with an iterator range
    Myhash_map c3(c1.begin(), c1.end());
    for each (Myhash_map::value_type elem in c3)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct with an iterator range and an ordering rule
    Myhash_map c4(c1.begin(), c1.end(),
        cliext::greater_equal<wchar_t>());
    for each (Myhash_map::value_type elem in c4)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct with an iterator range and an ordering rule and hash function
    Myhash_map c4h(c1.begin(), c1.end(),
        cliext::greater_equal<wchar_t>(),
        gcnew Myhash_map::hasher(&myfun));
    for each (Myhash_map::value_type elem in c4h)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    System::Console::WriteLine();

    // construct with an enumeration
    Myhash_map c5(   // NOTE: cast is not needed
        (System::Collections::Generic::IEnumerable<
            Myhash_map::value_type>^)%c3);
    for each (Myhash_map::value_type elem in c5)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct with an enumeration and an ordering rule
    Myhash_map c6(   // NOTE: cast is not needed
        (System::Collections::Generic::IEnumerable<
            Myhash_map::value_type>^)%c3,
                cliext::greater_equal<wchar_t>());
    for each (Myhash_map::value_type elem in c6)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct with an enumeration and an ordering rule and hash function
    Myhash_map c6h(   // NOTE: cast is not needed
        (System::Collections::Generic::IEnumerable<
            Myhash_map::value_type>^)%c3,
                cliext::greater_equal<wchar_t>(),
                gcnew Myhash_map::hasher(&myfun));
    for each (Myhash_map::value_type elem in c6h)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    System::Console::WriteLine();

    // construct by copying another container
    Myhash_map c7(c4);
    for each (Myhash_map::value_type elem in c7)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct by copying a container handle
    Myhash_map c8(%c3);
    for each (Myhash_map::value_type elem in c8)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    return (0);
    }
size() = 0
[a 1] [b 2] [c 3]
size() = 0
[a 1] [b 2] [c 3]
size() = 0
[c 3] [b 2] [a 1]

[a 1] [b 2] [c 3]
[a 1] [b 2] [c 3]
[c 3] [b 2] [a 1]

[a 1] [b 2] [c 3]
[a 1] [b 2] [c 3]
[c 3] [b 2] [a 1]

[a 1] [b 2] [c 3]
[a 1] [b 2] [c 3]

hash_map::hasher

索引鍵的雜湊委派。

語法

Microsoft::VisualC::StlClr::UnaryDelegate<GKey, int>
    hasher;

備註

此類型描述將索引鍵值轉換成整數的委派。

範例

// cliext_hash_map_hasher.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    Myhash_map::hasher^ myhash = c1.hash_delegate();

    System::Console::WriteLine("hash(L'a') = {0}", myhash(L'a'));
    System::Console::WriteLine("hash(L'b') = {0}", myhash(L'b'));
    return (0);
    }
hash(L'a') = 1616896120
hash(L'b') = 570892832

hash_map::insert

加入項目。

語法

cliext::pair<iterator, bool> insert(value_type val);
iterator insert(iterator where, value_type val);
template<typename InIter>
    void insert(InIter first, InIter last);
void insert(System::Collections::Generic::IEnumerable<value_type>^ right);

參數

first
要插入的範圍開頭。

last
要插入的範圍結尾。

right
要插入的列舉。

val
要插入的索引鍵值。

where
容器中要插入的位置(僅限提示)。

備註

每個成員函式都會插入其餘運算元所指定的序列。

第一個成員函式會嘗試插入具有 值 val 的專案,並傳回一組值 X 。 如果 X.second 為 true, X.first 則指定新插入的專案;否則 X.first 會指定具有相等順序的專案,而且不會插入任何新專案。 您可以使用它來插入單一元素。

第二個成員函式會使用 where 作為提示來插入具有 值 val 的專案,並傳回指定新插入元素的反覆運算器。 您可以使用它來插入可能位於您知道之專案旁邊的單一元素。

第三個成員函式會插入序列 [ firstlast 。 您可以使用它插入從另一個序列複製的零個或多個專案。

第四個成員函式會插入 所 right 指定的序列。 您可以使用它來插入列舉值所描述的序列。

每個元素插入需要時間與受控制序列中元素數目的對數成正比。 不過,在指定插入點旁邊的專案提示時,插入可能會以分攤的常數時間發生。

範例

// cliext_hash_map_insert.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
typedef Myhash_map::pair_iter_bool Pairib;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // insert a single value, unique and duplicate
    Pairib pair1 =
        c1.insert(Myhash_map::make_value(L'x', 24));
    System::Console::WriteLine("insert([L'x' 24]) = [{0} {1}] {2}",
        pair1.first->first, pair1.first->second, pair1.second);

    pair1 = c1.insert(Myhash_map::make_value(L'b', 2));
    System::Console::WriteLine("insert([L'b' 2]) = [{0} {1}] {2}",
        pair1.first->first, pair1.first->second, pair1.second);

    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // insert a single value with hint
    Myhash_map::iterator it =
        c1.insert(c1.begin(), Myhash_map::make_value(L'y', 25));
    System::Console::WriteLine("insert(begin(), [L'y' 25]) = [{0} {1}]",
        it->first, it->second);
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // insert an iterator range
    Myhash_map c2;
    it = c1.end();
    c2.insert(c1.begin(), --it);
    for each (Myhash_map::value_type elem in c2)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // insert an enumeration
    Myhash_map c3;
    c3.insert(   // NOTE: cast is not needed
        (System::Collections::Generic::
            IEnumerable<Myhash_map::value_type>^)%c1);
    for each (Myhash_map::value_type elem in c3)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]
insert([L'x' 24]) = [x 24] True
insert([L'b' 2]) = [b 2] False
[a 1] [b 2] [c 3] [x 24]
insert(begin(), [L'y' 25]) = [y 25]
[a 1] [b 2] [c 3] [x 24] [y 25]
[a 1] [b 2] [c 3] [x 24]
[a 1] [b 2] [c 3] [x 24] [y 25]

hash_map::iterator

受控制序列之迭代器的類型。

語法

typedef T1 iterator;

備註

此類型描述非指定型 T1 別的物件,該物件可作為受控制序列的雙向反覆運算器。

範例

// cliext_hash_map_iterator.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    Myhash_map::iterator it = c1.begin();
    for (; it != c1.end(); ++it)
        System::Console::Write("[{0} {1}] ", it->first, it->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]

hash_map::key_comp

複製兩個索引鍵的排序委派。

語法

key_compare^key_comp();

備註

成員函式會傳回用來排序受控制序列的排序委派。 您可以使用它來比較兩個索引鍵。

範例

// cliext_hash_map_key_comp.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    Myhash_map::key_compare^ kcomp = c1.key_comp();

    System::Console::WriteLine("compare(L'a', L'a') = {0}",
        kcomp(L'a', L'a'));
    System::Console::WriteLine("compare(L'a', L'b') = {0}",
        kcomp(L'a', L'b'));
    System::Console::WriteLine("compare(L'b', L'a') = {0}",
        kcomp(L'b', L'a'));
    System::Console::WriteLine();

    // test a different ordering rule
    Myhash_map c2 = cliext::greater<wchar_t>();
    kcomp = c2.key_comp();

    System::Console::WriteLine("compare(L'a', L'a') = {0}",
        kcomp(L'a', L'a'));
    System::Console::WriteLine("compare(L'a', L'b') = {0}",
        kcomp(L'a', L'b'));
    System::Console::WriteLine("compare(L'b', L'a') = {0}",
        kcomp(L'b', L'a'));
    return (0);
    }
compare(L'a', L'a') = True
compare(L'a', L'b') = True
compare(L'b', L'a') = False

compare(L'a', L'a') = False
compare(L'a', L'b') = False
compare(L'b', L'a') = True

hash_map::key_compare

兩個索引鍵的排序委派。

語法

Microsoft::VisualC::StlClr::BinaryDelegate<GKey, GKey, bool>
    key_compare;

備註

此類型與決定其索引鍵引數順序的委派同義。

範例

// cliext_hash_map_key_compare.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    Myhash_map::key_compare^ kcomp = c1.key_comp();

    System::Console::WriteLine("compare(L'a', L'a') = {0}",
        kcomp(L'a', L'a'));
    System::Console::WriteLine("compare(L'a', L'b') = {0}",
        kcomp(L'a', L'b'));
    System::Console::WriteLine("compare(L'b', L'a') = {0}",
        kcomp(L'b', L'a'));
    System::Console::WriteLine();

    // test a different ordering rule
    Myhash_map c2 = cliext::greater<wchar_t>();
    kcomp = c2.key_comp();

    System::Console::WriteLine("compare(L'a', L'a') = {0}",
        kcomp(L'a', L'a'));
    System::Console::WriteLine("compare(L'a', L'b') = {0}",
        kcomp(L'a', L'b'));
    System::Console::WriteLine("compare(L'b', L'a') = {0}",
        kcomp(L'b', L'a'));
    return (0);
    }
compare(L'a', L'a') = True
compare(L'a', L'b') = True
compare(L'b', L'a') = False

compare(L'a', L'a') = False
compare(L'a', L'b') = False
compare(L'b', L'a') = True

hash_map::key_type

排序索引鍵的類型。

語法

typedef Key key_type;

備註

此類型是範本參數 Key的同義字。

範例

// cliext_hash_map_key_type.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]" using key_type
    for (Myhash_map::iterator it = c1.begin(); it != c1.end(); ++it)
        {   // store element in key_type object
        Myhash_map::key_type val = it->first;

        System::Console::Write("{0} ", val);
        }
    System::Console::WriteLine();
    return (0);
    }
a b c

hash_map::load_factor

計算每個值區的平均項目數。

語法

float load_factor();

備註

此成員函式會傳回 (float)size() / bucket_count()。 您可以使用它來判斷平均貯體大小。

範例

// cliext_hash_map_load_factor.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1 = gcnew Myhash_map;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // inspect current parameters
    System::Console::WriteLine("bucket_count() = {0}", c1.bucket_count());
    System::Console::WriteLine("load_factor() = {0}", c1.load_factor());
    System::Console::WriteLine("max_load_factor() = {0}",
        c1.max_load_factor());
    System::Console::WriteLine();

    // change max_load_factor and redisplay
    c1.max_load_factor(0.25f);
    System::Console::WriteLine("bucket_count() = {0}", c1.bucket_count());
    System::Console::WriteLine("load_factor() = {0}", c1.load_factor());
    System::Console::WriteLine("max_load_factor() = {0}",
        c1.max_load_factor());
    System::Console::WriteLine();

    // rehash and redisplay
    c1.rehash(100);
    System::Console::WriteLine("bucket_count() = {0}", c1.bucket_count());
    System::Console::WriteLine("load_factor() = {0}", c1.load_factor());
    System::Console::WriteLine("max_load_factor() = {0}",
        c1.max_load_factor());
    return (0);
    }
[a 1] [b 2] [c 3]
bucket_count() = 16
load_factor() = 0.1875
max_load_factor() = 4

bucket_count() = 16
load_factor() = 0.1875
max_load_factor() = 0.25

bucket_count() = 128
load_factor() = 0.0234375
max_load_factor() = 0.25

hash_map::lower_bound

尋找符合指定索引鍵的範圍開頭。

語法

iterator lower_bound(key_type key);

參數

key
要搜尋的索引鍵值。

備註

成員函式會決定受控制序列中的第一個元素,該元素 X 會雜湊至與 相同的貯 key 體,並且具有對等的順序 key 。 如果不存在這類專案,則會傳 end() 回 ,否則會傳回指定 X 的反覆運算器。 您可以使用它來尋找目前在受控制序列中符合指定索引鍵之元素序列的開頭。

範例

// cliext_hash_map_lower_bound.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    System::Console::WriteLine("lower_bound(L'x')==end() = {0}",
        c1.lower_bound(L'x') == c1.end());

    Myhash_map::iterator it = c1.lower_bound(L'a');
    System::Console::WriteLine("*lower_bound(L'a') = [{0} {1}]",
        it->first, it->second);
    it = c1.lower_bound(L'b');
    System::Console::WriteLine("*lower_bound(L'b') = [{0} {1}]",
        it->first, it->second);
    return (0);
    }
[a 1] [b 2] [c 3]
lower_bound(L'x')==end() = True
*lower_bound(L'a') = [a 1]
*lower_bound(L'b') = [b 2]

hash_map::make_value

建構值物件。

語法

static value_type make_value(key_type key, mapped_type mapped);

參數

key
要使用的索引鍵值。

mapped
要搜尋的對應值。

備註

成員函式會傳 value_type 回索引鍵為 key 且對應的值為 mapped 的物件。 您可以使用它來撰寫適合與數個其他成員函式搭配使用的物件。

範例

// cliext_hash_map_make_value.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]

hash_map::mapped_type

與每個索引鍵關聯的對應值類型。

語法

typedef Mapped mapped_type;

備註

此類型是範本參數 Mapped的同義字。

範例

// cliext_hash_map_mapped_type.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]" using mapped_type
    for (Myhash_map::iterator it = c1.begin(); it != c1.end(); ++it)
        {   // store element in mapped_type object
        Myhash_map::mapped_type val = it->second;

        System::Console::Write("{0} ", val);
        }
    System::Console::WriteLine();
    return (0);
    }
1 2 3

hash_map::max_load_factor

取得或設定每個 Bucket 最大項目數。

語法

float max_load_factor();
void max_load_factor(float new_factor);

參數

new_factor
要儲存的新最大負載因數。

備註

第一個成員函式會傳回目前儲存的最大負載因數。 您可以使用它來判斷平均貯體大小上限。

第二個成員函式會將存放區最大負載因數取代為 new_factor 。 在後續插入之前,不會發生自動重新套用。

範例

// cliext_hash_map_max_load_factor.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1 = gcnew Myhash_map;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // inspect current parameters
    System::Console::WriteLine("bucket_count() = {0}", c1.bucket_count());
    System::Console::WriteLine("load_factor() = {0}", c1.load_factor());
    System::Console::WriteLine("max_load_factor() = {0}",
        c1.max_load_factor());
    System::Console::WriteLine();

    // change max_load_factor and redisplay
    c1.max_load_factor(0.25f);
    System::Console::WriteLine("bucket_count() = {0}", c1.bucket_count());
    System::Console::WriteLine("load_factor() = {0}", c1.load_factor());
    System::Console::WriteLine("max_load_factor() = {0}",
        c1.max_load_factor());
    System::Console::WriteLine();

    // rehash and redisplay
    c1.rehash(100);
    System::Console::WriteLine("bucket_count() = {0}", c1.bucket_count());
    System::Console::WriteLine("load_factor() = {0}", c1.load_factor());
    System::Console::WriteLine("max_load_factor() = {0}",
        c1.max_load_factor());
    return (0);
    }
[a 1] [b 2] [c 3]
bucket_count() = 16
load_factor() = 0.1875
max_load_factor() = 4

bucket_count() = 16
load_factor() = 0.1875
max_load_factor() = 0.25

bucket_count() = 128
load_factor() = 0.0234375
max_load_factor() = 0.25

hash_map::operator=

取代受控制的序列。

語法

hash_map<Key, Mapped>% operator=(hash_map<Key, Mapped>% right);

參數

right
要複製的容器。

備註

成員運算子會 right 複製到 物件,然後傳 *this 回 。 您使用它將受控制序列取代為 right 中受控制序列的複本。

範例

// cliext_hash_map_operator_as.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // assign to a new container
    Myhash_map c2;
    c2 = c1;
// display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c2)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]
[a 1] [b 2] [c 3]

hash_map::operator[]

地圖索引鍵與其相關聯的對應值。

語法

mapped_type operator[](key_type key);

參數

key
要搜尋的索引鍵值。

備註

成員函式會努力尋找具有對等順序的專案 key 。 如果找到一個,則會傳回相關聯的對應值;否則,它會插入 value_type(key, mapped_type()) 並傳回相關聯的 (預設值) 對應值。 您可以使用它來查閱對應的值,並指定其相關聯的索引鍵,或確保找不到索引鍵時有專案存在。

範例

// cliext_hash_map_operator_sub.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    System::Console::WriteLine("c1[{0}] = {1}",
        L'A', c1[L'A']);
    System::Console::WriteLine("c1[{0}] = {1}",
        L'b', c1[L'b']);

    // redisplay altered contents
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // alter mapped values and redisplay
    c1[L'A'] = 10;
    c1[L'c'] = 13;
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]
c1[A] = 0
c1[b] = 2
[a 1] [A 0] [b 2] [c 3]
[a 1] [A 10] [b 2] [c 13]

hash_map::rbegin

指定反向受控制序列的開頭。

語法

reverse_iterator rbegin();

備註

成員函式會傳回反向反覆運算器,指定受控制序列的最後一個專案,或剛好超出空序列的開頭。 因此,它會指定 beginning 反向序列的 。 您可以使用它來取得反覆運算器,指定 current 以反向順序顯示之受控制序列的開頭,但如果受控制序列的長度變更,其狀態可能會變更。

範例

// cliext_hash_map_rbegin.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // inspect first two items in reversed sequence
    Myhash_map::reverse_iterator rit = c1.rbegin();
    System::Console::WriteLine("*rbegin() = [{0} {1}]",
        rit->first, rit->second);
    ++rit;
    System::Console::WriteLine("*++rbegin() = [{0} {1}]",
        rit->first, rit->second);
    return (0);
    }
[a 1] [b 2] [c 3]
*rbegin() = [c 3]
*++rbegin() = [b 2]

hash_map::reference

項目的參考類型。

語法

typedef value_type% reference;

備註

此類型描述專案的參考。

範例

// cliext_hash_map_reference.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    Myhash_map::iterator it = c1.begin();
    for (; it != c1.end(); ++it)
        {   // get a reference to an element
        Myhash_map::reference ref = *it;
        System::Console::Write("[{0} {1}] ", ref->first, ref->second);
        }
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]

hash_map::rehash

重建雜湊資料表。

語法

void rehash();

備註

成員函式會重建雜湊表,確保 load_factor() <= max_load_factor() 。 否則,只有在插入之後,雜湊表才會視需要增加大小。 (它永遠不會自動減少大小。您可以使用它來調整雜湊表的大小。

範例

// cliext_hash_map_rehash.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1 = gcnew Myhash_map;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // inspect current parameters
    System::Console::WriteLine("bucket_count() = {0}", c1.bucket_count());
    System::Console::WriteLine("load_factor() = {0}", c1.load_factor());
    System::Console::WriteLine("max_load_factor() = {0}",
        c1.max_load_factor());
    System::Console::WriteLine();

    // change max_load_factor and redisplay
    c1.max_load_factor(0.25f);
    System::Console::WriteLine("bucket_count() = {0}", c1.bucket_count());
    System::Console::WriteLine("load_factor() = {0}", c1.load_factor());
    System::Console::WriteLine("max_load_factor() = {0}",
        c1.max_load_factor());
    System::Console::WriteLine();

    // rehash and redisplay
    c1.rehash(100);
    System::Console::WriteLine("bucket_count() = {0}", c1.bucket_count());
    System::Console::WriteLine("load_factor() = {0}", c1.load_factor());
    System::Console::WriteLine("max_load_factor() = {0}",
        c1.max_load_factor());
    return (0);
    }
[a 1] [b 2] [c 3]
bucket_count() = 16
load_factor() = 0.1875
max_load_factor() = 4

bucket_count() = 16
load_factor() = 0.1875
max_load_factor() = 0.25

bucket_count() = 128
load_factor() = 0.0234375
max_load_factor() = 0.25

hash_map::rend

指定反向受控制序列的結尾。

語法

reverse_iterator rend();

備註

成員函式會傳回反向反覆運算器,指向受控制序列的開頭以外。 因此,它會指定 end 反向序列的 。 您可以使用它來取得反覆運算器,指定 current 以反向順序顯示的受控制序列結尾,但如果受控制序列的長度變更,其狀態可能會變更。

範例

// cliext_hash_map_rend.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // inspect first two items in reversed sequence
    Myhash_map::reverse_iterator rit = c1.rend();
    --rit;
    --rit;
    System::Console::WriteLine("*-- --rend() = [{0} {1}]",
        rit->first, rit->second);
    ++rit;
    System::Console::WriteLine("*--rend() = [{0} {1}]",
        rit->first, rit->second);
    return (0);
    }
[a 1] [b 2] [c 3]
*-- --rend() = [b 2]
*--rend() = [a 1]

hash_map::reverse_iterator

受控制序列的反向迭代器類型。

語法

typedef T3 reverse_iterator;

備註

此類型描述未指定類型 T3 的物件,其可用作受控制序列的反向迭代器。

範例

// cliext_hash_map_reverse_iterator.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]" reversed
    Myhash_map::reverse_iterator rit = c1.rbegin();
    for (; rit != c1.rend(); ++rit)
        System::Console::Write("[{0} {1}] ", rit->first, rit->second);
    System::Console::WriteLine();
    return (0);
    }
[c 3] [b 2] [a 1]

hash_map::size

計算元素的數目。

語法

size_type size();

備註

成員函式會傳回受控制序列的長度。 您可以使用它來判斷目前在受控制序列中的元素數目。 如果您關心的只是序列是否具有非零大小,請參閱 hash_map::empty

範例

// cliext_hash_map_size.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // clear the container and reinspect
    c1.clear();
    System::Console::WriteLine("size() = {0} after clearing", c1.size());

    // add elements and clear again
    c1.insert(Myhash_map::make_value(L'd', 4));
    c1.insert(Myhash_map::make_value(L'e', 5));
    System::Console::WriteLine("size() = {0} after adding 2", c1.size());
    return (0);
    }
[a 1] [b 2] [c 3]
size() = 0 after clearing
size() = 2 after adding 2

hash_map::size_type

兩個項目之間帶正負號距離的類型。

語法

typedef int size_type;

備註

此類型描述非負數專案計數。

範例

// cliext_hash_map_size_type.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // compute positive difference
    Myhash_map::size_type diff = 0;
    for (Myhash_map::iterator it = c1.begin(); it != c1.end(); ++it)
        ++diff;
    System::Console::WriteLine("end()-begin() = {0}", diff);
    return (0);
    }
[a 1] [b 2] [c 3]
end()-begin() = 3

hash_map::swap

交換兩個容器的內容。

語法

void swap(hash_map<Key, Mapped>% right);

參數

right
要交換內容的容器。

備註

成員函式會交換 thisright 之間受控制的序列。 它會在固定時間內執行此動作,而且不會擲回任何例外狀況。 您可以使用它作為交換兩個容器內容的快速方式。

範例

// cliext_hash_map_swap.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // construct another container with repetition of values
    Myhash_map c2;
    c2.insert(Myhash_map::make_value(L'd', 4));
    c2.insert(Myhash_map::make_value(L'e', 5));
    c2.insert(Myhash_map::make_value(L'f', 6));
    for each (Myhash_map::value_type elem in c2)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // swap and redisplay
    c1.swap(c2);
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    for each (Myhash_map::value_type elem in c2)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]
[d 4] [e 5] [f 6]
[d 4] [e 5] [f 6]
[a 1] [b 2] [c 3]

hash_map::to_array

將受控制序列複製到新的陣列。

語法

cli::array<value_type>^ to_array();

備註

成員函式會傳回包含受控制序列的陣列。 您可以使用它,以陣列形式取得受控制序列的複本。

範例

// cliext_hash_map_to_array.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // copy the container and modify it
    cli::array<Myhash_map::value_type>^ a1 = c1.to_array();

    c1.insert(Myhash_map::make_value(L'd', 4));
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    // display the earlier array copy
    for each (Myhash_map::value_type elem in a1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3] [d 4]
[a 1] [b 2] [c 3]

hash_map::upper_bound

尋找符合指定索引鍵的範圍結尾。

語法

iterator upper_bound(key_type key);

參數

key
要搜尋的索引鍵值。

備註

成員函式會決定受控制序列中的最後一個專案,該元素 X 會雜湊至與 相同的貯 key 體,而且其順序 key 相當於 。 如果不存在這類專案,或者如果 X 為受控制序列中的最後一個專案,則會傳 end() 回 ;否則會傳回指定 以外的 X 第一個專案的反覆運算器。 您可以使用它來找出目前在受控制序列中符合指定索引鍵之元素序列的結尾。

範例

// cliext_hash_map_upper_bound.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]"
    for each (Myhash_map::value_type elem in c1)
        System::Console::Write("[{0} {1}] ", elem->first, elem->second);
    System::Console::WriteLine();

    System::Console::WriteLine("upper_bound(L'x')==end() = {0}",
        c1.upper_bound(L'x') == c1.end());

    Myhash_map::iterator it = c1.upper_bound(L'a');
    System::Console::WriteLine("*upper_bound(L'a') = [{0} {1}]",
        it->first, it->second);
    it = c1.upper_bound(L'b');
    System::Console::WriteLine("*upper_bound(L'b') = [{0} {1}]",
        it->first, it->second);
    return (0);
    }
[a 1] [b 2] [c 3]
upper_bound(L'x')==end() = True
*upper_bound(L'a') = [b 2]
*upper_bound(L'b') = [c 3]

hash_map::value_comp

複製兩個元素值的排序委派。

語法

value_compare^ value_comp();

備註

成員函式會傳回用來排序受控制序列的排序委派。 您可以使用它來比較兩個元素值。

範例

// cliext_hash_map_value_comp.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    Myhash_map::value_compare^ kcomp = c1.value_comp();

    System::Console::WriteLine("compare([L'a', 1], [L'a', 1]) = {0}",
        kcomp(Myhash_map::make_value(L'a', 1),
            Myhash_map::make_value(L'a', 1)));
    System::Console::WriteLine("compare([L'a', 1], [L'b', 2]) = {0}",
        kcomp(Myhash_map::make_value(L'a', 1),
            Myhash_map::make_value(L'b', 2)));
    System::Console::WriteLine("compare([L'b', 2], [L'a', 1]) = {0}",
        kcomp(Myhash_map::make_value(L'b', 2),
            Myhash_map::make_value(L'a', 1)));
    System::Console::WriteLine();
    return (0);
    }
compare([L'a', 1], [L'a', 1]) = True
compare([L'a', 1], [L'b', 2]) = True
compare([L'b', 2], [L'a', 1]) = False

hash_map::value_compare

兩個元素值的排序委派。

語法

Microsoft::VisualC::StlClr::BinaryDelegate<generic_value, generic_value, bool>
    value_compare;

備註

此類型與決定其值引數順序的委派同義。

範例

// cliext_hash_map_value_compare.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    Myhash_map::value_compare^ kcomp = c1.value_comp();

    System::Console::WriteLine("compare([L'a', 1], [L'a', 1]) = {0}",
        kcomp(Myhash_map::make_value(L'a', 1),
            Myhash_map::make_value(L'a', 1)));
    System::Console::WriteLine("compare([L'a', 1], [L'b', 2]) = {0}",
        kcomp(Myhash_map::make_value(L'a', 1),
            Myhash_map::make_value(L'b', 2)));
    System::Console::WriteLine("compare([L'b', 2], [L'a', 1]) = {0}",
        kcomp(Myhash_map::make_value(L'b', 2),
            Myhash_map::make_value(L'a', 1)));
    System::Console::WriteLine();
    return (0);
    }
compare([L'a', 1], [L'a', 1]) = True
compare([L'a', 1], [L'b', 2]) = True
compare([L'b', 2], [L'a', 1]) = False

hash_map::value_type

元素的類型。

語法

typedef generic_value value_type;

備註

此類型是 generic_value的同義字。

範例

// cliext_hash_map_value_type.cpp
// compile with: /clr
#include <cliext/hash_map>

typedef cliext::hash_map<wchar_t, int> Myhash_map;
int main()
    {
    Myhash_map c1;
    c1.insert(Myhash_map::make_value(L'a', 1));
    c1.insert(Myhash_map::make_value(L'b', 2));
    c1.insert(Myhash_map::make_value(L'c', 3));

    // display contents " [a 1] [b 2] [c 3]" using value_type
    for (Myhash_map::iterator it = c1.begin(); it != c1.end(); ++it)
        {   // store element in value_type object
        Myhash_map::value_type val = *it;
        System::Console::Write("[{0} {1}] ", val->first, val->second);
        }
    System::Console::WriteLine();
    return (0);
    }
[a 1] [b 2] [c 3]