STL学习----入门(1)[unordered_set]

#include:

C++11

 
    
  1. //
  2. template < class Key,
  3. class Hash = hash<Key>,
  4. class Pred = equal_to<Key>,
  5. class Alloc = allocator<Key>
  6. > class unordered_set;

无序集合(Unordered Set)容器是一个存储唯一(Unique,即无重复)元素的关联容器(Associative container),容器中的元素无特别的次序关系。该容器允许基于值地快速元素检索。

An unordered_set is an unordered associative container that supports unique keys (an unordered_set contains at most one of each key value) and in which the elements’ keys are the elements themselves.

容器特性:

关联(Associative)

关联容器中的元素是通过主键(Key)而不是它们在容器中的绝对位置来引用的。

无序(Unordered)

无序容器通过 hash 表来组织它们的元素,允许通过主键快速地访问元素。

集合(Set)

元素的值同时可以用来标志对应的元素。

键唯一(Unique keys)

容器中不存在两个元素有相同的主键。

能够感知内存分配器的(Allocator-aware)

容器使用一个内存分配器对象来动态地处理它的存储需求。

  • 模板参数

    Key

    元素的类型。

    在类模板内部,使用其别名为 key_type 及 value_type 的成员类型。

    Hash

    一元谓词,以一个 Key 类型的对象为参数,返回一个基于该对象的 size_t 类型的唯一值(无重复值)。

    可以是函数指针(Function pointer)类型或函数对象(Function object)类型。

    在类模板内部,使用其别名为 hasher 的成员类型。

    Pred

    二元谓词,以两个 Key 类型的对象为参数,返回一个 bool 值,如果第一个参数等价于第二个参数,该 bool 值为 true,否则为 false。默认为 std::equal_to。

    可以是函数指针类型(Function pointer)类型或函数对象(Function object)类型。

    在类模板内部,使用其别名为 key_equal 的成员类型。

    Alloc

    容器内部用来管理内存分配及释放的内存分配器的类型。

    这个参数是可选的,它的默认值是 std::allocator,这个是一个最简单的非值依赖的(Value-independent)内存分配器。在类模板内部,使用其别名为 allocator_type 的成员类型。

  • 详细说明

    在一个 unordered_set 容器中,元素的值同时可以用来标志对应的元素(即值是自身的主键),每个值必须是唯一的。主键是不可修改的,因此在 unordered_set 中的元素不能被逐个修改(所有元素保持恒定),但是可以删除某个元素或插入新的元素。

    在 unordered_set 内部,元素不会按任何顺序排序,而是通过元素值的 hash 值将元素分组放置到各个槽(Bucket,也可译成“桶”)中,这样就能通过元素值快速地访问各个对应的元素(平均耗时为一个常量,即时间复杂度为 O(1))。

    在访问容器中的某个元素时,unordered_set 容器比 set 容器高效,而在迭代容器元素的某个子集时,前者比后者稍微低效了一点。

    unordered_set 容器支持正向迭代。

    The unordered_set class supports forward iterators.

  • 成员类型

    成员类型 定义
    key_type 第一个模板参数 Key
    value_type 第一个模板参数 Key
    size_type 无符号整数类型(通常为 size_t
    difference_type 有符号整数类型(通常为 ptrdiff_t
    hasher 第二个模板参数 Hash
    key_equal 第三个模板参数 KeyEqual
    allocator_type 第四个模板参数 Alloc
    reference value_type&
    const_reference const value_type&
    pointer std::allocator_traits::pointer
    const_pointer std::allocator_traits::const_pointer
    iterator 正向迭代器
    const_iterator 常正向迭代器
    local_iterator 本地迭代器,可以迭代一个槽的元素,但不能跨槽迭代
    const_local_iterator 常本地迭代器,可以迭代一个槽的元素,但不能跨槽迭代
  • 成员函数

    (constructor) 创建 unordered_set
    (destructor) 释放 unordered_set
    operator= 值赋操作

    ​Iterators:

    begin 返回指向容器起始位置的迭代器(iterator
    end 返回指向容器末尾位置的迭代器
    cbegin 返回指向容器起始位置的常迭代器(const_iterator
    cend 返回指向容器末尾位置的常迭代器

    Capacity:

    size 返回有效元素个数
    max_size 返回 unordered_set 支持的最大元素个数
    empty 判断是否为空

    Modifiers:

    insert 插入元素
    erase 删除元素
    swap 交换内容
    clear 清空内容
    emplace 构造及插入一个元素
    emplace_hint 按提示构造及插入一个元素

    Observers:

    hash_function 返回 hash 函数
    key_eq 返回主键等价性判断谓词

    Operations:

    find 通过给定主键查找元素
    count 返回匹配给定主键的元素的个数
    equal_range 返回值匹配给定搜索值的元素组成的范围

    Buckets:

    bucket_count 返回槽(Bucket)
    max_bucket_count 返回最大槽数
    bucket_size 返回槽大小
    bucket 返回元素所在槽的序号

    Hash policy:

    load_factor 返回载入因子,即一个元素槽(Bucket)的最大元素数
    max_load_factor 返回或设置最大载入因子
    rehash 设置槽数
    reserve 请求改变容器容量

    Allocator:

    get_allocator 获得内存分配器
  • 非成员函数

    operator==、operator!=、operator<、operator<=、operator>、operator>= 

    关系操作符
    std::swap 交换两个无序集合容器的内容
  • 算法相关

    头文件中包含大量与 unordered_set 容器相关的算法,常使用的有

    搜索算法 std::adjacent_findstd::count
    std::count_ifstd::find
    std::find_ifstd::find_end
    std::find_first_ofstd::search
    std::search_nstd::equal
    std::mismatch
    二分查找(Binary search) std::lower_boundstd::upper_bound
    std::equal_rangestd::binary_search
    集合(Set)操作 std::includesstd::set_difference
    std::set_intersectionstd::set_union
    std::set_symmetric_difference
    最大与最小 std::min_elementstd::max_element
    字典序比较 std::lexicographical_compare
    排列生成器 std::next_permutation
    std::prev_permutation
    其它操作 std::all_ofstd::any_ofstd::none_of
    std::for_eachstd::copystd::copy_if
    std::copy_nstd::copy_backward
    std::movestd::move_backward
    std::swap_rangesstd::iter_swap
    std::transformstd::replace
    std::replace_ifstd::replace_copy
    std::replace_copy_ifstd::fill
    std::fill_nstd::generate
    std::generate_nstd::remove
    std::remove_ifstd::unique
    std::unique_copystd::reverse
    ​std::reverse_copystd::rotate
    std::rotate_copystd::random_shuffle
    std::shufflestd::partition
    std::is_partitionedstd::stable_partition
    std::partition_copystd::merge


你可能感兴趣的:(STL学习----入门(1)[unordered_set])