Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit838d30e

Browse files
committed
revisionchangkun#1: 更新第二章中已维护的代码
1 parent11efa38 commit838d30e

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.cpprenamed 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
intmain() {
15-
//使用 lambda表达式
15+
//use lambdaexpression
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+
// Ccode
1313
intadd(intx,inty) {
1414
returnx+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+
voidfoo(char *);
13+
voidfoo(int);
14+
15+
intmain() {
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+
return0;
27+
}
28+
29+
voidfoo(char *) {
30+
std::cout <<"foo(char*) is called" << std::endl;
31+
}
32+
voidfoo(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+
autoprint_type_info(const T& t) {
13+
ifconstexpr (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+
intmain() {
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+
intmain() {
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+
#defineLEN10
11+
12+
intlen_foo() {
13+
int i =2;
14+
return i;
15+
}
16+
constexprintlen_foo_constexpr() {
17+
return5;
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+
constexprintfibonacci(constint n) {
29+
return n ==1 || n ==2 ?1 :fibonacci(n-1) +fibonacci(n-2);
30+
}
31+
32+
33+
intmain() {
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+
constint 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+
return0;
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+
intmain() {
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+
classFoo {
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+
classMagicFoo {
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+
voidfoo(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+
intmain() {
36+
// before C++11
37+
int arr[3] = {1,2,3};
38+
Foofoo(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+
return0;
59+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp