0%

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 Point {
public:
constexpr Point(double x = 0, double y = 0) noexcept : x(x), y(y) {}

constexpr double xValue() const noexcept { return x; }
constexpr double yValue() const noexcept { return y; }

constexpr void setX(double newX) noexcept { x = newX; }
constexpr void setY(double newY) noexcept { y = newY; }

private:
double x, y;
};

constexpr Point midpoint(const Point& p1, const Point& p2) noexcept {
return {(p1.xValue() + p2.xValue()) / 2, (p1.yValue() + p2.yValue()) / 2};
}

constexpr Point reflection(const Point& p) noexcept {
Point res;
res.setX(-p.xValue());
res.setY(-p.yValue());
return res;
}

constexpr Point p1(9.4, 27.7);
constexpr Point p2(28.8, 5.3);
constexpr auto mid = midpoint(p1, p2);

constexpr auto reflectedMid = reflection(mid);
Read more »

1
2
3
4
5
6
7
8
9
10
template<typename T>
void f(T&& param);

int x = 27;
const int cx = x;
const int& rx = x;
f(x); // x is lvalue, so T is int&, param's type is also int&
f(cx); // cx is lvalue, so T is const int&, param's type is also const int&
f(rx); // rx is lvalue, so T is const int&, param's type is also const int&
f(27); // 27 is rvalue, so T is int, param's type is therefore int&&
Read more »

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
struct A {
int &m;
A(int &x) : m(x) {}
};

int main() {
int x = 5;
A a(x);
cout << a.m << endl; // 5
x = 6;
cout << a.m << endl; // 6
a.m = 7;
cout << x << endl; // 7
return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class thread_guard {
public:
explicit thread_guard(thread &t) : _t(t) {}
~thread_guard() {
if (_t.joinable()) {
_t.join();
}
}
thread_guard(const thread_guard &) = delete;
thread_guard &operator=(const thread_guard &) = delete;

private:
thread &_t;
};

int main() {
for (int i = 0; i < 10; ++i) {
thread t{[](int x) { cout << x << endl; }, i};
thread_guard tg{t};
}
return 0;
}

  1. 表格如果想正确显示一定要与前面的正文隔两行

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
#include <iostream>
#include <thread>
#include <chrono>

using namespace std;
using namespace std::chrono;

struct A {
A() { cout << "A::A()" << endl; }
~A() { cout << "A::~A() " << endl; }
};

void child() {
A a;
cout << "child" << endl;
this_thread::sleep_for(chrono::seconds(3));
}

void parent() {
cout << "parent" << endl;
thread t{child};
t.detach();
// this_thread::sleep_for(chrono::seconds(1));
}

int main() {
parent();
cout << "main" << endl;
// this_thread::sleep_for(chrono::seconds(5));
return 0;
}

线程detach以后,如果进程结束(比如main函数返回)则该detached线程的资源交给系统回收,与该线程的父线程是否返回无关(除非父线程是main函数)

CName file

Github uses a file called CNAME to redirect your site. If you read the official
guide it says the file must be saved under the root directory in the master branch. But remember that for us the source branch is where the raw material is and master branch is generated when you deploy. So if you put something in the master branch it will be overwritten next time you deploy your site.

Read more »