看一下bits/stl_map和bits/stl_set可以看到map和set的定义如下:
84 template <typename _Key, typename _Tp, typename _Compare = std::less<_Key>,
85 typename _Alloc = std::allocator<std::pair<const _Key, _Tp> > >
86 class map
87 {
88 public:
89 typedef _Key key_type;
90 typedef _Tp mapped_type;
91 typedef std::pair<const _Key, _Tp> value_type;
92 typedef _Compare key_compare;
93 typedef _Alloc allocator_type;
94
95 private:
96 // concept requirements
97 typedef typename _Alloc::value_type _Alloc_value_type;
98 __glibcxx_class_requires(_Tp, _SGIAssignableConcept)
99 __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
100 _BinaryFunctionConcept)
101 __glibcxx_class_requires2(value_type, _Alloc_value_type, _SameTypeConcept)
102
103 public:
104 class value_compare
105 : public std::binary_function<value_type, value_type, bool>
106 {
107 friend class map<_Key, _Tp, _Compare, _Alloc>;
108 protected:
109 _Compare comp;
110
111 value_compare(_Compare __c)
112 : comp(__c) { }
113
114 public:
115 bool operator()(const value_type& __x, const value_type& __y) const
116 { return comp(__x.first, __y.first); }
117 };
118
119 private:
120 /// This turns a red-black tree into a [multi]map.
121 typedef typename _Alloc::template rebind<value_type>::other
122 _Pair_alloc_type;
123
124 typedef _Rb_tree<key_type, value_type, _Select1st<value_type>,
125 key_compare, _Pair_alloc_type> _Rep_type;
126
127 /// The actual tree structure.
128 _Rep_type _M_t;
85 template<typename _Key, typename _Compare = std::less<_Key>,
86 typename _Alloc = std::allocator<_Key> >
87 class set
88 {
89 // concept requirements
90 typedef typename _Alloc::value_type _Alloc_value_type;
91 __glibcxx_class_requires(_Key, _SGIAssignableConcept)
92 __glibcxx_class_requires4(_Compare, bool, _Key, _Key,
93 _BinaryFunctionConcept)
94 __glibcxx_class_requires2(_Key, _Alloc_value_type, _SameTypeConcept)
95
96 public:
97 // typedefs:
98 //@{
99 /// Public typedefs.
100 typedef _Key key_type;
101 typedef _Key value_type;
102 typedef _Compare key_compare;
103 typedef _Compare value_compare;
104 typedef _Alloc allocator_type;
105 //@}
106
107 private:
108 typedef typename _Alloc::template rebind<_Key>::other _Key_alloc_type;
109
110 typedef _Rb_tree<key_type, value_type, _Identity<value_type>,
111 key_compare, _Key_alloc_type> _Rep_type;
112 _Rep_type _M_t; // Red-black tree representing set.
113
更多内容请关注微信公众号"debugeeker", 链接为https://mp.weixin.qq.com/s/XGTfJaGmbVEq5d32FR4FiA