构造函数
unordered_set:构造一个空的unordered_set容器。unordered_set(const unordered_set& other):拷贝构造函数,构造一个unordered_set容器,并用另一个unordered_set容器的副本初始化。unordered_set(unordered_set&& other) noexcept:移动构造函数,构造一个unordered_set容器,并用另一个unordered_set容器的元素初始化,另一个容器的内容将被移动。unordered_set(size_type count):构造一个包含count个元素的unordered_set容器。unordered_set(size_type count, const key_type& key, const hasher& hash_fn = hasher(), const key_equal& equal_fn = key_equal()):构造一个包含count个重复的key元素的unordered_set容器。template< class InputIt > unordered_set(InputIt first, InputIt last, size_type count = 0, const hasher& hash_fn = hasher(), const key_equal& equal_fn = key_equal()):构造一个包含范围在[first, last)之间的元素的unordered_set容器。
迭代器
begin():返回一个指向unordered_set容器第一个元素的迭代器。end():返回一个指向unordered_set容器尾元素下一个位置的迭代器。cbegin() const:返回一个指向unordered_set容器第一个元素的const迭代器。cend() const:返回一个指向unordered_set容器尾元素下一个位置的const迭代器。
容量
empty() const:返回一个bool值,指示unordered_set容器是否为空。size() const:返回unordered_set容器中元素的数量。max_size() const:返回unordered_set容器可以容纳的最大元素数。
修改器
insert(const value_type& value):将元素value插入unordered_set容器中。insert(value_type&& value):将元素value插入unordered_set容器中,使用移动语义。insert(InputIt first, InputIt last):插入来自范围[first, last)的元素到unordered_set容器中。insert(std::initializer_list<value_type> ilist):从initializer_list中插入元素到unordered_set容器中。emplace(Args&&... args):通过参数args构造元素并将其插入到unordered_set容器中。erase(const_iterator position):从unordered_set容器中删除迭代器position所指向的元素。erase(const key_type& key):从unordered_set容器中删除所有等于key的元素。erase(first, last):从unordered_set容器中删除范围[first, last)的元素。clear():从unordered_set容器中删除所有元素。
查找
find(const key_type& key):返回指向键值为key的元素的迭代器。如果未找到key,则返回end迭代器。count(const key_type& key):统计键为key的元素个数,返回值为0或1,因为该容器不允许有重复元素。equal_range(const key_type& key):返回键值等于key的元素范围,返回值为一个pair,其两个成员都是迭代器类型,表示范围的起始位置和结束位置。
本文详细介绍了C++标准库中的unordered_set容器的各种构造方法,包括空构造、拷贝构造、移动构造等。此外,还涵盖了迭代器的使用,如begin()和end(),以及查询容器状态的方法,如empty()和size()。文章还讨论了插入、删除和查找元素的关键函数,如insert、erase和find等。
1万+

被折叠的 条评论
为什么被折叠?



