STL::map mutil_map unordered_map 小结

来源:互联网 发布:js代码提取网页信息 编辑:程序博客网 时间:2024/06/06 18:03

1、STL::map

我们且看map在STL中的定义方法:


template <class Key, class T,class Compare = less<Key>, class Alloc = alloc>

第一个参数Key是关键字类型

第二个参数T是值类型

第三个参数Compare是比较函数(仿函数)

第四个参数是内存配置对象

map内部存储机制实际是以红黑树为基础,红黑树在插入节点时,必须依照大小比对之后在一个合适的位置上执行插入动作。所以作为关键字,起码必须有“<”这个比较操作符。我们知道,int,float,enum,size_t等等简单关键字,都有内置的比较函数,与map搭配无论是插入还是查找,都没什么问题。但是作为复杂数据类型,如果没有明确定义“<”比较操作符,就不能与map直接搭配使用,除非我们自己定义第三个参数。

在选择map的关键字时,注意以下两点,同时这两点也是改错的方法:

a) 关键字明确定义“<”比较操作符

b) 没有“<”比较操作符,自定义仿函数替代第三个参数Compare,该仿函数实现“()”操作符,提供比较功能。插入时各节点顺序以该仿函数为纲。

实例代码:

#include "stdafx.h"#include <map>#include <iostream> typedef std::map<char, int> Mymap; int _tmain(int argc, _TCHAR* argv[]){Mymap c1; c1.insert(Mymap::value_type('a', 1)); c1.insert(Mymap::value_type('b', 2)); c1.insert(Mymap::value_type('b', 3)); c1.insert(Mymap::value_type('f', 3)); c1.insert(Mymap::value_type('d', 4)); for (Mymap::const_iterator it = c1.begin(); it != c1.end(); ++it) std::cout << " [" << it->first << ", " << it->second << "]"; std::cout << std::endl; return (0); }
运行结果:




2、STL::mutil_map

multimap 与 map 一样,都是使用红黑树对记录型的元素数据,按元素键值的比较关系,进行快速的插入、删除和检索操作,所不同的是 multimap 允许将具有重复键值的元素插入容器。在 multimap 容器中,元素的键值与元素的映照数据的映照关系,是多对多的,因此,multimap 称为多重映照容器。multimap 与 map 之间的多重特性差异,类似于 multiset 与 set 的多重特性差异。

实例代码:

#include "stdafx.h"#include <map>#include <iostream> typedef std::multimap<char, int> Mymap; int _tmain(int argc, _TCHAR* argv[]){Mymap c1; c1.insert(Mymap::value_type('a', 1)); c1.insert(Mymap::value_type('b', 2)); c1.insert(Mymap::value_type('b', 3)); c1.insert(Mymap::value_type('f', 3)); c1.insert(Mymap::value_type('d', 4)); for (Mymap::const_iterator it = c1.begin(); it != c1.end(); ++it) std::cout << " [" << it->first << ", " << it->second << "]"; std::cout << std::endl; return (0); }
运行结果:




3、STL::unordered_map

定义:

template<class Key,                                            //The key type.    class Ty,                                                  //The mapped type.    class Hash = std::hash<Key>,                               //The hash function object type.    class Pred = std::equal_to<Key>,                           //The equality comparison function object type.    class Alloc = std::allocator<std::pair<const Key, Ty> > >  //The allocator class.    class unordered_map;
我们可以先看看官方解释:MSDN地址:http://msdn.microsoft.com/en-us/library/bb982522.aspx
The template class describes an object that controls a varying-length sequence of elements of type std::pair<const Key, Ty>. The sequence is weakly ordered by a hash function, which partitions the sequence into an ordered set of subsequences called buckets. Within each bucket a comparison function determines whether any pair of elements has equivalent ordering. Each element stores two objects, a sort key and a value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element with a number of operations that can be independent of the number of elements in the sequence (constant time), at least when all buckets are of roughly equal length. In the worst case, when all of the elements are in one bucket, the number of operations is proportional to the number of elements in the sequence (linear time). Moreover, inserting an element invalidates no iterators, and removing an element invalidates only those iterators which point at the removed element.

哈希map是一种关联容器,通过键值和映射值存储元素。允许根据键值快速检索各个元素。
在unordered_map中,键值一般用来唯一标识元素,而对应的值是一个对象关联到这个键的内容。键映射值的类型可能会有所不同。
在内部unordered_map的元素不以键值或映射的元素作任何特定的顺序排序,其存储位置取决于哈希值允许直接通过其键值为快速访问单个元素(具有恒定平均的平均时间复杂度)。
unordered_map容器比map容器更快地通过键值访问他们的单个元素,虽然unordered_map一般都是比map通过其元素的一个子集范围迭代效率低。
哈希map允许使用操作运算符(运算符[])以其键值作为参数直接访问元素。

实例如下:

#include "stdafx.h"#include <unordered_map> #include <iostream> typedef std::unordered_map<char, int> Mymap; int _tmain(int argc, _TCHAR* argv[]){Mymap c1; c1.insert(Mymap::value_type('a', 1)); c1.insert(Mymap::value_type('b', 2)); c1.insert(Mymap::value_type('b', 3)); c1.insert(Mymap::value_type('f', 3)); c1.insert(Mymap::value_type('d', 4)); for (Mymap::const_iterator it = c1.begin(); it != c1.end(); ++it) std::cout << " [" << it->first << ", " << it->second << "]"; std::cout << std::endl; return (0); }
运行结果:





0 0
原创粉丝点击