forked from changkun/modern-cpp-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
28 changed files
with
460 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,3 +27,6 @@ | |
*.exe | ||
*.out | ||
*.app | ||
|
||
node_modules | ||
website/public |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.