Cpp Standard Library 简明教程

C++ Library - <unordered_set>

Introduction

它是一个关联容器,以无特定顺序存储唯一元素,并允许基于元素的值快速检索单个元素。

It is an associative container that store unique elements in no particular order, and which allow for fast retrieval of individual elements based on their value.

Definition

以下是 std::unordered_set 的定义:

Below is definition of std::unordered_set

template < class Key, class Hash = hash<Key>, class Pred = equal_to<Key>, class Alloc = allocator<Key> > class unordered_set;

Parameters

  1. Key − It defines the type of element.

  2. Hash − It is a unary function object.

  3. Pred − It is a binary predicate that takes two arguments of the same type as the elements and returns a bool.

  4. Alloc − It defines the type of allowcater.

Member types

以下成员类型可作为参数或返回类型,由成员函数使用。

Following member types can be used as parameters or return type by member functions.

Member Type

Definition

Notes

key_type

It is the first template parameter (Key)

value_type

It is the first template parameter (Key)

The same as key_type

hasher

It is the second template parameter (Hash)

defaults to: hash<key_type>

key_equal

It is the third template parameter (Pred)

defaults to: equal_to<key_type>

allocator_type

It is the fourth template parameter (Alloc)

defaults to: allocator<value_type>

reference

Alloc::reference

const_reference

Alloc::const_reference

pointer

Alloc::pointer

for the default allocator: value_type*

const_pointer

Alloc::const_pointer

for the default allocator: const value_type*

iterator

a forward iterator to const value_type

* convertible to const_iterator

const_iterator

a forward iterator to const value_type

*

local_iterator

a forward iterator to const value_type

* convertible to const_local_iterator

const_local_iterator

a forward iterator to const value_type

*

size_type

an unsigned integral type

usually the same as size_t

difference_type

a signed integral type

usually the same as ptrdiff_t

Member functions

下面列出成员函数

Below is list of member functions

Sr.No.

Method & Description

[role="bare"]../cpp_standard_library/cpp_unordered_set_equal_operator.html It is used to assign the content.

Capacity

Sr.No.

Capacity & Description

1

empty It is used to test whether container is empty.

2

size It returns container size.

3

max_size It returns maximum size.

Iterators

Sr.No.

Iterators & Description

1

begin It returns iterator to beginning.

2

end It returns iterator to end.

3

cbegin It returns const_iterator to beginning.

4

cend It return const_iterator to end.

Element lookup

Sr.No.

Element lookup & Description

1

find It is used to get iterator to element.

2

count It is used to count elements with a specific key.

3

equal_range It is used to get range of elements with a specific key.

Modifiers

Sr.No.

Modifiers & Description

1

emplace It is used to construct and insert element.

2

emplace_hint It is used to construct and insert element with hint.

3

insert It is used to insert elements.

4

erase It is used to erase elements.

5

clear It is used to clear content.

6

swap It is used to swap content.

Buckets

Sr.No.

Buckets & Description

1

bucket_count It returns number of buckets.

2

max_bucket_count It returns maximum number of buckets.

3

bucket_size It returns bucket size.

4

bucket It locates element’s bucket.

Hash policy

Sr.No.

Hash policy & Description

1

load_factor It returns load factor.

2

max_load_factor It is used to get or set maximum load factor.

3

rehash It is used to set number of buckets.

4

reserve It gives a request to capacity chage of backets

Observers

Sr.No.

Observers & Description

1

hash_function It is used to get hash function.

2

key_eq It is used to get key equivalence predicate.

3

get_allocator It is used to get allocator.

Non-member overloaded functions

Sr.No.

Non-member function overloads & Description

1

operator== Tests whether two maps are equal or not.

2

operator!= Tests whether two maps are equal or not.

3

swap It exchanges contents of two unordered_set containers.