此模板类描述了一个对象,该对象控制一个具有双向访问权限的变长元素序列。 使用容器 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>
    { ..... };

parameters

受控序列中元素的键组件的类型。

Mapped
受控序列中某个元素的另一个组件的类型。

标头:<cliext/hash_map>

命名空间:

对象为它控制的序列分配并释放存储,作为双向链接列表中的单个节点。 为了加快访问速度,对象还会将指针的变长数组保留到列表(哈希表)中,从而有效地将整个列表作为子列表或存储桶的序列进行管理。 它将元素插入到存储桶中,通过更改节点之间的链接来保持有序,而不是通过将一个节点的内容复制到另一个节点。 这意味着你可以自由插入和移除元素,不会干扰到其余的元素。

对象通过调用 类型的 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,则 XY 被认为具有等效顺序。

行为类似于 operator<=(key_type, key_type)operator>=(key_type, key_type)operator==(key_type, key_type) 定义等效排序的任何排序规则。

容器只确保其键具有等效排序 (的元素,以及与同一整数值) 的哈希在存储桶中相邻。 与模板类 hash_multimap不同,模板类 hash_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();

该成员函数将返回存储桶的当前数量。 用于确定哈希表的大小。

// 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等效排序的元素数。 用于确定受控序列中当前与指定键匹配的元素数。

// 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);
要搜索的键值。

成员函数返回一对迭代器 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)

parameters

first
要擦除的范围的开头。

要擦除的键值。

要擦除的范围的末尾。

where
要擦除的元素。

第一个成员函数删除由 where指向的受控序列的 元素,并返回一个迭代器,该迭代器指定移除的元素之外剩余的第一个元素,或者 end() 如果不存在此类元素,则返回该迭代器。 用于移除单个元素。

第二个成员函数删除范围 [first,) 中受控序列的元素, last 并返回一个迭代器,该迭代器指定除删除的任何元素之外剩余的第一个元素,或者 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等效的排序,则成员函数将返回指定其中一个元素的迭代器;否则返回 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);

parameters

first
要插入的范围的开头。

hashfn
用于将键映射到存储桶的哈希函数。

要插入的范围的末尾。

受控序列的排序谓词。

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_mapright控制的顺序的副本,具有默认的排序谓词和哈希函数。

构造函数:

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

初始化有序列 [right->begin(), right->end())、默认排序谓词以及默认哈希函数的受控序列。 使用它来指定初始受控序列,该序列是对象 hash_mapright控制的顺序的副本,具有默认的排序谓词和哈希函数。

构造函数:

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

初始化有序列 [first, last)、默认排序谓词以及默认哈希函数的受控序列。 可以用它来使受控序列成为另一个序列的副本,并使用默认的排序谓词和哈希函数。

构造函数:

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

使用序列 [firstlast) ,使用排序谓词 pred和默认哈希函数初始化受控序列。 可以用它来使受控序列成为另一个序列的副本,并使用指定的排序谓词和默认哈希函数。

构造函数:

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);

parameters

first
要插入的范围的开头。

要插入的范围的末尾。

right
要插入的枚举。

要插入的键值。

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

每个成员函数都插入了一个由其余操作数指定的序列。

第一个成员函数尝试插入一个值为 val 的元素,并返回一对 X 值。 如果 X.second 为 true,则 X.first 指定新插入的元素;否则 X.first 指定一个具有等效顺序的现有元素,不插入新元素。 可以用它来插入单个元素。

第二个成员函数插入一个具有 值的 val元素,使用 where 作为提示 (以提高性能) ,并返回一个指定新插入元素的迭代器。 使用它来插入单个元素,该元素可能紧邻你知道的元素。

第三个成员函数插入序列 [first, last)。 可以用它来插入从另一个序列复制的零个或多个元素。

第四个成员函数插入 由 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);
要搜索的键值。

成员函数确定受控序列中的第一个元素,该元素 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);

parameters

要使用的键值。

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

获取或设置每个存储桶的最多元素数。

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

parameters

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元素。 如果找到一个元素,则返回关联的映射值;否则,将插入 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);

parameters

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);
要搜索的键值。

成员函数确定受控序列中的最后一个元素,该元素 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]