Skip to content

Commit c8e7b7a

Browse files
committed
Upload note code of chapter 5 associated container
- RB_tree, set, map, multiset, multimap
1 parent 55e3171 commit c8e7b7a

24 files changed

+2803
-0
lines changed

5_STL_associated_container/5_2_3_stl_tree.h

Lines changed: 1435 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// file: 5rbtree-test.cpp
2+
#include <iostream>
3+
using namespace std;
4+
5+
int main() {
6+
rb_tree<int, int, identity<int>, less<int>> itree;
7+
cout << itree.size() << endl;
8+
9+
itree.insert_unique(10);
10+
itree.insert_unique(7);
11+
itree.insert_unique(8);
12+
13+
itree.insert_unique(15);
14+
itree.insert_unique(5);
15+
itree.insert_unique(6);
16+
17+
itree.insert_unique(11);
18+
19+
itree.insert_unique(13);
20+
itree.insert_unique(12);
21+
22+
cout << itree.size() << endl;
23+
rb_tree<int, int, identity<int>, less<int>>::iterator ite1 = itree.begin();
24+
rb_tree<int, int, identity<int>, less<int>>::iterator ite2 = itree.end();
25+
for (; ite1 != ite2; ++ite1)
26+
cout << *ite1 << ' ';
27+
cout << endl;
28+
29+
__rb_tree_base_iterator rbtite;
30+
for (; ite1 != ite2; ++ite1) {
31+
rbtite = __rb_tree_base_iterator(ite1);
32+
cout << *ite1 << "(" << rbtite.node->color << ")";
33+
}
34+
cout << endl;
35+
36+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// file: 5set-test.cpp
2+
3+
#include <set>
4+
#include <iostream>
5+
#include <algorithm>
6+
7+
using namespace std;
8+
9+
int main() {
10+
int i;
11+
int ia[5] = {0, 1, 2, 3, 4};
12+
set<int> iset{ia, ia + 5};
13+
14+
cout << "size=" << iset.size() << endl;
15+
cout << "3 count =" << iset.count(3) << endl;
16+
iset.insert(3);
17+
cout << "size=" << iset.size() << endl;
18+
cout << "3 count =" << iset.count(3) << endl;
19+
20+
iset.insert(5);
21+
cout << "size=" << iset.size() << endl;
22+
cout << "3 count =" << iset.count(3) << endl;
23+
24+
iset.erase(1);
25+
cout << "size=" << iset.size() << endl;
26+
cout << "3 count =" << iset.count(3) << endl;
27+
cout << "1 count =" << iset.count(3) << endl;
28+
29+
set<int>::iterator ite1 = iset.begin();
30+
set<int>::iterator ite2 = iset.end();
31+
for (; ite1 != ite2; ++ite1) {
32+
cout << *ite1;
33+
}
34+
cout << endl;
35+
36+
// 使用STL算法find可以搜索元素,但不推荐
37+
ite1 = find(iset.begin(), iset.end(), 3);
38+
if (ite1 != iset.end())
39+
cout << "3 found" << endl;
40+
41+
ite1 = find(iset.begin(), iset.end(), 1);
42+
if (ite1 == iset.end())
43+
cout << "1 not found" << endl;
44+
45+
// 关联式容器应使用专用的find函数搜索更有效率
46+
ite1 = iset.find(3);
47+
if (ite1 != iset.end())
48+
cout << "3 found" << endl;
49+
50+
ite1 = iset.find(1);
51+
if (ite1 == iset.end())
52+
cout << "1 not found" << endl;
53+
54+
// *ite1 = 9; // 修改失败
55+
}
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
/*
2+
*
3+
* Copyright (c) 1994
4+
* Hewlett-Packard Company
5+
*
6+
* Permission to use, copy, modify, distribute and sell this software
7+
* and its documentation for any purpose is hereby granted without fee,
8+
* provided that the above copyright notice appear in all copies and
9+
* that both that copyright notice and this permission notice appear
10+
* in supporting documentation. Hewlett-Packard Company makes no
11+
* representations about the suitability of this software for any
12+
* purpose. It is provided "as is" without express or implied warranty.
13+
*
14+
*
15+
* Copyright (c) 1996,1997
16+
* Silicon Graphics Computer Systems, Inc.
17+
*
18+
* Permission to use, copy, modify, distribute and sell this software
19+
* and its documentation for any purpose is hereby granted without fee,
20+
* provided that the above copyright notice appear in all copies and
21+
* that both that copyright notice and this permission notice appear
22+
* in supporting documentation. Silicon Graphics makes no
23+
* representations about the suitability of this software for any
24+
* purpose. It is provided "as is" without express or implied warranty.
25+
*/
26+
27+
/* NOTE: This is an internal header file, included by other STL headers.
28+
* You should not attempt to use it directly.
29+
*/
30+
31+
#ifndef __SGI_STL_INTERNAL_SET_H
32+
#define __SGI_STL_INTERNAL_SET_H
33+
34+
#include <concept_checks.h>
35+
36+
__STL_BEGIN_NAMESPACE
37+
38+
#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
39+
#pragma set woff 1174
40+
#pragma set woff 1375
41+
#endif
42+
43+
// Forward declarations of operators < and ==, needed for friend declaration.
44+
45+
template <class _Key, class _Compare __STL_DEPENDENT_DEFAULT_TMPL(less<_Key>),
46+
class _Alloc = __STL_DEFAULT_ALLOCATOR(_Key) >
47+
class set;
48+
49+
template <class _Key, class _Compare, class _Alloc>
50+
inline bool operator==(const set<_Key,_Compare,_Alloc>& __x,
51+
const set<_Key,_Compare,_Alloc>& __y);
52+
53+
template <class _Key, class _Compare, class _Alloc>
54+
inline bool operator<(const set<_Key,_Compare,_Alloc>& __x,
55+
const set<_Key,_Compare,_Alloc>& __y);
56+
57+
58+
// Set默认递增排序
59+
template <class _Key, class _Compare, class _Alloc>
60+
class set {
61+
// requirements:
62+
63+
__STL_CLASS_REQUIRES(_Key, _Assignable);
64+
__STL_CLASS_BINARY_FUNCTION_CHECK(_Compare, bool, _Key, _Key);
65+
66+
public:
67+
// typedefs:
68+
69+
typedef _Key key_type;
70+
typedef _Key value_type;
71+
// key_compare与value_compare使用同一个函数
72+
typedef _Compare key_compare;
73+
typedef _Compare value_compare;
74+
private:
75+
typedef _Rb_tree<key_type, value_type,
76+
_Identity<value_type>, key_compare, _Alloc> _Rep_type;
77+
// 使用红黑树表现set
78+
_Rep_type _M_t; // red-black tree representing set
79+
public:
80+
typedef typename _Rep_type::const_pointer pointer;
81+
typedef typename _Rep_type::const_pointer const_pointer;
82+
typedef typename _Rep_type::const_reference reference;
83+
typedef typename _Rep_type::const_reference const_reference;
84+
// iterator定义为const,不允许写入操作
85+
typedef typename _Rep_type::const_iterator iterator;
86+
typedef typename _Rep_type::const_iterator const_iterator;
87+
typedef typename _Rep_type::const_reverse_iterator reverse_iterator;
88+
typedef typename _Rep_type::const_reverse_iterator const_reverse_iterator;
89+
typedef typename _Rep_type::size_type size_type;
90+
typedef typename _Rep_type::difference_type difference_type;
91+
typedef typename _Rep_type::allocator_type allocator_type;
92+
93+
// allocation/deallocation
94+
// set使用红黑树的insert_unique,不允许重复键值
95+
set() : _M_t(_Compare(), allocator_type()) {}
96+
explicit set(const _Compare& __comp,
97+
const allocator_type& __a = allocator_type())
98+
: _M_t(__comp, __a) {}
99+
100+
#ifdef __STL_MEMBER_TEMPLATES
101+
template <class _InputIterator>
102+
set(_InputIterator __first, _InputIterator __last)
103+
: _M_t(_Compare(), allocator_type())
104+
{ _M_t.insert_unique(__first, __last); }
105+
106+
template <class _InputIterator>
107+
set(_InputIterator __first, _InputIterator __last, const _Compare& __comp,
108+
const allocator_type& __a = allocator_type())
109+
: _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
110+
#else
111+
set(const value_type* __first, const value_type* __last)
112+
: _M_t(_Compare(), allocator_type())
113+
{ _M_t.insert_unique(__first, __last); }
114+
115+
set(const value_type* __first,
116+
const value_type* __last, const _Compare& __comp,
117+
const allocator_type& __a = allocator_type())
118+
: _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
119+
120+
set(const_iterator __first, const_iterator __last)
121+
: _M_t(_Compare(), allocator_type())
122+
{ _M_t.insert_unique(__first, __last); }
123+
124+
set(const_iterator __first, const_iterator __last, const _Compare& __comp,
125+
const allocator_type& __a = allocator_type())
126+
: _M_t(__comp, __a) { _M_t.insert_unique(__first, __last); }
127+
#endif /* __STL_MEMBER_TEMPLATES */
128+
129+
set(const set<_Key,_Compare,_Alloc>& __x) : _M_t(__x._M_t) {}
130+
set<_Key,_Compare,_Alloc>& operator=(const set<_Key, _Compare, _Alloc>& __x)
131+
{
132+
_M_t = __x._M_t;
133+
return *this;
134+
}
135+
136+
// accessors:
137+
// 调用红黑树实现
138+
key_compare key_comp() const { return _M_t.key_comp(); }
139+
// value_compare一样调用key_compare
140+
value_compare value_comp() const { return _M_t.key_comp(); }
141+
allocator_type get_allocator() const { return _M_t.get_allocator(); }
142+
143+
iterator begin() const { return _M_t.begin(); }
144+
iterator end() const { return _M_t.end(); }
145+
reverse_iterator rbegin() const { return _M_t.rbegin(); }
146+
reverse_iterator rend() const { return _M_t.rend(); }
147+
bool empty() const { return _M_t.empty(); }
148+
size_type size() const { return _M_t.size(); }
149+
size_type max_size() const { return _M_t.max_size(); }
150+
void swap(set<_Key,_Compare,_Alloc>& __x) { _M_t.swap(__x._M_t); }
151+
152+
// insert/erase
153+
pair<iterator,bool> insert(const value_type& __x) {
154+
pair<typename _Rep_type::iterator, bool> __p = _M_t.insert_unique(__x);
155+
return pair<iterator, bool>(__p.first, __p.second);
156+
}
157+
iterator insert(iterator __position, const value_type& __x) {
158+
typedef typename _Rep_type::iterator _Rep_iterator;
159+
return _M_t.insert_unique((_Rep_iterator&)__position, __x);
160+
}
161+
#ifdef __STL_MEMBER_TEMPLATES
162+
template <class _InputIterator>
163+
void insert(_InputIterator __first, _InputIterator __last) {
164+
_M_t.insert_unique(__first, __last);
165+
}
166+
#else
167+
void insert(const_iterator __first, const_iterator __last) {
168+
_M_t.insert_unique(__first, __last);
169+
}
170+
void insert(const value_type* __first, const value_type* __last) {
171+
_M_t.insert_unique(__first, __last);
172+
}
173+
#endif /* __STL_MEMBER_TEMPLATES */
174+
void erase(iterator __position) {
175+
typedef typename _Rep_type::iterator _Rep_iterator;
176+
_M_t.erase((_Rep_iterator&)__position);
177+
}
178+
size_type erase(const key_type& __x) {
179+
return _M_t.erase(__x);
180+
}
181+
void erase(iterator __first, iterator __last) {
182+
typedef typename _Rep_type::iterator _Rep_iterator;
183+
_M_t.erase((_Rep_iterator&)__first, (_Rep_iterator&)__last);
184+
}
185+
void clear() { _M_t.clear(); }
186+
187+
// set operations:
188+
189+
iterator find(const key_type& __x) const { return _M_t.find(__x); }
190+
size_type count(const key_type& __x) const {
191+
return _M_t.find(__x) == _M_t.end() ? 0 : 1;
192+
}
193+
iterator lower_bound(const key_type& __x) const {
194+
return _M_t.lower_bound(__x);
195+
}
196+
iterator upper_bound(const key_type& __x) const {
197+
return _M_t.upper_bound(__x);
198+
}
199+
pair<iterator,iterator> equal_range(const key_type& __x) const {
200+
return _M_t.equal_range(__x);
201+
}
202+
203+
#ifdef __STL_TEMPLATE_FRIENDS
204+
template <class _K1, class _C1, class _A1>
205+
friend bool operator== (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);
206+
template <class _K1, class _C1, class _A1>
207+
friend bool operator< (const set<_K1,_C1,_A1>&, const set<_K1,_C1,_A1>&);
208+
#else /* __STL_TEMPLATE_FRIENDS */
209+
friend bool __STD_QUALIFIER
210+
// __STL_NULL_TMPL_ARGS定义为<>
211+
operator== __STL_NULL_TMPL_ARGS (const set&, const set&);
212+
friend bool __STD_QUALIFIER
213+
operator< __STL_NULL_TMPL_ARGS (const set&, const set&);
214+
#endif /* __STL_TEMPLATE_FRIENDS */
215+
};
216+
217+
template <class _Key, class _Compare, class _Alloc>
218+
inline bool operator==(const set<_Key,_Compare,_Alloc>& __x,
219+
const set<_Key,_Compare,_Alloc>& __y) {
220+
return __x._M_t == __y._M_t;
221+
}
222+
223+
template <class _Key, class _Compare, class _Alloc>
224+
inline bool operator<(const set<_Key,_Compare,_Alloc>& __x,
225+
const set<_Key,_Compare,_Alloc>& __y) {
226+
return __x._M_t < __y._M_t;
227+
}
228+
229+
#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
230+
231+
template <class _Key, class _Compare, class _Alloc>
232+
inline bool operator!=(const set<_Key,_Compare,_Alloc>& __x,
233+
const set<_Key,_Compare,_Alloc>& __y) {
234+
return !(__x == __y);
235+
}
236+
237+
template <class _Key, class _Compare, class _Alloc>
238+
inline bool operator>(const set<_Key,_Compare,_Alloc>& __x,
239+
const set<_Key,_Compare,_Alloc>& __y) {
240+
return __y < __x;
241+
}
242+
243+
template <class _Key, class _Compare, class _Alloc>
244+
inline bool operator<=(const set<_Key,_Compare,_Alloc>& __x,
245+
const set<_Key,_Compare,_Alloc>& __y) {
246+
return !(__y < __x);
247+
}
248+
249+
template <class _Key, class _Compare, class _Alloc>
250+
inline bool operator>=(const set<_Key,_Compare,_Alloc>& __x,
251+
const set<_Key,_Compare,_Alloc>& __y) {
252+
return !(__x < __y);
253+
}
254+
255+
template <class _Key, class _Compare, class _Alloc>
256+
inline void swap(set<_Key,_Compare,_Alloc>& __x,
257+
set<_Key,_Compare,_Alloc>& __y) {
258+
__x.swap(__y);
259+
}
260+
261+
#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
262+
263+
#if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32)
264+
#pragma reset woff 1174
265+
#pragma reset woff 1375
266+
#endif
267+
268+
__STL_END_NAMESPACE
269+
270+
#endif /* __SGI_STL_INTERNAL_SET_H */
271+
272+
// Local Variables:
273+
// mode:C++
274+
// End:

0 commit comments

Comments
 (0)