Skip to content

Commit acc6874

Browse files
committed
add daily_bb
1 parent eed17b5 commit acc6874

File tree

4 files changed

+87
-5
lines changed

4 files changed

+87
-5
lines changed

Diff for: C++Notes/STL-source-code-notes/11-vector-上.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!-- ## STL 源码剖析之 vector
2+
3+
`vector` 是一种常用的容器,基本能够支持任何类型的对象,同时也是一个可以动态增长的数组,它的背后蕴含着怎样精妙的设计呢?话不多说,马上就来分析关于`vector`是怎么实现这些功能.
4+
5+
-->
6+
7+
template <class T, class Alloc = alloc>
8+
class vector
9+
{
10+
public:
11+
// 定义vector自身的嵌套型别
12+
typedef T value_type;
13+
typedef value_type* pointer;
14+
typedef const value_type* const_pointer;
15+
// 定义迭代器, 这里就只是一个普通的指针
16+
typedef value_type* iterator;
17+
typedef const value_type* const_iterator;
18+
typedef value_type& reference;
19+
typedef const value_type& const_reference;
20+
typedef size_t size_type;
21+
typedef ptrdiff_t difference_type;
22+
...
23+
protected:
24+
typedef simple_alloc<value_type, Alloc> data_allocator; // 设置其空间配置器
25+
iterator start; // 使用空间的头
26+
iterator finish; // 使用空间的尾
27+
iterator end_of_storage; // 可用空间的尾
28+
...
29+
};

Diff for: C++Notes/STL-source-code-notes/11-vector-上.md

-4
This file was deleted.

Diff for: C++Notes/STL-source-code-notes/tmp.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <string>
2+
3+
template <class T, class Alloc = alloc>
4+
class vector {
5+
public:
6+
vector() : start(0), finish(0), end_of_storage(0) {} // 默认构造函数
7+
explicit vector(size_type n) { fill_initialize(n, T()); } // 必须显示的调用这个构造函数, 接受一个值
8+
vector(size_type n, const T& value) { fill_initialize(n, value); } // 接受一个大小和初始化值. int和long都执行相同的函数初始化
9+
vector(int n, const T& value) { fill_initialize(n, value); }
10+
vector(long n, const T& value) { fill_initialize(n, value); }
11+
vector(const vector<T, Alloc>& x); // 接受一个vector参数的构造函数
12+
public:
13+
typedef T value_type; // 定义vector自身的嵌套型别
14+
typedef value_type* pointer;
15+
typedef const value_type* const_pointer;
16+
17+
typedef value_type* iterator; // 定义迭代器, 这里就只是一个普通的指针
18+
typedef const value_type* const_iterator;
19+
typedef value_type& reference;
20+
typedef const value_type& const_reference;
21+
typedef size_t size_type;
22+
typedef ptrdiff_t difference_type;
23+
public:
24+
iterator begin() { return start; }
25+
iterator end() { return finish; }
26+
size_type size() const { return size_type(end() - begin()); }
27+
size_type max_size() const { return size_type(-1) / sizeof(T); } // 最大能存储的元素个数
28+
size_type capacity() const { return size_type(end_of_storage - begin()); }
29+
bool empty() const { return begin() == end(); }
30+
reference operator[](size_type n) { return *(begin() + n); }
31+
reference front() { return *begin(); }
32+
reference back() { return *(end() - 1); }
33+
protected:
34+
typedef simple_alloc<value_type, Alloc> data_allocator; // 设置其空间配置器
35+
iterator start; // 表示目前使用空间的头
36+
iterator finish; // 表示目前使用空间的尾
37+
iterator end_of_storage; // 表示目前可用空间的尾
38+
...
39+
};
40+
41+
void fill_initialize(size_type n, const T& value) {
42+
start = allocate_and_fill(n, value); // 初始化并初始化值
43+
finish = start + n;
44+
end_of_storage = finish;
45+
}
46+
47+
// 调用默认的第二配置器分配内存, 分配失败就释放所分配的内存
48+
iterator allocate_and_fill(size_type n, const T& x) {
49+
iterator result = data_allocator::allocate(n); // 申请n个元素的线性空间.
50+
__STL_TRY // 对整个线性空间进行初始化, 如果有一个失败则删除全部空间并抛出异常.
51+
{
52+
uninitialized_fill_n(result, n, x);
53+
return result;
54+
}
55+
__STL_UNWIND(data_allocator::deallocate(result, n));
56+
}

Diff for: daily_bb/worth_the_world/zgnb.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@
6666

6767
32、坚持别人所不能坚持的,收获别人所不能收获的。
6868

69-
69+
33、不要停下前进的脚步,你就有永远年轻
7070

7171
<center> <img src="https://cdn.jsdelivr.net/gh/rongweihe/ImageHost01/zgnb.png" width="50%"/></center>
7272

7373
from 「帅张和他的朋友们」
7474

75+

0 commit comments

Comments
 (0)