hash_set (STL/CLR)

此模板类描述了一个对象,该对象控制一个具有双向访问权限且长度可变的元素序列。 使用容器 hash_set 将元素序列作为哈希表进行管理,每个表项存储节点的一个双向链接列表,并且每个节点存储一个元素。 每个元素的值用作键来对序列进行排序。

在以下说明中, GValueGKey 相同,而后者又与 Key 相同,除非后者是 ref 类型,在这种情况下它是 Key^

语法

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

参数

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

要求

标头:<cliext/hash_set>

命名空间:cliext

声明

类型定义 说明
hash_set::const_iterator 受控序列的常量迭代器的类型。
hash_set::const_reference 元素的常量引用的类型。
hash_set::const_reverse_iterator 受控序列的常量反向迭代器的类型。
hash_set::difference_type 两个元素间的(可能带符号)距离的类型。
hash_set::generic_container 容器的泛型接口的类型。
hash_set::generic_iterator 容器的泛型接口的迭代器的类型。
hash_set::generic_reverse_iterator 容器的泛型接口的反向迭代器的类型。
hash_set::generic_value 容器的泛型接口的元素类型。
hash_set::hasher 键的哈希委托。
hash_set::iterator 受控序列的迭代器的类型。
hash_set::key_compare 两个键的排序委托。
hash_set::key_type 排序键的类型。
hash_set::reference 元素的引用的类型。
hash_set::reverse_iterator 受控序列的反向迭代器的类型。
hash_set::size_type 两个元素间的(非负)距离的类型。
hash_set::value_compare 两个元素值的排序委托。
hash_set::value_type 元素的类型。
成员函数 说明
hash_set::begin 指定受控序列的开头。
hash_set::bucket_count 对存储桶数进行计数。
hash_set::clear 删除所有元素。
hash_set::count 对与指定键匹配的元素进行计数。
hash_set::empty 测试元素是否存在。
hash_set::end 指定受控序列的末尾。
hash_set::equal_range 查找与指定键匹配的范围。
hash_set::erase 移除指定位置处的元素。
hash_set::find 查找与指定键匹配的元素。
hash_set::hash_delegate 复制键的哈希委托。
hash_set::hash_set 构造容器对象。
hash_set::insert 添加元素。
hash_set::key_comp 复制两个键的排序委托。
hash_set::load_factor 对每个存储桶的平均元素数进行计数。
hash_set::lower_bound 查找与指定键匹配的范围的开头。
hash_set::make_value 构造值对象。
hash_set::max_load_factor 获取或设置每个存储桶的最多元素数。
hash_set::rbegin 指定反向受控序列的开头。
hash_set::rehash 重新生成哈希表。
hash_set::rend 指定反向受控序列的末尾。
hash_set::size 对元素数进行计数。
hash_set::swap 交换两个容器的内容。
hash_set::to_array 将受控序列复制到新数组。
hash_set::upper_bound 查找与指定键匹配的范围的末尾。
hash_set::value_comp 复制两个元素值的排序委托。
运算符 说明
hash_set::operator= 替换受控序列。

接口

接口 说明
ICloneable 复制对象。
IEnumerable 对元素进行排序。
ICollection 维护元素组。
IEnumerable<T> 对类型化元素进行排序。
ICollection<T> 维护类型化元素组。
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,则 XY 被认为具有等效顺序。

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

容器只确保键具有等效顺序(以及哈希处理到同一整数值)的元素在一个存储桶内相邻。 与模板类 hash_multiset (STL/CLR) 不同,模板类 hash_set 的对象可确保所有元素的键都是唯一的。 (没有两个键具有等效顺序。)

对象通过调用类型为 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_set 支持双向迭代器,这意味着你可以通过指定受控序列中某个元素的迭代器来单步执行相邻元素。 有一个特殊的头节点对应于 end() 返回的迭代器。 可以递减此迭代器,以到达受控序列中的最后一个元素(如果存在)。 可以递增 hash_set 迭代器以到达头节点,然后它与 end() 相等。 但你不能取消引用 end() 返回的迭代器。

不能在给定其数值位置的情况下直接引用 hash_set 元素。 这需要一个随机访问迭代器。

hash_set 迭代器将句柄存储到其关联的 hash_set 节点,该节点又将句柄存储到其关联的容器。 只能将迭代器与其关联的容器对象一起使用。 只要其关联的 hash_set 节点与某些 hash_set 关联,hash_set 迭代器就保持有效。 此外,有效的迭代器可取消引用。 只要所指定的元素值不等于 end(),就可以使用该迭代器对其进行访问或更改。

擦除或移除元素会为其存储的值调用析构函数。 销毁容器会擦除所有元素。 因此,元素类型为 ref 类的容器可确保没有元素的生存期长于容器。 但是,句柄的容器不会销毁其元素。

成员

hash_set::begin

指定受控序列的开头。

语法

iterator begin();

备注

该成员函数返回一个双向迭代器,指定受控序列的第一个元素,或刚超出空序列末尾的位置。 用于获取一个迭代器,该迭代器指定受控序列的 current 开头,但如果受控序列的长度发生更改,则该迭代器的状态也会发生更改。

示例

// cliext_hash_set_begin.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // inspect first two items
    Myhash_set::iterator it = c1.begin();
    System::Console::WriteLine("*begin() = {0}", *it);
    System::Console::WriteLine("*++begin() = {0}", *++it);
    return (0);
    }

hash_set::bucket_count

对存储桶数进行计数。

语法

int bucket_count();

备注

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

示例

// cliext_hash_set_bucket_count.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    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 b c
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_set::clear

删除所有元素。

语法

void clear();

备注

该成员函数有效调用 erase(begin(), end())。 用于确保受控序列为空。

示例

// cliext_hash_set_clear.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

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

    // add elements and clear again
    c1.insert(L'a');
    c1.insert(L'b');

    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    c1.clear();
    System::Console::WriteLine("size() = {0}", c1.size());
    return (0);
    }
a b c
size() = 0
a b
size() = 0

hash_set::const_iterator

受控序列的常量迭代器的类型。

语法

typedef T2 const_iterator;

备注

该类型描述了可充当受控序列的常量双向迭代器的未指定类型 T2 的对象。

示例

// cliext_hash_set_const_iterator.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display contents " a b c"
    Myhash_set::const_iterator cit = c1.begin();
    for (; cit != c1.end(); ++cit)
        System::Console::Write("{0} ", *cit);
    System::Console::WriteLine();
    return (0);
    }
a b c

hash_set::const_reference

元素的常量引用的类型。

语法

typedef value_type% const_reference;

备注

该类型描述了对元素的常量引用。

示例

// cliext_hash_set_const_reference.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    Myhash_set::const_iterator cit = c1.begin();
    for (; cit != c1.end(); ++cit)
        {   // get a const reference to an element
        Myhash_set::const_reference cref = *cit;
        System::Console::Write("{0} ", cref);
        }
    System::Console::WriteLine();
    return (0);
    }
a b c

hash_set::const_reverse_iterator

受控序列的常量反向迭代器的类型。

语法

typedef T4 const_reverse_iterator;

备注

该类型描述了可充当受控序列的常量反向迭代器的未指定类型 T4 的对象。

示例

// cliext_hash_set_const_reverse_iterator.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display contents " a b c" reversed
    Myhash_set::const_reverse_iterator crit = c1.rbegin();
    for (; crit != c1.rend(); ++crit)
        System::Console::Write("{0} ", *crit);
    System::Console::WriteLine();
    return (0);
    }
c b a

hash_set::count

查找与指定键匹配的元素数。

语法

size_type count(key_type key);

参数

key
要搜索的键值。

备注

该成员函数返回受控序列中与 key 具有等效顺序的元素的数量。 用于确定受控序列中当前与指定键匹配的元素数。

示例

// cliext_hash_set_count.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    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 b c
count(L'A') = 0
count(L'b') = 1
count(L'C') = 0

hash_set::difference_type

两个元素间的带符号距离的类型。

语法

typedef int difference_type;

备注

该类型描述了可能为负的元素计数。

示例

// cliext_hash_set_difference_type.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

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

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

hash_set::empty

测试元素是否存在。

语法

bool empty();

备注

对于空受控序列,该成员函数返回 true。 它等效于 size() == 0。 用于测试 hash_set 是否为空。

示例

// cliext_hash_set_empty.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    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 b c
size() = 3
empty() = False
size() = 0
empty() = True

hash_set::end

指定受控序列的末尾。

语法

iterator end();

备注

该成员函数返回一个双向迭代器,指向刚超出受控序列末尾的位置。 用于获取一个迭代器,该迭代器指定受控序列的末尾;如果受控序列的长度改变,其状态不会改变。

示例

// cliext_hash_set_end.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // inspect last two items
    Myhash_set::iterator it = c1.end();
    --it;
    System::Console::WriteLine("*-- --end() = {0}", *--it);
    System::Console::WriteLine("*--end() = {0}", *++it);
    return (0);
    }
a b c
*-- --end() = b
*--end() = c

hash_set::equal_range

查找与指定键匹配的范围。

语法

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

参数

key
要搜索的键值。

备注

成员函数返回一对迭代器 cliext::pair<iterator, iterator>(lower_bound(key), upper_bound(key))。 用于确定受控序列中当前与指定键匹配的元素范围。

示例

// cliext_hash_set_equal_range.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
typedef Myhash_set::pair_iter_iter Pairii;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    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} ", *pair1.first);
    System::Console::WriteLine();
    return (0);
    }
a b c
equal_range(L'x') empty = True
b

hash_set::erase

移除指定位置处的元素。

语法

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

参数

first
要擦除的范围的开头。

key
要擦除的键值。

last
要擦除的范围的末尾。

where
要擦除的元素。

备注

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

第二个成员函数移除范围 [first, last] 中的受控序列的元素,并返回一个迭代器,该迭代器指定除任何移除的元素之外的第一个元素,如果不存在这样的元素,则指定 end()。 用于移除零个或多个连续元素。

第三个成员函数移除受控序列中其键与 key 具有等效顺序的任何元素,并返回移除的元素数的计数。 用于移除与指定键匹配的所有元素并对其进行计数。

每个元素擦除所需的时间与受控序列中元素数的对数成正比。

示例

// cliext_hash_set_erase.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // erase an element and reinspect
    System::Console::WriteLine("erase(begin()) = {0}",
        *c1.erase(c1.begin()));

    // add elements and display " b c d e"
    c1.insert(L'd');
    c1.insert(L'e');
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // erase all but end
    Myhash_set::iterator it = c1.end();
    System::Console::WriteLine("erase(begin(), end()-1) = {0}",
        *c1.erase(c1.begin(), --it));
    System::Console::WriteLine("size() = {0}", c1.size());
    return (0);
    }
a b c
erase(begin()) = b
b c d e
erase(begin(), end()-1) = e
size() = 1

hash_set::find

查找与指定键匹配的元素。

语法

iterator find(key_type key);

参数

key
要搜索的键值。

备注

如果受控序列中的至少一个元素具有与 key 等效的顺序,该成员函数将返回指定其中一个元素的迭代器;否则返回 end()。 用于定位受控序列中当前与指定键匹配的元素。

示例

// cliext_hash_set_find.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

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

hash_set::generic_container

容器的泛型接口的类型。

语法

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

备注

该类型描述此模板容器类的泛型接口。

示例

// cliext_hash_set_generic_container.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct a generic container
    Myhash_set::generic_container^ gc1 = %c1;
    for each (wchar_t elem in gc1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // modify generic and display original
    gc1->insert(L'd');
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // modify original and display generic
    c1.insert(L'e');
    for each (wchar_t elem in gc1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
a b c
a b c d
a b c d e

hash_set::generic_iterator

要与容器的泛型接口一起使用的迭代器的类型。

语法

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

备注

该类型描述可与此模板容器类的泛型接口一起使用的泛型迭代器。

示例

// cliext_hash_set_generic_iterator.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct a generic container
    Myhash_set::generic_container^ gc1 = %c1;
    for each (wchar_t elem in gc1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // get an element and display it
    Myhash_set::generic_iterator gcit = gc1->begin();
    Myhash_set::generic_value gcval = *gcit;
    System::Console::WriteLine("{0} ", gcval);
    return (0);
    }
a b c
a b c
a

hash_set::generic_reverse_iterator

要与容器的泛型接口一起使用的反向迭代器的类型。

语法

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

备注

该类型描述可与此模板容器类的泛型接口一起使用的泛型反向迭代器。

示例

// cliext_hash_set_generic_reverse_iterator.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct a generic container
    Myhash_set::generic_container^ gc1 = %c1;
    for each (wchar_t elem in gc1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // get an element and display it
    Myhash_set::generic_reverse_iterator gcit = gc1->rbegin();
    Myhash_set::generic_value gcval = *gcit;
    System::Console::WriteLine("{0} ", gcval);
    return (0);
    }
a b c
a b c
c

hash_set::generic_value

要与容器的泛型接口一起使用的元素的类型。

语法

typedef GValue generic_value;

备注

该类型描述一个 GValue 类型的对象,该对象描述可与此模板容器类的泛型接口一起使用的存储元素值。

示例

// cliext_hash_set_generic_value.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct a generic container
    Myhash_set::generic_container^ gc1 = %c1;
    for each (wchar_t elem in gc1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // get an element and display it
    Myhash_set::generic_iterator gcit = gc1->begin();
    Myhash_set::generic_value gcval = *gcit;
    System::Console::WriteLine("{0} ", gcval);
    return (0);
    }
a b c
a b c
a

hash_set::hash_delegate

查找与指定键匹配的元素。

语法

hasher^ hash_delegate();

备注

成员函数返回用于将键值转换为整数的委托。 用于对键进行哈希处理。

示例

// cliext_hash_set_hash_delegate.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    Myhash_set::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_set::hash_set

构造容器对象。

语法

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

参数

first
要插入的范围的开头。

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

last
要插入的范围的末尾。

pred
受控序列的排序谓词。

right
要插入的对象或范围。

注解

构造函数:

hash_set();

初始化没有元素、有默认排序谓词 key_compare() 以及默认哈希函数的受控序列。 可以用它来指定一个空的初始受控序列,使用默认排序谓词和哈希函数。

构造函数:

explicit hash_set(key_compare^ pred);

初始化没有元素、有排序谓词 pred 以及默认哈希函数的受控序列。 可以用它来指定一个空的初始受控序列,使用指定的排序谓词和默认哈希函数。

构造函数:

hash_set(key_compare^ pred, hasher^ hashfn);

初始化没有元素、有排序谓词 pred 以及哈希函数 hashfn 的受控序列。 可以用它来指定一个空的初始受控序列,使用指定的排序谓词和哈希函数。

构造函数:

hash_set(hash_set<Key>% right);

初始化有序列 [right.begin(), right.end())、默认排序谓词以及默认哈希函数的受控序列。 可以用它来指定一个初始受控序列,它是由 hash_set 对象 right 控制的序列的副本,使用默认排序谓词和哈希函数。

构造函数:

hash_set(hash_set<Key>^ right);

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

构造函数:

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

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

构造函数:

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

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

构造函数:

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

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

构造函数:

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

初始化有枚举 right 指定的序列、默认排序谓词以及默认哈希函数的受控序列。 可以用它来使受控序列成为枚举器所描述的另一个序列的副本,并使用默认的排序谓词和哈希函数。

构造函数:

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

初始化有枚举 right 指定的序列、排序谓词 pred 以及默认哈希函数的受控序列。 可以用它来使受控序列成为枚举器所描述的另一个序列的副本,并使用指定的排序谓词和默认哈希函数。

构造函数:

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

初始化有枚举 right 指定的序列、排序谓词 pred 以及哈希函数 hashfn 的受控序列。 可以用它来使受控序列成为枚举器所描述的另一个序列的副本,并使用指定的排序谓词和哈希函数。

示例

// cliext_hash_set_construct.cpp
// compile with: /clr
#include <cliext/hash_set>

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

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

    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

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

    c2.insert(c1.begin(), c1.end());
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

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

    c2h.insert(c1.begin(), c1.end());
    for each (wchar_t elem in c2h)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    System::Console::WriteLine();

    // construct with an iterator range
    Myhash_set c3(c1.begin(), c1.end());
    for each (wchar_t elem in c3)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

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

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

    // construct with an enumeration
    Myhash_set c5(   // NOTE: cast is not needed
        (System::Collections::Generic::IEnumerable<wchar_t>^)%c3);
    for each (wchar_t elem in c5)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

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

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

    // construct from a generic container
    Myhash_set c7(c4);
    for each (wchar_t elem in c7)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct by copying another container
    Myhash_set c8(%c3);
    for each (wchar_t elem in c8)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
size() = 0
a b c
size() = 0
a b c
size() = 0
c b a

a b c
a b c
c b a

a b c
a b c
c b a

a b c
a b c

hash_set::hasher

键的哈希委托。

语法

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

备注

该类型描述将键值转换为整数的委托。

示例

// cliext_hash_set_hasher.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    Myhash_set::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_set::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 指定一个具有等效顺序的现有元素,不插入新元素。 可以用它来插入单个元素。

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

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

第四个成员函数插入由 right 指定的序列。 用于插入枚举器所描述的序列。

每个元素插入所需的时间与受控序列中元素数的对数成正比。 但是,如果系统提示在插入点旁边指定一个元素,则插入可以在摊销的恒定时间内发生。

示例

// cliext_hash_set_insert.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
typedef Myhash_set::pair_iter_bool Pairib;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

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

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

    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // insert a single value with hint
    System::Console::WriteLine("insert(begin(), L'y') = {0}",
        *c1.insert(c1.begin(), L'y'));
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // insert an iterator range
    Myhash_set c2;
    Myhash_set::iterator it = c1.end();
    c2.insert(c1.begin(), --it);
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

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

hash_set::iterator

受控序列的迭代器的类型。

语法

typedef T1 iterator;

备注

该类型描述了可充当受控序列的双向迭代器的未指定类型 T1 的对象。

示例

// cliext_hash_set_iterator.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display contents " a b c"
    Myhash_set::iterator it = c1.begin();
    for (; it != c1.end(); ++it)
        System::Console::Write("{0} ", *it);
    System::Console::WriteLine();
    return (0);
    }
a b c

hash_set::key_comp

复制两个键的排序委托。

语法

key_compare^key_comp();

备注

该成员函数返回用于对受控序列进行排序的排序委托。 用于对两个键进行比较。

示例

// cliext_hash_set_key_comp.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    Myhash_set::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_set 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_set::key_compare

两个键的排序委托。

语法

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

备注

该类型是委托的同义词,用于确定其键参数的顺序。

示例

// cliext_hash_set_key_compare.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    Myhash_set::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_set 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_set::key_type

排序键的类型。

语法

typedef Key key_type;

备注

该类型是模板参数 Key 的同义词。

示例

// cliext_hash_set_key_type.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display contents " a b c" using key_type
    for (Myhash_set::iterator it = c1.begin(); it != c1.end(); ++it)
        {   // store element in key_type object
        Myhash_set::key_type val = *it;

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

hash_set::load_factor

对每个存储桶的平均元素数进行计数。

语法

float load_factor();

备注

成员函数返回 (float)size() / bucket_count()。 用于确定平均存储桶大小。

示例

// cliext_hash_set_load_factor.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    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 b c
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_set::lower_bound

查找与指定键匹配的范围的开头。

语法

iterator lower_bound(key_type key);

参数

key
要搜索的键值。

备注

成员函数确定受控序列中可哈希处理为与 key 相同的存储桶并具有与 key 等效顺序的第一个元素 X。 如果没有这样的元素,则返回 end();否则返回用于指定 X 的迭代器。 可以用它来定位受控序列中当前与指定键匹配的元素的序列的开头。

示例

// cliext_hash_set_lower_bound.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

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

    System::Console::WriteLine("*lower_bound(L'a') = {0}",
        *c1.lower_bound(L'a'));
    System::Console::WriteLine("*lower_bound(L'b') = {0}",
        *c1.lower_bound(L'b'));
    return (0);
    }
a b c
lower_bound(L'x')==end() = True
*lower_bound(L'a') = a
*lower_bound(L'b') = b

hash_set::make_value

构造值对象。

语法

static value_type make_value(key_type key);

参数

key
要使用的键值。

备注

成员函数返回一个键为 keyvalue_type 对象。 可以用它来编写适合与多个其他成员函数一起使用的对象。

示例

// cliext_hash_set_make_value.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(Myhash_set::make_value(L'a'));
    c1.insert(Myhash_set::make_value(L'b'));
    c1.insert(Myhash_set::make_value(L'c'));

    // display contents " a b c"
    for each (Myhash_set::value_type elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c

hash_set::max_load_factor

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

语法

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

参数

new_factor
要存储的新的最大加载因子。

备注

第一个成员函数将返回当前存储的最大加载因子。 用于确定平均存储桶大小的最大值。

第二个成员函数将用 new_factor 替换已存储的最大加载因子。 在后续插入之前,不会发生自动重新哈希处理。

示例

// cliext_hash_set_max_load_factor.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    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);
    }

hash_set::operator=

替换受控序列。

语法

hash_set<Key>% operator=(hash_set<Key>% right);

参数

right
用于复制的容器。

注解

成员运算符将 right 复制到对象,然后返回 *this。 用它将受控序列替换为 right 中的受控序列的副本。

示例

// cliext_hash_set_operator_as.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display contents " a b c"
    for each (Myhash_set::value_type elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // assign to a new container
    Myhash_set c2;
    c2 = c1;
// display contents " a b c"
    for each (Myhash_set::value_type elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
a b c

hash_set::rbegin

指定反向受控序列的开头。

语法

reverse_iterator rbegin();

备注

该成员函数返回一个反向迭代器,指定受控序列的最后一个元素,或刚超出空序列开头的位置。 因此,它指定反向序列的 beginning。 用于获取一个迭代器,该迭代器指定按相反顺序显示的受控序列的 current 开头,但如果受控序列的长度发生更改,则该迭代器的状态也会发生更改。

示例

// cliext_hash_set_rbegin.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // inspect first two items in reversed sequence
    Myhash_set::reverse_iterator rit = c1.rbegin();
    System::Console::WriteLine("*rbegin() = {0}", *rit);
    System::Console::WriteLine("*++rbegin() = {0}", *++rit);
    return (0);
    }
a b c
*rbegin() = c
*++rbegin() = b

hash_set::reference

元素的引用的类型。

语法

typedef value_type% reference;

备注

该类型描述了对元素的引用。

示例

// cliext_hash_set_reference.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    Myhash_set::iterator it = c1.begin();
    for (; it != c1.end(); ++it)
        {   // get a reference to an element
        Myhash_set::reference ref = *it;
        System::Console::Write("{0} ", ref);
        }
    System::Console::WriteLine();
    return (0);
    }
a b c

hash_set::rehash

重新生成哈希表。

语法

void rehash();

备注

成员函数会重新生成哈希表,确保 load_factor() <= max_load_factor()。 否则,哈希表仅在插入后根据需要增加大小。 (它从不自动减小大小。)用于调整哈希表的大小。

示例

// cliext_hash_set_rehash.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    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 b c
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_set::rend

指定反向受控序列的末尾。

语法

reverse_iterator rend();

备注

该成员函数返回一个反向迭代器,指向刚超出受控序列开头的位置。 因此,它指定反向序列的 end。 用于获取一个迭代器,该迭代器指定按相反顺序显示的受控序列的 current 末尾,但如果受控序列的长度发生更改,则该迭代器的状态也会发生更改。

示例

// cliext_hash_set_rend.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // inspect first two items
    Myhash_set::reverse_iterator rit = c1.rend();
    --rit;
    System::Console::WriteLine("*-- --rend() = {0}", *--rit);
    System::Console::WriteLine("*--rend() = {0}", *++rit);
    return (0);
    }
a b c
*-- --rend() = b
*--rend() = a

hash_set::reverse_iterator

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

语法

typedef T3 reverse_iterator;

备注

该类型描述了可充当受控序列的反向迭代器的未指定类型 T3 的对象。

示例

// cliext_hash_set_reverse_iterator.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display contents " a b c" reversed
    Myhash_set::reverse_iterator rit = c1.rbegin();
    for (; rit != c1.rend(); ++rit)
        System::Console::Write("{0} ", *rit);
    System::Console::WriteLine();
    return (0);
    }
c b a

hash_set::size

对元素数进行计数。

语法

size_type size();

备注

成员函数将返回受控序列的长度。 用于确定受控序列中的当前元素数。 如果只关心序列是否具有非零大小,请参阅 empty()

示例

// cliext_hash_set_size.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    System::Console::WriteLine("size() = {0} starting with 3", c1.size());

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

    // add elements and clear again
    c1.insert(L'a');
    c1.insert(L'b');
    System::Console::WriteLine("size() = {0} after adding 2", c1.size());
    return (0);
    }
a b c
size() = 3 starting with 3
size() = 0 after clearing
size() = 2 after adding 2

hash_set::size_type

两个元素间的带符号距离的类型。

语法

typedef int size_type;

备注

该类型描述非负元素计数。

示例

// cliext_hash_set_size_type.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

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

hash_set::swap

交换两个容器的内容。

语法

void swap(hash_set<Key>% right);

参数

right
要与其交换内容的容器。

备注

成员函数交换 thisright 之间的受控序列。 它在常量时间内执行此操作且不引发异常。 你可以将其用作交换两个容器的内容的快捷方式。

示例

// cliext_hash_set_swap.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // construct another container with repetition of values
    Myhash_set c2;
    c2.insert(L'd');
    c2.insert(L'e');
    c2.insert(L'f');
    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // swap and redisplay
    c1.swap(c2);
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    for each (wchar_t elem in c2)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c
d e f
d e f
a b c

hash_set::to_array

将受控序列复制到新数组。

语法

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

备注

成员函数返回一个包含受控序列的数组。 用于以数组形式获取受控序列的副本。

示例

// cliext_hash_set_to_array.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

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

    c1.insert(L'd');
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

    // display the earlier array copy
    for each (wchar_t elem in a1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();
    return (0);
    }
a b c d
a b c

hash_set::upper_bound

查找与指定键匹配的范围的末尾。

语法

iterator upper_bound(key_type key);

参数

key
要搜索的键值。

备注

成员函数确定受控序列中可哈希处理为与 key 相同的存储桶并具有与 key 等效顺序的最后一个元素 X。 如果不存在这样的元素,或者如果 X 是受控序列中的最后一个元素,则返回 end();否则返回用于指定 X 之外的第一个元素的迭代器。 可以用它来定位受控序列中当前与指定键匹配的元素的序列的末尾。

示例

// cliext_hash_set_upper_bound.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display initial contents " a b c"
    for each (wchar_t elem in c1)
        System::Console::Write("{0} ", elem);
    System::Console::WriteLine();

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

    System::Console::WriteLine("*upper_bound(L'a') = {0}",
        *c1.upper_bound(L'a'));
    System::Console::WriteLine("*upper_bound(L'b') = {0}",
        *c1.upper_bound(L'b'));
    return (0);
    }
a b c
upper_bound(L'x')==end() = True
*upper_bound(L'a') = b
*upper_bound(L'b') = c

hash_set::value_comp

复制两个元素值的排序委托。

语法

value_compare^ value_comp();

备注

该成员函数返回用于对受控序列进行排序的排序委托。 可以用它来对两个元素值进行比较。

示例

// cliext_hash_set_value_comp.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    Myhash_set::value_compare^ kcomp = c1.value_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();
    return (0);
    }
compare(L'a', L'a') = True
compare(L'a', L'b') = True
compare(L'b', L'a') = False

hash_set::value_compare

两个元素值的排序委托。

语法

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

备注

该类型是委托的同义词,用于确定其值参数的顺序。

示例

// cliext_hash_set_value_compare.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    Myhash_set::value_compare^ kcomp = c1.value_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();
    return (0);
    }
compare(L'a', L'a') = True
compare(L'a', L'b') = True
compare(L'b', L'a') = False

hash_set::value_type

元素的类型。

语法

typedef generic_value value_type;

备注

类型是 generic_value 的同义词。

示例

// cliext_hash_set_value_type.cpp
// compile with: /clr
#include <cliext/hash_set>

typedef cliext::hash_set<wchar_t> Myhash_set;
int main()
    {
    Myhash_set c1;
    c1.insert(L'a');
    c1.insert(L'b');
    c1.insert(L'c');

    // display contents " a b c" using value_type
    for (Myhash_set::iterator it = c1.begin(); it != c1.end(); ++it)
        {   // store element in value_type object
        Myhash_set::value_type val = *it;

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