c++ std::unordered_set
std::unordered_set
template < class Key, // unordered_set::key_type/value_type
class Hash = hash<Key>, // unordered_set::hasher
class Pred = equal_to<Key>, // unordered_set::key_equal
class Alloc = allocator<Key> // unordered_set::allocator_type
> class unordered_set;
Unordered Set
Unordered sets are containers that store unique elements in no particular order, and which allow for fast retrieval of individual elements based on their value.
In an unordered_set, the value of an element is at the same time its key, that identifies it uniquely. Keys are immutable(不可变), therefore, the elements in an unordered_set cannot be modified once in the container - they can be inserted and removed, though.
Internally, the elements in the unordered_set are not sorted in any particular order, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their values (with a constant average time complexity on average).
unordered_set containers are faster than set containers to access individual elements by their key, although they are generally less efficient for range iteration through a subset of their elements.
Iterators in the container are at least forward iterators.
Container properties
- Associative: Elements in associative containers are referenced by their key and not by their absolute position in the container.
- Unordered: Unordered containers organize their elements using hash tables that allow for fast access to elements by their key.
- Set: The value of an element is also the key used to identify it.
- Unique: keys No two elements in the container can have equivalent keys.
- Allocator-aware: The container uses an allocator object to dynamically handle its storage needs.
Template parameters
- Key: Type of the elements. Each element in an unordered_set is also uniquely identified by this value. Aliased as member types unordered_set::key_type and unordered_set::value_type.
- Hash: A unary function object type that takes an object of the same type as the elements as argument and returns a unique value of type size_t based on it. This can either be a class implementing a function call operator or a pointer to a function (see constructor for an example). This defaults to hash, which returns a hash value with a probability of collision approaching 1.0/std::numeric_limits<size_t>::max(). The unordered_set object uses the hash values returned by this function to organize its elements internally, speeding up the process of locating individual elements. Aliased as member type unordered_set::hasher.
- Pred: A binary predicate that takes two arguments of the same type as the elements and returns a bool. The expression pred(a,b), where pred is an object of this type and a and b are key values, shall return true if a is to be considered equivalent to b. This can either be a class implementing a function call operator or a pointer to a function (see constructor for an example). This defaults to equal_to, which returns the same as applying the equal-to operator (a==b). The unordered_set object uses this expression to determine whether two element keys are equivalent. No two elements in an unordered_set container can have keys that yield true using this predicate. Aliased as member type unordered_set::key_equal.
- Alloc: Type of the allocator object used to define the storage allocation model. By default, the allocator class template is used, which defines the simplest memory allocation model and is value-independent. Aliased as member type unordered_set::allocator_type.
In the reference for the unordered_set member functions, these same names (Key, Hash, Pred and Alloc) are assumed for the template parameters.
Member types
The following aliases are member types of unordered_set. They are widely used as parameter and return types by member functions:
| member type | definition | notes |
|---|---|---|
| key_type | the first template parameter (Key) | |
| value_type | the first template parameter (Key) | The same as key_type |
| hasher | the second template parameter (Hash) | defaults to: hash<key_type> |
| key_equal | the third template parameter (Pred) | defaults to: equal_to<key_type> |
| allocator_type | 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 |
*Note: All iterators in an unordered_set point to const elements. Whether the const_ member type is the same type as its non-const_ counterpart depends on the particular library implementation, but programs should not rely on them being different to overload functions: const_iterator is more generic, since iterator is always convertible to it.
The same applies to local_ and non-local_ iterator types: they may either be the same type or not, but a program should not rely on them being different.
Member functions
- (constructor):Construct unordered_set (public member function )
- (destructor): Destroy unordered set (public member function)
- operator= Assign content (public member function )
Capacity
empty, size, max_size
Iterators
begin, end, cbegin, cend
Element lookup
- find: Get iterator to element (public member function)
- count: Count elements with a specific key (public member function)
- equal_range: Get range of elements with a specific key (public member function)
Modifiers
- emplace: Construct and insert element (public member function )
- emplace_hint: Construct and insert element with hint (public member function)
- insert: Insert elements (public member function )
- erase: Erase elements (public member function )
- clear: Clear content (public member function)
- swap: Swap content (public member function)
Buckets
- bucket_count: Return number of buckets (public member function)
- max_bucket_count: Return maximum number of buckets (public member function)
- bucket_size: Return bucket size (public member type)
- bucket: Locate element's bucket (public member function)
Hash policy
- load_factor: Return load factor (public member function)
- max_load_factor: Get or set maximum load factor (public member function)
- rehash: Set number of buckets (public member function )
- reserve: Request a capacity change (public member function)
Observers
- hash_function: Get hash function (public member type )
- key_eq: Get key equivalence predicate (public member type)
- get_allocator: Get allocator (public member function)
Non-member function overloads
- operators (unordered_set) Relational operators for unordered_set (function template )
- swap (unordered_set) Exchanges contents of two unordered_set containers (function template )
Code Example
#include <iostream>
#include <string>
#include <unordered_set>
using namespace std;
template<class T>
T cmerge(T a, T b){
T t(a);
t.insert(b.begin(), b.end());
return t;
}
int main(int argc, char **argv)
{
unordered_set<string> first1;
unordered_set<string> first2( {"one", "two", "three"} );
unordered_set<string> first3( {"red", "green", "blue"} );
unordered_set<string> first4( first2 );
unordered_set<string> first5( cmerge(first4,first3) );
unordered_set<string> first6( first5.begin(), first5.end() );
cout << "\nFirst6 set: ";
for(const string& x: first6 ){
cout << " " << x;
}
return 0;
/** other function please to see the unordered_map */
Reference
c++ std::unordered_set的更多相关文章
- E0443类模板 "std::unordered_set" 的参数太多
1>------ 已启动全部重新生成: 项目: QtGuiApplication20190416, 配置: Debug x64 ------1>Uic'ing QtGuiApplicati ...
- STL:unordered_set/unordered_multiset(c++11)
unordered_set:容器内的元素无序排列,基于值进行获取单个元素速度非常快.内部根据它们的 hash value 被组织成 buckets(slot). unordered_multiset: ...
- set, unordered_set模板类
template< class Key, class Hash = std::hash<Key>, class KeyEqual = std::equal_to<Key> ...
- STL标准库-容器-unordered_set
技术在于交流.沟通,本文为博主原创文章转载请注明出处并保持作品的完整性 unordered_set与与unordered_map相似,这次主要介绍unordered_set unordered_set ...
- std::hash
std::hash 由于C++11引入了哈希表数据结构std::unordered_map和std::unordered_set,所以对于基本类型也实现了标准的哈希函数std::hash,标准并没有规 ...
- [LeetCode]丑数 II&C++中priority_queue和unordered_set的使用
[LeetCode]丑数 II&C++中priority_queue和unordered_set的使用 考虑到现实因素,LeetCode每日一题不再每天都写题解了(甚至有可能掉题目?--)但对 ...
- std::hash<std::pair<int, int> >
标题是搞笑的 ! 这个问题只需要 since C++11 问题:怎么让 unordered_map 支持使用 pair 作为 key? 如果你能把两个东西压到一个基本类型里那么就不用解决这个问题了 . ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- 使用C++11的一点总结
C++11已不是新鲜技术,但对于我来说,工作中用得还不够多(前东家长时间使用gcc3.4.5,虽然去年升了4.8.2,但旧模块维护还是3.4.5居多:新东家用的是4.4.6,不能完整支持C ...
随机推荐
- bzoj4598: [Sdoi2016]模式字符串
Description 给出n个结点的树结构T,其中每一个结点上有一个字符,这里我们所说的字符只考虑大写字母A到Z,再给出长度为m 的模式串s,其中每一位仍然是A到z的大写字母.Alice希望知道,有 ...
- 20181105_线程之Task
Task是基于.net Framework3.0框架, Task使用的线程也是来自于ThreadPool 多线程的两个意义: 优化体验(常见于不卡界面), 提升运行速度(不同线程可以分担运算任务) 总 ...
- 十七 能停止的线程 暴力停止 和 interrupt/return方法
1:stop: 使用stop() 停止的线程则是非常暴力的. stop() 已经废弃了,因为: 1 如果强制停止则有可能使得一些清理工作得不到完成. 2 对锁定的对象进行了“解锁”,导致数据得不到同步 ...
- java软件设计模式——单例设计模式中的【饿汉式】与 【懒汉式】示例
以下为单例设计模式中的两种经典模式的代码示意: 单例设计模式(spring框架IOC,默认创建的对象都是单例的): 饿汉式: public class SingleClass { private Si ...
- enq:TM-contention
enq:TM-contention 2011-08-04 15:55:17 分类: Linux 7.1 enq:TM-contention 执行dml期间,为防止对与dml相关的对象进 ...
- Array 数组类
除了 Object 之外, Array 类型恐怕是 ECMAScript 中最常用的类型了.而且,ECMAScript 中的数组与其他多数语言中的数组有着相当大的区别.虽然 ECMAScript 数组 ...
- mysql 里的 ibdata1 文件不断的增长?
我们在 Percona 支持栏目经常收到关于 MySQL 的 ibdata1 文件的这个问题.当监控服务器发送一个关于 MySQL 服务器存储的报警时,恐慌就开始了 —— 就是说磁盘快要满了.一番调查 ...
- Python实践练习:生成随机的测验试卷文件
题目 假如你是一位地理老师,班上有 35 名学生,你希望进行美国各州首府的一个小测验.不妙的是,班里有几个坏蛋,你无法确信学生不会作弊.你希望随机调整问题的次序,这样每份试卷都是独一无二的,这让任何人 ...
- Django学习笔记之Class-Based-View
Django写的多了,有些问题才逐渐认识到. 比如有一个view比较复杂,调用了很多其他的函数.想要把这些函数封装起来,怎么办? 当然,可以用注释#------view------这样将函数隔离开,这 ...
- django -- url (模版语言 {% url 'test1' param1=5 param2=6 %})
如果想让form表单提交的url是类似 action="/index-5-6.html" 这样的,可以在html模版语言中使用{% url 'test1' param1=5 par ...