Skip to content

Commit 838d30e

Browse files
committed
revision #1: 更新第二章中已维护的代码
1 parent 11efa38 commit 838d30e

28 files changed

+460
-111
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@
2727
*.exe
2828
*.out
2929
*.app
30+
31+
node_modules
32+
website/public

code/1/1.1.cpp renamed to code/1/1.1.c.and.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// 1.1.cpp
33
//
44
// chapter 1 introduction
5-
// c++1x tutorial
5+
// modern cpp tutorial
66
//
77
// created by changkun at changkun.de
88
//
@@ -12,7 +12,7 @@
1212
#include <functional>
1313

1414
int main() {
15-
// 使用 lambda 表达式
15+
// use lambda expression
1616
[out = std::ref(std::cout << "Result from C code: " << add(1, 2))](){
1717
out.get() << ".\n";
1818
}();

code/1/Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
# 1.1.cpp
33
#
44
# chapter 1 introduction
5-
# c++1x tutorial
5+
# modern cpp tutorial
66
#
77
# created by changkun at changkun.de
88
#
99

1010
C = gcc
11-
CXX = g++
11+
CXX = clang++
1212

1313
SOURCE_C = foo.c
1414
OBJECTS_C = foo.o
1515

16-
SOURCE_CXX = 1.1.cpp
16+
SOURCE_CXX = 1.1.c.and.cpp
1717

18-
TARGET = 1.1
19-
LDFLAGS_COMMON = -std=c++1z
18+
TARGET = 1.1.out
19+
LDFLAGS_COMMON = -std=c++17
2020

2121
all:
2222
$(C) -c $(SOURCE_C)

code/1/foo.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
// foo.c
33
//
44
// chapter 1 introduction
5-
// c++1x tutorial
5+
// modern cpp tutorial
66
//
77
// created by changkun at changkun.de
88
//
99

1010
#include "foo.h"
1111

12-
// C 语言代码
12+
// C code
1313
int add(int x, int y) {
1414
return x+y;
1515
}

code/1/foo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// foo.h
33
//
44
// chapter 1 introduction
5-
// c++1x tutorial
5+
// modern cpp tutorial
66
//
77
// created by changkun at changkun.de
88
//

code/2/2.1.cpp

Lines changed: 0 additions & 32 deletions
This file was deleted.

code/2/2.1.nullptr.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// 2.1.nullptr.cpp
3+
// chapter 2 language usability
4+
// modern cpp tutorial
5+
//
6+
// created by changkun at changkun.de
7+
//
8+
9+
#include <iostream>
10+
#include <type_traits>
11+
12+
void foo(char *);
13+
void foo(int);
14+
15+
int main() {
16+
if (std::is_same<decltype(NULL), decltype(0)>::value)
17+
std::cout << "NULL == 0" << std::endl;
18+
if (std::is_same<decltype(NULL), decltype((void*)0)>::value)
19+
std::cout << "NULL == (void *)0" << std::endl;
20+
if (std::is_same<decltype(NULL), std::nullptr_t>::value)
21+
std::cout << "NULL == nullptr" << std::endl;
22+
23+
foo(0); // will call foo(int)
24+
// foo(NULL); // doen't compile
25+
foo(nullptr); // will call foo(char*)
26+
return 0;
27+
}
28+
29+
void foo(char *) {
30+
std::cout << "foo(char*) is called" << std::endl;
31+
}
32+
void foo(int i) {
33+
std::cout << "foo(int) is called" << std::endl;
34+
}

code/2/2.10.if.constexpr.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// 2.10.if.constexpr.cpp
3+
// chapter 2 language usability
4+
// modern cpp tutorial
5+
//
6+
// created by changkun at changkun.de
7+
//
8+
9+
#include <iostream>
10+
11+
template<typename T>
12+
auto print_type_info(const T& t) {
13+
if constexpr (std::is_integral<T>::value) {
14+
return t + 1;
15+
} else {
16+
return t + 0.001;
17+
}
18+
}
19+
20+
// at compiling time
21+
// int print_type_info(const int& t) {
22+
// return t + 1;
23+
// }
24+
// double print_type_info(const double& t) {
25+
// return t + 0.001;
26+
// }
27+
28+
int main() {
29+
std::cout << print_type_info(5) << std::endl;
30+
std::cout << print_type_info(3.14) << std::endl;
31+
}

code/2/2.11.for.loop.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// 2.11.for.loop.cpp
3+
// chapter 2 language usability
4+
// modern cpp tutorial
5+
//
6+
// created by changkun at changkun.de
7+
//
8+
9+
#include <iostream>
10+
#include <vector>
11+
#include <algorithm>
12+
13+
int main() {
14+
std::vector<int> vec = {1, 2, 3, 4};
15+
if (auto itr = std::find(vec.begin(), vec.end(), 3); itr != vec.end()) *itr = 4;
16+
for (auto element : vec)
17+
std::cout << element << std::endl; // read only
18+
for (auto &element : vec) {
19+
element += 1; // writeable
20+
}
21+
for (auto element : vec)
22+
std::cout << element << std::endl; // read only
23+
}

code/2/2.2.constexpr.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//
2+
// 2.2.constexpr.cpp
3+
// chapter 2 language usability
4+
// modern cpp tutorial
5+
//
6+
// created by changkun at changkun.de
7+
//
8+
9+
#include <iostream>
10+
#define LEN 10
11+
12+
int len_foo() {
13+
int i = 2;
14+
return i;
15+
}
16+
constexpr int len_foo_constexpr() {
17+
return 5;
18+
}
19+
20+
// error in c++11
21+
// constexpr int fibonacci(const int n) {
22+
// if(n == 1) return 1;
23+
// if(n == 2) return 1;
24+
// return fibonacci(n-1) + fibonacci(n-2);
25+
// }
26+
27+
// ok
28+
constexpr int fibonacci(const int n) {
29+
return n == 1 || n == 2 ? 1 : fibonacci(n-1) + fibonacci(n-2);
30+
}
31+
32+
33+
int main() {
34+
char arr_1[10]; // legal
35+
char arr_2[LEN]; // legal
36+
37+
int len = 10;
38+
// char arr_3[len]; // illegal
39+
40+
const int len_2 = len + 1;
41+
char arr_4[len_2]; // legal
42+
43+
// char arr_5[len_foo()+5]; // illegal
44+
char arr_6[len_foo_constexpr() + 1]; // legal
45+
46+
// 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
47+
std::cout << fibonacci(10) << std::endl;
48+
49+
return 0;
50+
}

code/2/2.2.cpp

Lines changed: 0 additions & 34 deletions
This file was deleted.

code/2/2.3.if.switch.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// 2.3.if.switch.cpp
3+
// chapter 2 language usability
4+
// modern cpp tutorial
5+
//
6+
// created by changkun at changkun.de
7+
//
8+
9+
#include <iostream>
10+
#include <vector>
11+
#include <algorithm>
12+
13+
int main() {
14+
std::vector<int> vec = {1, 2, 3, 4};
15+
16+
// before c++17, can be simplefied by using `auto`
17+
const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 2);
18+
if (itr != vec.end()) {
19+
*itr = 3;
20+
}
21+
22+
// after c++17, can be simplefied by using `auto`
23+
if (const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 3);
24+
itr != vec.end()) {
25+
*itr = 4;
26+
}
27+
28+
// should output: 1, 4, 3, 4. can be simplefied using `auto`
29+
for (std::vector<int>::iterator element = vec.begin(); element != vec.end(); ++element)
30+
std::cout << *element << std::endl;
31+
}

code/2/2.4.initializer.list.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// 2.4.initializer.list.cpp
3+
// chapter 2 language usability
4+
// modern cpp tutorial
5+
//
6+
// created by changkun at changkun.de
7+
//
8+
9+
#include <iostream>
10+
#include <initializer_list>
11+
#include <vector>
12+
13+
class Foo {
14+
public:
15+
int value_a;
16+
int value_b;
17+
Foo(int a, int b) : value_a(a), value_b(b) {}
18+
};
19+
20+
class MagicFoo {
21+
public:
22+
std::vector<int> vec;
23+
MagicFoo(std::initializer_list<int> list) {
24+
for (std::initializer_list<int>::iterator it = list.begin(); it != list.end(); ++it) {
25+
vec.push_back(*it);
26+
}
27+
}
28+
void foo(std::initializer_list<int> list) {
29+
for (std::initializer_list<int>::iterator it = list.begin(); it != list.end(); ++it) {
30+
vec.push_back(*it);
31+
}
32+
}
33+
};
34+
35+
int main() {
36+
// before C++11
37+
int arr[3] = {1, 2, 3};
38+
Foo foo(1, 2);
39+
std::vector<int> vec = {1, 2, 3, 4, 5};
40+
41+
// after C++11
42+
MagicFoo magicFoo = {1, 2, 3, 4, 5};
43+
magicFoo.foo({6,7,8,9});
44+
Foo foo2 {3, 4};
45+
46+
std::cout << "arr[0]: " << arr[0] << std::endl;
47+
std::cout << "foo:" << foo.value_a << ", " << foo.value_b << std::endl;
48+
std::cout << "vec: ";
49+
for (std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) {
50+
std::cout << *it << std::endl;
51+
}
52+
std::cout << "magicFoo: ";
53+
for (std::vector<int>::iterator it = magicFoo.vec.begin(); it != magicFoo.vec.end(); ++it) {
54+
std::cout << *it << std::endl;
55+
}
56+
std::cout << "foo2: " << foo2.value_a << ", " << foo2.value_b << std::endl;
57+
58+
return 0;
59+
}

0 commit comments

Comments
 (0)