标签:
unordered_map和map类似,都是存储的key-value的值,可以通过key快速索引到value。
不同的是unordered_map不会根据key的大小进行排序,存储时是根据key的hash值判断元素是否相同,即unordered_map内部元素是无序的,而map中的元素是按照二叉搜索树存储,进行中序遍历会得到有序遍历。
所 以使用时map的key需要定义operator<。而unordered_map需要定义hash_value函数并且重载 operator==。但是很多系统内置的数据类型都自带这些,那么如果是自定义类型,那么就需要自己重载operator<或者 hash_value()了。
结论:如果需要内部元素自动排序,使用map,不需要排序使用unordered_map
C++0X为什么不把unordered_map定义为hash_map呢?那是因为在新标准出现之前很多库厂商已经暂用了hash_map这个名词。因此为了向前兼容不得不定义新的unordered_map。
函数原型
template < class Key, // unordered_map::key_type
class T, // unordered_map::mapped_type
class Hash = hash<Key>, // unordered_map::hasher
class Pred = equal_to<Key>, // unordered_map::key_equal
class Alloc = allocator< pair<const Key,T> > // unordered_map::allocator_type
>
class unordered_map;
Key表示建的类型
T表示键映射到的hash值的类型
Hash是一个接受一个参数且类型要与Key兼容的函数对象。返回值为T型。注意,参数是const引用, 函数是const
Pred是一个接受两个参数且其类型与Key兼容的函数对象。返回值为BOOL型。注意,参数是const引用, 函数是const
成员函数:
Member functions
- (constructor)
- Construct unordered_map (public member function )
- (destructor)
- Destroy unordered map (public member function)
- operator=
- Assign content (public member function )
Capacity
- empty
- Test whether container is empty (public member function)
- size
- Return container size (public member function)
- max_size
- Return maximum size (public member function)
Iterators
- begin
- Return iterator to beginning (public member function)
- end
- Return iterator to end (public member function)
- cbegin
- Return const_iterator to beginning (public member function)
- cend
- Return const_iterator to end (public member function)
Element access
- operator[]
- Access element (public member function )
- at
- Access element (public member function)
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 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)
C++11中新特性之:unordered_map
标签:
原文地址:http://www.cnblogs.com/lysuns/p/4324535.html