undefined

类模板和模板类

类模板

类模板:允许用户为类定义一种模式,使得类中的某些数据成员、默认成员函数的参数、某些成员函数的返回值,能够取任意类型(包括系统预定义和用户自定义)。如果一个类中数据成员的数据类型不能确定,或者是某个成员函数的参数或返回值的类型不能确定,就必须将此类声明为模板,它的存在不是代表一个具体的、实际的类,而是代表一类 类

1
2
3
4
5
6
7
8
9
10
11
12
13
template <class T>
class foo {
private:
T n;
const T i;
static T cnt;
public:
Test(): i(0) {}
Test(T k);
~Test(){}
void print();
T operator+(T x);
};

查看更多

undefined

基本概念

1
2
3
4
5
C++标准里一个相关概念是自由存储区(free store),特指使用new 和 delete 来分配和释放内存的区域。一般而言,这是堆的一个子集。
new 和 delete 操作的区域是 free store
malloc 和 free 操作的区域是 heap

RAII:Resource Acquisition Is Initialization. 是C++所特有的资源管理方式。

栈展开

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
编译器会自动调用析构函数,包括在函数执行发生异常的情况。在发送异常时对析构函数的调用,专门的术语叫“栈展开”。如下面代码:

class Obj {
public:
Obj() { puts("Obj()"); }
~Obj() { puts("~Obj()"); }
};

void foo(int n) {
Obj obj;
if (n == 42)
throw "life, the universe and everything";
}

int main() {
try {
foo(41);
foo(41);
} catch (const char* s) {
puts(s);
}
}
执行代码的结果是:
Obj()
~Obj()
Obj()
~Obj()
life, the universe and everything

不管是否发送了异常,obj 的析构函数都会得到执行

查看更多

undefined

自动类型推导

1. auto

自动类型推导,C++14开始,还有函数的返回类型。auto 并没有改变C++是静态语言这一事实,使用auto 的变量(或函数返回值)的类型仍然是编译时就确定了,只不过编译器能自动帮你填充而已。

auto 实际使用的规则类似于函数模板参数的推导规则。当你写了一个含有auto的表达式时,相当于把 auto 替换为模板参数的结果。如下:

  1. auto a = expr; 意味着用 expr 去匹配一个假想的 template<typename T> f(T) 函数模板,结果为值类型
  2. const auto& a = expr; 意味着用 expr 去匹配一个假想的 template<typename T> f(const T&) 函数模板,结果为常左值引用类型
查看更多

undefined

先看看概念

实现一个数入行迭代器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <string>
#include <iostream>

using namespace std;

class istream_line_reader {
public:
class iterator { // 实现 InputIterator
public:
typedef ptrdiff_t difference_type;
typedef string value_type;
typedef const value_type* pointer;
typedef const value_type& reference;
typedef input_iterator_tag iterator_category;

iterator() noexcept : stream_(nullptr) {}

explicit iterator(istream& is) : stream_(&is) {
++*this;
}

reference operator*() const noexcept {
return line_;
}

pointer operator->() const noexcept {
return &line_;
}

iterator& operator++() {
getline(*stream_, line_);
if (!*stream_) {
stream_ = nullptr;
}
return *this;
}

iterator operator++(int) {
iterator temp(*this);
++*this;
return temp;
}

bool operator==(const iterator& rhs) const noexcept {
return stream_ == rhs.stream_;
}

bool operator!=(const iterator& rhs) const noexcept {
return !operator==(rhs);
}

private:
istream* stream_;
string line_;
};

istream_line_reader() noexcept : stream_(nullptr) {}

explicit istream_line_reader(istream& is) noexcept : stream_(&is) {}

iterator begin() {
return iterator(*stream_);
}

iterator end() const noexcept {
return iterator();
}


private:
istream* stream_;
};

int main() {
for (const string& line : istream_line_reader(cin)) {
cout << line << endl;
}}

undefined

HTTP2 的特性

一、头部压缩

由于报文 Header 一般会携带”User Agent“、”Cookie“、”Accept“、”Server“ 等许多固定的头字段,多达几百字节甚至上千字节,但 Body 却经常只有几十字节(比如 Get 请求、204/301/304响应),而且很多的请求响应报文中有很多字段值都是重复的,非常浪费。因此压缩头部报文是应该的

HTTP2 开发了专门的“HPACK” 算法,专门为压缩 HTTP 头部定制的算法,与 gzip、zlib 等压缩算法不同,它是一个“有状态”的算法,需要客户端和服务器各自维护一份“索引表”,也可以说是“字典”(这有点类似 brotli),压缩和解压缩就是查表和更新表的操作。在客户端和服务器两端建立“字典”,用索引号表示重复的字符串,还采用哈夫曼编码来压缩整数和字符串,可以达到 50%-90% 的压缩率。

为了方便管理和压缩,HTTP/2 废除了原有的起始行概念,把起始行里面的请求方法、URI、状态码等统一转换成了头字段的形式,并且给这些“不是头字段的头字段”起了个特别的名字——“伪头字段”(pseudo-header fields)。而起始行里的版本号和错误原因短语因为没什么大用,顺便也给废除了。为了与“真头字段”区分开来,这些“伪头字段”会在名字前加一个“:”,比如“:authority” “:method” “:status”,分别表示的是域名、请求方法和状态码。那么 HTTP2 报文就简单了,全都是 K-V 形式的字段,于是 HTTP/2 就为一些最常用的头字段定义了一个只读的“静态表”。

查看更多

undefined

c++11中 std::unique_lock :https://blog.csdn.net/fengbingchun/article/details/78638138

c++11中 std::condition_variable : https://blog.csdn.net/fengbingchun/article/details/73695596

c++11 中 std::lock_guard : https://blog.csdn.net/fengbingchun/article/details/78649260

std::lock_guard 和 std::unique_lock 区别:https://chhy2009.github.io/2018/09/23/c11concurrency/c11concurrency/

std::all_of :https://vimsky.com/examples/usage/stdall_of-in-cpp.html

查看更多