SGISTL源码探究-关联式容器:multimap
来源:互联网 发布:python 字符串换行 编辑:程序博客网 时间:2024/06/05 17:54
前言
在本小节中我们将分析multimap
的实现。它和map
的最大的不同之处在于,map
不允许重复的键值插入,而multimap
允许重复的键值插入,对应到底层的实现,multimap
调用的是insert_equal
进行插入操作,而map
调用的是insert_unique
进行插入操作。
另外还有一个不同之处是multimap
不支持下标运算,这个也很合理,因为可能有重复键,访问下标时可能就对应了多个值,这样进行修改时并不知道该修改哪一个。
接下来进入到multimap
的源码部分,看看它和map
的区别,顺便复习下map
容器的实现。
multimap的实现
#ifndef __STL_LIMITED_DEFAULT_TEMPLATEStemplate <class Key, class T, class Compare = less<Key>, class Alloc = alloc>#elsetemplate <class Key, class T, class Compare, class Alloc = alloc>#endifclass multimap {/* key代表键的类型 * T代表值的类型 * compare则代表大小比较的标准 * alloc则是空间配置器 */public:// typedefs: /* Key的别名为key_type * T的别名为data_type以及mapped_type * 而pair<const Key, T>的别名为value_type,这个才是插入的元素单位 * 并且注意到pair的第一个元素是const修饰的,即键不允许被改变 * Compare的别名为key_compare */ typedef Key key_type; typedef T data_type; typedef T mapped_type; typedef pair<const Key, T> value_type; typedef Compare key_compare; //仿函数 class value_compare : public binary_function<value_type, value_type, bool> { friend class multimap<Key, T, Compare, Alloc>; protected: Compare comp; value_compare(Compare c) : comp(c) {} public: bool operator()(const value_type& x, const value_type& y) const { return comp(x.first, y.first); } };private: //红黑树成员变量的定义部分 typedef rb_tree<key_type, value_type, select1st<value_type>, key_compare, Alloc> rep_type; rep_type t; // red-black tree representing multimappublic: //一些类型别名 typedef typename rep_type::pointer pointer; typedef typename rep_type::const_pointer const_pointer; typedef typename rep_type::reference reference; typedef typename rep_type::const_reference const_reference; typedef typename rep_type::iterator iterator; typedef typename rep_type::const_iterator const_iterator; typedef typename rep_type::reverse_iterator reverse_iterator; typedef typename rep_type::const_reverse_iterator const_reverse_iterator; typedef typename rep_type::size_type size_type; typedef typename rep_type::difference_type difference_type;// allocation/deallocation //构造函数 multimap() : t(Compare()) { } explicit multimap(const Compare& comp) : t(comp) { } /* 范围构造时,map采用的插入操作是insert_unique * 而在multimap中,采用的是insert_equal */#ifdef __STL_MEMBER_TEMPLATES template <class InputIterator> multimap(InputIterator first, InputIterator last) : t(Compare()) { t.insert_equal(first, last); } template <class InputIterator> multimap(InputIterator first, InputIterator last, const Compare& comp) : t(comp) { t.insert_equal(first, last); }#else multimap(const value_type* first, const value_type* last) : t(Compare()) { t.insert_equal(first, last); } multimap(const value_type* first, const value_type* last, const Compare& comp) : t(comp) { t.insert_equal(first, last); } multimap(const_iterator first, const_iterator last) : t(Compare()) { t.insert_equal(first, last); } multimap(const_iterator first, const_iterator last, const Compare& comp) : t(comp) { t.insert_equal(first, last); }#endif /* __STL_MEMBER_TEMPLATES */ //拷贝构造函数 multimap(const multimap<Key, T, Compare, Alloc>& x) : t(x.t) { } multimap<Key, T, Compare, Alloc>& operator=(const multimap<Key, T, Compare, Alloc>& x) { t = x.t; return *this; } // accessors: //一些常用的操作 key_compare key_comp() const { return t.key_comp(); } value_compare value_comp() const { return value_compare(t.key_comp()); } iterator begin() { return t.begin(); } const_iterator begin() const { return t.begin(); } iterator end() { return t.end(); } const_iterator end() const { return t.end(); } reverse_iterator rbegin() { return t.rbegin(); } const_reverse_iterator rbegin() const { return t.rbegin(); } reverse_iterator rend() { return t.rend(); } const_reverse_iterator rend() const { return t.rend(); } bool empty() const { return t.empty(); } size_type size() const { return t.size(); } size_type max_size() const { return t.max_size(); } void swap(multimap<Key, T, Compare, Alloc>& x) { t.swap(x.t); } //没有重载[]操作符,不支持下标运算 // insert/erase /* 插入操作,内部调用的是insert_equal * 而不像map,调用的是insert_unique */ iterator insert(const value_type& x) { return t.insert_equal(x); } iterator insert(iterator position, const value_type& x) { return t.insert_equal(position, x); }#ifdef __STL_MEMBER_TEMPLATES template <class InputIterator> void insert(InputIterator first, InputIterator last) { t.insert_equal(first, last); }#else void insert(const value_type* first, const value_type* last) { t.insert_equal(first, last); } void insert(const_iterator first, const_iterator last) { t.insert_equal(first, last); }#endif /* __STL_MEMBER_TEMPLATES */ //删除操作,这个和map是一样的 void erase(iterator position) { t.erase(position); } size_type erase(const key_type& x) { return t.erase(x); } void erase(iterator first, iterator last) { t.erase(first, last); } //还记得header节点没被删除吗 void clear() { t.clear(); } // multimap operations: iterator find(const key_type& x) { return t.find(x); } const_iterator find(const key_type& x) const { return t.find(x); } size_type count(const key_type& x) const { return t.count(x); } iterator lower_bound(const key_type& x) {return t.lower_bound(x); } const_iterator lower_bound(const key_type& x) const { return t.lower_bound(x); } iterator upper_bound(const key_type& x) {return t.upper_bound(x); } const_iterator upper_bound(const key_type& x) const { return t.upper_bound(x); } pair<iterator,iterator> equal_range(const key_type& x) { return t.equal_range(x); } pair<const_iterator,const_iterator> equal_range(const key_type& x) const { return t.equal_range(x); } friend bool operator== __STL_NULL_TMPL_ARGS (const multimap&, const multimap&); friend bool operator< __STL_NULL_TMPL_ARGS (const multimap&, const multimap&);};//重载操作符template <class Key, class T, class Compare, class Alloc>inline bool operator==(const multimap<Key, T, Compare, Alloc>& x, const multimap<Key, T, Compare, Alloc>& y) { return x.t == y.t;}template <class Key, class T, class Compare, class Alloc>inline bool operator<(const multimap<Key, T, Compare, Alloc>& x, const multimap<Key, T, Compare, Alloc>& y) { return x.t < y.t;}#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDERtemplate <class Key, class T, class Compare, class Alloc>inline void swap(multimap<Key, T, Compare, Alloc>& x, multimap<Key, T, Compare, Alloc>& y) { x.swap(y);}#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
例子
#include <iostream>#include <map>using namespace std;int main(){ multimap<string, int> multim; multim.insert(make_pair("123", 123)); //下面这条表达式会报错,因为multimap并没有重载[]运算符 //multim[string("123")] = 456; multim.insert(make_pair("123", 789)); map<string, int> m; multimap<string, int>::iterator ite1 = multim.begin(); multimap<string, int>::iterator ite2 = multim.end(); while(ite1 != ite2) { cout << ite1->first << " " << ite1->second << endl; ite1++; } return 0;}
小结
这里总结一下,map
和multimap
的区别有:
- map
不允许键重复,multimap
允许键重复
- map
重载了[]
运算符,支持下标运算;multimap
没有重载[]
运算符,不支持下标运算
关于multimap
就到此为止,下一小节我们将介绍hashtable
,它作为底层的结构实现了hashset
和hashmap
。
阅读全文
0 0
- SGISTL源码探究-关联式容器:multimap
- SGISTL源码探究-关联式容器:set
- SGISTL源码探究-关联式容器:map
- SGISTL源码探究-关联式容器:multiset
- SGISTL源码探究-关联式容器:hash_set
- SGISTL源码探究-关联式容器:hash_map
- SGISTL源码探究-关联式容器:hash_multiset
- SGISTL源码探究-关联式容器:hash_multimap
- SGISTL源码探究-vector容器(上)
- SGISTL源码探究-vector容器(下)
- SGISTL源码探究-list容器(上)
- SGISTL源码探究-list容器(下)
- SGISTL源码探究-deque容器(上)
- SGISTL源码探究-deque容器(下)
- SGISTL源码探究-配接器
- STL源码剖析 - 第5章 关联式容器 - multimap
- 关联式容器map/multimap
- STL容器-关联式容器map/multimap
- 别被final、finally和finalize这三兄弟给骗了,其实它们半毛钱关系也没有
- 【多线程】实例变量(synchronized)与线程安全
- SimHash+汉明距离的C#实现方法
- SpringMvc之参数绑定
- 脸盲,及其解决
- SGISTL源码探究-关联式容器:multimap
- 43. Multiply Strings
- 【第二周】项目2
- muduo网络库中buffer的设计
- epoll机制
- bzoj1051: [HAOI2006]受欢迎的牛(强联通)
- 使用Jersey框架构建RestfulWeb服务(服务端+客户端)
- macOS配置使用ODB
- 练习5: 输入一个一维数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。