README.md 7.3 KB
Newer Older
邹晓航 已提交
1 2
TinySTL
=======
邹晓航 已提交
3
采用C++11实现一款简易的STL标准库,既是C++STL的一个子集(裁剪了一些容器和算法)又是一个超集(增加了一些容器和算法)
邹晓航 已提交
4

邹晓航 已提交
5 6 7
目的:练习数据结构与算法和C++ Template编程

编译环境:VS2013及以上版本
邹晓航 已提交
8

邹晓航 已提交
9
##开发计划:
邹晓航 已提交
10
  * STL的几大基本组件,如string、vector、list、deque、set、map、unordered_\*
邹晓航 已提交
11 12 13 14 15 16
  * STL算法库中的大部分算法
  * circular buffer
  * bitmap
  * skip list
  * binary search tree
  * AVL tree
邹晓航 已提交
17
  * rbtree
邹晓航 已提交
18
  * 线段树
邹晓航 已提交
19 20 21 22 23 24 25 26 27 28
  * splay tree
  * rope
  * Van Emde Boas tree
  * treap
  * B-tree
  * trie
  * suffix array/tree
  * Disjoint-set data structure
  * k-d tree
  * R-tree
邹晓航 已提交
29
  * Matrix
邹晓航 已提交
30
  * Graph
邹晓航 已提交
31
  * bloom filter
邹晓航 已提交
32

邹晓航 已提交
33
##完成进度:
邹晓航 已提交
34 35 36
* STL的几大基本组件
    * type traits:100%  
    * 空间配置器:100%
邹晓航 已提交
37
    * iterator traits:100%
邹晓航 已提交
38
    * reverse_iterator:100%
邹晓航 已提交
39
    * vector:100%
邹晓航 已提交
40 41 42 43 44
    * string:100%
    * priority_queue:100%
    * stack:100%
    * deque:100%
    * queue:100%
邹晓航 已提交
45 46 47
* STL Algorithms:  
    * fill:100% 
    * fill_n:100% 
邹晓航 已提交
48
    * find:100%
邹晓航 已提交
49
    * is_heap:100%
邹晓航 已提交
50
    * min、max:100%
邹晓航 已提交
51 52 53 54
    * make_heap:100%
    * pop_heap:100%
    * push_heap:100%
    * sort_heap:100%
邹晓航 已提交
55
    * swap:100%
邹晓航 已提交
56 57 58
    * all_of:100%
    * any_of:100%
    * none_of:100%
邹晓航 已提交
59 60
    * find_if:100%
    * find_if_not:100%
邹晓航 已提交
61 62 63 64 65
    * adjacent_find:100%
* 其他组件:
    * circular_buffer:100%   
    * bitmap:100%
    * binary_search_tree:100%
邹晓航 已提交
66
    * avl_tree:100%
邹晓航 已提交
67 68 69 70

#TinySTL测试:
###测试环境:Windows 7 && VS2013 && release模式
###测试结果:
邹晓航 已提交
71
####(1):vector<int>
邹晓航 已提交
72

邹晓航 已提交
73 74
    //std::vector<int> vec;
    TinySTL::vector<int> vec;
邹晓航 已提交
75 76 77 78 79 80 81
	ProfilerInstance::start();
	int i = 0;
	for (; i != 10000; ++i){
		vec.push_back(i);
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
邹晓航 已提交
82
    
邹晓航 已提交
83 84 85 86 87 88 89 90
|container|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::vector&lt;int>|10万|2|  
|TinySTL::vector&lt;int>|100万|11|  
|TinySTL::vector&lt;int>|1000万|129|  
|std::vector&lt;int>|10万|6|  
|std::vector&lt;int>|100万|16|  
|std::vector&lt;int>|1000万|210|    
邹晓航 已提交
91
####(2):vector&lt;string>
邹晓航 已提交
92

邹晓航 已提交
93 94 95 96
    //std::vector<std::string> vec;
    TinySTL::vector<std::string> vec;
	ProfilerInstance::start();
	int i = 0;
邹晓航 已提交
97
	for (; i != 100000; ++i){
邹晓航 已提交
98 99 100 101 102
		vec.push_back(std::string("zouxiaohang"));
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
    
邹晓航 已提交
103 104 105 106 107 108 109 110
|container|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::vector&lt;string>|10万|18|  
|TinySTL::vector&lt;string>|100万|181|  
|TinySTL::vector&lt;string>|1000万|2372|  
|std::vector&lt;string>|10万|29|  
|std::vector&lt;string>|100万|232|  
|std::vector&lt;string>|1000万|1972|
邹晓航 已提交
111
####(3):circular_buffer&lt;int, N>
邹晓航 已提交
112

邹晓航 已提交
113 114 115 116 117 118 119 120 121
    TinySTL::circular_buffer<int, 10000> cb(10000, 0);
    //boost::circular_buffer<int> cb(10000, 0);
	ProfilerInstance::start();
	for (int i = 0; i != 10000000; ++i){
		cb.push_back(i);
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
    
邹晓航 已提交
122 123 124 125 126 127 128 129
|container|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::circular_buffer|1000万|75|  
|TinySTL::circular_buffer|10000万|604|  
|TinySTL::circular_buffer|100000万|5936|  
|boost::circular_buffer|1000万|22|  
|boost::circular_buffer|10000万|252|  
|boost::circular_buffer|100000万|2241|  
邹晓航 已提交
130
####(4):题目:利用bitmap找出str中未出现的字母  
邹晓航 已提交
131

邹晓航 已提交
132 133 134 135 136 137 138 139 140 141 142 143
    std::string str("abcdefghijklmnpqrstuvwxyz");
    TinySTL::bitmap<26> bm;
	for (auto it = str.cbegin(); it != str.cend(); ++it){
		bm.set(*it - 'a');
	}
	cout << bm << endl;
	cout << bm.size() << endl;
	for (int i = 0; i != 26; ++i){
		if (!bm.test(i))
			cout << "字母" << (char)('a' + i) << "没出现!!!" << endl;
	}
输出结果:  
邹晓航 已提交
144

邹晓航 已提交
145 146 147
    111111111111110111111111111000000
    32  
    字母o没出现!!!
邹晓航 已提交
148
    
邹晓航 已提交
149
####(5):string
邹晓航 已提交
150 151 152 153 154 155 156 157 158 159 160

    //std::string str;
    TinySTL::string str;
	ProfilerInstance::start();
	int i = 0;
	for (; i != 1000000; ++i){
		str.push_back('x');
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
    
邹晓航 已提交
161 162 163 164 165 166 167 168
|container|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::string|100万|7|  
|TinySTL::string|1000万|39|  
|TinySTL::string|10000万|484|  
|std::string|100万|37|  
|std::string|1000万|229|  
|std::string|10000万|1965|  
邹晓航 已提交
169

邹晓航 已提交
170 171 172 173 174 175 176 177 178 179 180 181
####(6):priority_queue&lt;int>

    //std::priority_queue<int> pq;
    TinySTL::priority_queue<int> pq;
	ProfilerInstance::start();
	int i = 0;
	for (; i != 100000; ++i){
		pq.push(i);
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
    
邹晓航 已提交
182 183 184 185 186 187 188 189
|container|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::priority_queue&lt;int>|10万|13|  
|TinySTL::priority_queue&lt;int>|100万|97|  
|TinySTL::priority_queue&lt;int>|1000万|1032|  
|std::priority_queue&lt;int>|10万|12|  
|std::priority_queue&lt;int>|100万|67|  
|std::priority_queue&lt;int>|1000万|752|  
邹晓航 已提交
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

    TinySTL::vector<int> v;
    int i = 0;
	for (; i != 100000; ++i){
		v.push_back(i);
	}
	//std::priority_queue<int> pq(v.begin(), v.end());
	TinySTL::priority_queue<int> pq(v.begin(), v.end());
	ProfilerInstance::start();
	for (i = 0; i != 100000; ++i){
		pq.pop();
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
    
邹晓航 已提交
205 206 207 208 209 210 211 212
|container|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::priority_queue&lt;int>|10万|19|  
|TinySTL::priority_queue&lt;int>|100万|137|  
|TinySTL::priority_queue&lt;int>|1000万|1532|  
|std::priority_queue&lt;int>|10万|7|  
|std::priority_queue&lt;int>|100万|92|  
|std::priority_queue&lt;int>|1000万|1214|  
邹晓航 已提交
213

邹晓航 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227
####(7):binary_search_tree&lt;int>

    TinySTL::binary_search_tree<int> bst;
    const size_t max = 10000;
	std::random_device rd;
	ProfilerInstance::start();
	size_t i = 0;
	for (; i != max; ++i){
		bst.insert(rd());
		//rd();
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
    
邹晓航 已提交
228 229 230 231 232
|container|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::binary_search_tree&lt;int>|1万|5|  
|TinySTL::binary_search_tree&lt;int>|10万|64|  
|TinySTL::binary_search_tree&lt;int>|100万|828|   
邹晓航 已提交
233 234
#######注:真实的插入时间 = 总的插入时间 - C++11随机数生成器生成随机数的总的时间

邹晓航 已提交
235
####(8):deque&lt;int>
邹晓航 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

    //std::deque<int> dq;
    TinySTL::deque<int> dq;
	ProfilerInstance::start();
	const int max = 10000000;
	int i = 0;
	for (; i != max / 2; ++i){
		dq.push_front(i);
	}
	for (; i != max; ++i){
		dq.push_back(i);
	}
	ProfilerInstance::finish();
	ProfilerInstance::dumpDuringTime();
    
|container|quantity|time(ms)|  
|---------|--------|--------|  
|TinySTL::deque&lt;int>|10万|15|  
|TinySTL::deque&lt;int>|100万|78|  
|TinySTL::deque&lt;int>|1000万|1186|  
|std::deque&lt;int>|10万|90|  
|std::deque&lt;int>|100万|1087|  
|std::deque&lt;int>|1000万|4835|  
#####ps:这个性能差距的原因1是内部实现的机制不同,我的deque是预先分配内存因此相同条件下占用的内存更多,而stl的deque是需要的时候再分配,更加节省内存;2是stl的deque实现了更多更灵活的插入删除操作,我只是实现了在头尾的插入和删除

邹晓航 已提交
261 262 263 264 265 266 267 268 269 270
####(9):avl_tree&lt;int> 
    TinySTL::binary_search_tree<int> bst;
    TinySTL::avl_tree<int> avlt;
	for (int i = 0; i != 10000; ++i){
		avlt.insert(i);
		bst.insert(i);
	}
	cout << "binary_search_tree height = " << bst.height() << endl;
	cout << "avl_tree height = " << avlt.height() << endl;
输出结果:  
邹晓航 已提交
271

邹晓航 已提交
272 273
    binary_search_tree height = 10000
    avl_tree height = 14