Skip to content

Commit

Permalink
revision changkun#1: 更新第二章中已维护的代码
Browse files Browse the repository at this point in the history
  • Loading branch information
changkun committed Apr 1, 2018
1 parent 11efa38 commit 838d30e
Show file tree
Hide file tree
Showing 28 changed files with 460 additions and 111 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@
*.exe
*.out
*.app

node_modules
website/public
4 changes: 2 additions & 2 deletions code/1/1.1.cpp → code/1/1.1.c.and.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// 1.1.cpp
//
// chapter 1 introduction
// c++1x tutorial
// modern cpp tutorial
//
// created by changkun at changkun.de
//
Expand All @@ -12,7 +12,7 @@
#include <functional>

int main() {
// 使用 lambda 表达式
// use lambda expression
[out = std::ref(std::cout << "Result from C code: " << add(1, 2))](){
out.get() << ".\n";
}();
Expand Down
10 changes: 5 additions & 5 deletions code/1/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@
# 1.1.cpp
#
# chapter 1 introduction
# c++1x tutorial
# modern cpp tutorial
#
# created by changkun at changkun.de
#

C = gcc
CXX = g++
CXX = clang++

SOURCE_C = foo.c
OBJECTS_C = foo.o

SOURCE_CXX = 1.1.cpp
SOURCE_CXX = 1.1.c.and.cpp

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

all:
$(C) -c $(SOURCE_C)
Expand Down
4 changes: 2 additions & 2 deletions code/1/foo.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// foo.c
//
// chapter 1 introduction
// c++1x tutorial
// modern cpp tutorial
//
// created by changkun at changkun.de
//

#include "foo.h"

// C 语言代码
// C code
int add(int x, int y) {
return x+y;
}
2 changes: 1 addition & 1 deletion code/1/foo.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// foo.h
//
// chapter 1 introduction
// c++1x tutorial
// modern cpp tutorial
//
// created by changkun at changkun.de
//
Expand Down
32 changes: 0 additions & 32 deletions code/2/2.1.cpp

This file was deleted.

34 changes: 34 additions & 0 deletions code/2/2.1.nullptr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// 2.1.nullptr.cpp
// chapter 2 language usability
// modern cpp tutorial
//
// created by changkun at changkun.de
//

#include <iostream>
#include <type_traits>

void foo(char *);
void foo(int);

int main() {
if (std::is_same<decltype(NULL), decltype(0)>::value)
std::cout << "NULL == 0" << std::endl;
if (std::is_same<decltype(NULL), decltype((void*)0)>::value)
std::cout << "NULL == (void *)0" << std::endl;
if (std::is_same<decltype(NULL), std::nullptr_t>::value)
std::cout << "NULL == nullptr" << std::endl;

foo(0); // will call foo(int)
// foo(NULL); // doen't compile
foo(nullptr); // will call foo(char*)
return 0;
}

void foo(char *) {
std::cout << "foo(char*) is called" << std::endl;
}
void foo(int i) {
std::cout << "foo(int) is called" << std::endl;
}
31 changes: 31 additions & 0 deletions code/2/2.10.if.constexpr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// 2.10.if.constexpr.cpp
// chapter 2 language usability
// modern cpp tutorial
//
// created by changkun at changkun.de
//

#include <iostream>

template<typename T>
auto print_type_info(const T& t) {
if constexpr (std::is_integral<T>::value) {
return t + 1;
} else {
return t + 0.001;
}
}

// at compiling time
// int print_type_info(const int& t) {
// return t + 1;
// }
// double print_type_info(const double& t) {
// return t + 0.001;
// }

int main() {
std::cout << print_type_info(5) << std::endl;
std::cout << print_type_info(3.14) << std::endl;
}
23 changes: 23 additions & 0 deletions code/2/2.11.for.loop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// 2.11.for.loop.cpp
// chapter 2 language usability
// modern cpp tutorial
//
// created by changkun at changkun.de
//

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
std::vector<int> vec = {1, 2, 3, 4};
if (auto itr = std::find(vec.begin(), vec.end(), 3); itr != vec.end()) *itr = 4;
for (auto element : vec)
std::cout << element << std::endl; // read only
for (auto &element : vec) {
element += 1; // writeable
}
for (auto element : vec)
std::cout << element << std::endl; // read only
}
50 changes: 50 additions & 0 deletions code/2/2.2.constexpr.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//
// 2.2.constexpr.cpp
// chapter 2 language usability
// modern cpp tutorial
//
// created by changkun at changkun.de
//

#include <iostream>
#define LEN 10

int len_foo() {
int i = 2;
return i;
}
constexpr int len_foo_constexpr() {
return 5;
}

// error in c++11
// constexpr int fibonacci(const int n) {
// if(n == 1) return 1;
// if(n == 2) return 1;
// return fibonacci(n-1) + fibonacci(n-2);
// }

// ok
constexpr int fibonacci(const int n) {
return n == 1 || n == 2 ? 1 : fibonacci(n-1) + fibonacci(n-2);
}


int main() {
char arr_1[10]; // legal
char arr_2[LEN]; // legal

int len = 10;
// char arr_3[len]; // illegal

const int len_2 = len + 1;
char arr_4[len_2]; // legal

// char arr_5[len_foo()+5]; // illegal
char arr_6[len_foo_constexpr() + 1]; // legal

// 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
std::cout << fibonacci(10) << std::endl;

return 0;
}
34 changes: 0 additions & 34 deletions code/2/2.2.cpp

This file was deleted.

31 changes: 31 additions & 0 deletions code/2/2.3.if.switch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// 2.3.if.switch.cpp
// chapter 2 language usability
// modern cpp tutorial
//
// created by changkun at changkun.de
//

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
std::vector<int> vec = {1, 2, 3, 4};

// before c++17, can be simplefied by using `auto`
const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 2);
if (itr != vec.end()) {
*itr = 3;
}

// after c++17, can be simplefied by using `auto`
if (const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 3);
itr != vec.end()) {
*itr = 4;
}

// should output: 1, 4, 3, 4. can be simplefied using `auto`
for (std::vector<int>::iterator element = vec.begin(); element != vec.end(); ++element)
std::cout << *element << std::endl;
}
59 changes: 59 additions & 0 deletions code/2/2.4.initializer.list.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//
// 2.4.initializer.list.cpp
// chapter 2 language usability
// modern cpp tutorial
//
// created by changkun at changkun.de
//

#include <iostream>
#include <initializer_list>
#include <vector>

class Foo {
public:
int value_a;
int value_b;
Foo(int a, int b) : value_a(a), value_b(b) {}
};

class MagicFoo {
public:
std::vector<int> vec;
MagicFoo(std::initializer_list<int> list) {
for (std::initializer_list<int>::iterator it = list.begin(); it != list.end(); ++it) {
vec.push_back(*it);
}
}
void foo(std::initializer_list<int> list) {
for (std::initializer_list<int>::iterator it = list.begin(); it != list.end(); ++it) {
vec.push_back(*it);
}
}
};

int main() {
// before C++11
int arr[3] = {1, 2, 3};
Foo foo(1, 2);
std::vector<int> vec = {1, 2, 3, 4, 5};

// after C++11
MagicFoo magicFoo = {1, 2, 3, 4, 5};
magicFoo.foo({6,7,8,9});
Foo foo2 {3, 4};

std::cout << "arr[0]: " << arr[0] << std::endl;
std::cout << "foo:" << foo.value_a << ", " << foo.value_b << std::endl;
std::cout << "vec: ";
for (std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) {
std::cout << *it << std::endl;
}
std::cout << "magicFoo: ";
for (std::vector<int>::iterator it = magicFoo.vec.begin(); it != magicFoo.vec.end(); ++it) {
std::cout << *it << std::endl;
}
std::cout << "foo2: " << foo2.value_a << ", " << foo2.value_b << std::endl;

return 0;
}
Loading

0 comments on commit 838d30e

Please sign in to comment.