-
Notifications
You must be signed in to change notification settings - Fork 12
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
46 changed files
with
232 additions
and
172 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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
// 01-hellow.cpp : prints a line of text to the console | ||
|
||
#include <iostream> | ||
#include <print> | ||
using namespace std; | ||
|
||
int main() { | ||
cout << "Hello, World!" << '\n'; | ||
println("Hello, World!"); | ||
} |
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 |
---|---|---|
@@ -1,15 +1,15 @@ | ||
// 01-title.cpp : output the title page of a well-known book | ||
|
||
#include <iostream> | ||
#include <print> | ||
using namespace std; | ||
|
||
int main() { | ||
cout << 1+R"( | ||
print(1+R"( | ||
Alice's | ||
Adventures In | ||
Wonderland | ||
by | ||
LEWIS CARROLL | ||
)"; | ||
)"); | ||
} |
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 |
---|---|---|
@@ -1,14 +1,14 @@ | ||
// 02-assign.cpp : assign to local variables | ||
|
||
#include <iostream> | ||
#include <print> | ||
using namespace std; | ||
|
||
int main() { | ||
int i = 1, j = 2; | ||
unsigned k; | ||
cout << "(1) i = " << i << ", j = " << j << ", k = " << k << '\n'; | ||
println("(1) i = {}, j = {}, k = {}", i, j, k); | ||
i = j; | ||
j = 3; | ||
k = -1; | ||
cout << "(2) i = " << i << ", j = " << j << ", k = " << k << '\n'; | ||
println("(2) i = {}, j = {}, k = {}", i, j, k); | ||
} |
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 |
---|---|---|
@@ -1,13 +1,12 @@ | ||
// 02-constants.cpp : introducing the const keyword | ||
|
||
#include <iostream> | ||
#include <print> | ||
using namespace std; | ||
|
||
const double PI = 3.14159265358979; | ||
|
||
int main() { | ||
auto const APPROX_E = 3; | ||
cout << "pi is almost exactly " << PI | ||
<< "e is approximately " << APPROX_E | ||
<< '\n'; | ||
println("pi is almost exactly {}, while e is approximately {}", | ||
PI, APPROX_E); | ||
} |
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 |
---|---|---|
@@ -1,17 +1,16 @@ | ||
// 02-constexpr.cpp : introducing the constexpr keyword | ||
|
||
#include <iostream> | ||
#include <print> | ||
#include <cmath> | ||
using namespace std; | ||
|
||
const double PI1 = acos(-1.0); // acos is not (yet) constexpr | ||
constexpr double PI1 = acos(-1.0); | ||
constexpr double PI2 = 22.0 / 7.0; | ||
|
||
// the following line does not compile and has been commented out | ||
//static_assert(PI1 > 3.141 && PI1 < 3.143); | ||
static_assert(PI1 > 3.141 && PI1 < 3.143); | ||
static_assert(PI2 > 3.141 && PI2 < 3.143); | ||
|
||
int main() { | ||
cout << "PI1 = " << PI1 << '\n'; | ||
cout << "PI2 = " << PI2 << '\n'; | ||
println("PI1 = {}", PI1); | ||
println("PI2 = {}", PI2); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
// 02-references.cpp : introducing l-value references | ||
|
||
#include <iostream> | ||
#include <print> | ||
using namespace std; | ||
|
||
int alice_age{ 9 }; | ||
|
||
int main() { | ||
cout << "Alice\'s age is " << alice_age << '\n'; | ||
println("Alice\'s age is {}", alice_age); | ||
int& alice_age_ref = alice_age; | ||
alice_age_ref = 10; | ||
cout << "Alice\'s age is now " << alice_age << '\n'; | ||
println("Alice\'s age is now {}", alice_age); | ||
} |
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 |
---|---|---|
@@ -1,16 +1,16 @@ | ||
// 02-scopes.cpp : define three variables with the same name in one program | ||
|
||
#include <iostream> | ||
#include <print> | ||
using namespace std; | ||
|
||
auto a{ 1.5f }; | ||
|
||
int main() { | ||
cout << "(1) " << a << '\n'; | ||
println("(1) {}", a); | ||
auto a{ 2u }; | ||
cout << "(2) " << a << '\n'; | ||
println("(2) {}", a); | ||
{ | ||
auto a{ 2.5 }; | ||
cout << "(3) " << a << '\n'; | ||
println("(3) {}", a); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
// 02-swap.cpp : attempt to swap the values of an int and a double | ||
|
||
#include <iostream> | ||
#include <print> | ||
using namespace std; | ||
|
||
int main() { | ||
int a = 1; | ||
double b = 2.5; | ||
cout << "(1) a = " << a << ", b = " << b << '\n'; | ||
println("(1) a = {}, b = {}", a, b); | ||
a = 2.5; | ||
b = 1; | ||
cout << "(2) a = " << a << ", b = " << b << '\n'; | ||
println("(2) a = {}, b = {}", a, b); | ||
} |
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 |
---|---|---|
@@ -1,11 +1,11 @@ | ||
// 02-uniform.cpp : avoid compiler error with uniform initialization and explicit narrowing cast | ||
|
||
#include <iostream> | ||
#include <print> | ||
using namespace std; | ||
|
||
int main() { | ||
// int c = { 2.5 }; // Error: this does NOT compile | ||
int c = { static_cast<int>(2.5) }; // while this does | ||
double d = { 1 }; // and so does this | ||
cout << "c = " << c << ", d = " << d << '\n'; | ||
println("c = {}, d = {}", c, d); | ||
} |
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 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 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,12 @@ | ||
// 08-format1.cpp : Basic usage of format string | ||
|
||
#include <print> | ||
#include <string> | ||
using namespace std; | ||
|
||
int main() { | ||
string s{ "Formatted" }; | ||
auto d{ 10.0 / 3.0 }; | ||
auto i{ 20000 }; | ||
println("{0:20}:{2:8}, {1:12.11}", s, d, i); | ||
} |
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 @@ | ||
// 08-format2.cpp : Various format string-using functions | ||
|
||
#include <print> | ||
#include <format> | ||
#include <string> | ||
#include <iostream> | ||
#include <iterator> | ||
#include <array> | ||
#include <cmath> | ||
using namespace std; | ||
|
||
int main() { | ||
string world{ "World" }; | ||
print(cout, "Hello, {}!\n", world); | ||
println("{1} or {0}", false, true); | ||
|
||
constexpr const char *fmt = "Approximation of π = {:.12g}"; | ||
string s = format(fmt, asin(1.0) * 2); | ||
cout << s << '\n'; | ||
|
||
constexpr const wchar_t *wfmt = L"Approximation of pi = {:.12g}"; | ||
wstring ws = format(wfmt, asin(1.0) * 2); | ||
wcout << ws << L'\n'; | ||
|
||
format_to(ostream_iterator<char>(cout), "Hello, {}!\n", world); | ||
wstring ww{ L"World" }; | ||
array<wchar_t,9> wa; | ||
auto iter = format_to_n(wa.begin(), 8, L"Hello, {}!\n", ww); | ||
*(iter.out) = L'\0'; | ||
wcout << wa.data() << L'\n'; | ||
} |
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 |
---|---|---|
@@ -1,27 +1,26 @@ | ||
// 09-person1.cpp : model Person as a class with constructor | ||
|
||
#include <chrono> | ||
#include <iostream> | ||
#include <string> | ||
#include <string_view> | ||
using namespace std; | ||
|
||
struct Date { | ||
int year{}, month{}, day{}; | ||
}; | ||
using namespace std::chrono; | ||
|
||
class Person { | ||
public: | ||
Person(const Date& dob, string_view familyname, string_view firstname) | ||
Person(const year_month_day& dob, string_view familyname, string_view firstname) | ||
: dob{ dob }, familyname{ familyname }, firstname{ firstname } | ||
{} | ||
string getName() const { return firstname + ' ' + familyname; } | ||
const year_month_day& getDob() const { return dob; } | ||
private: | ||
const Date dob; | ||
const year_month_day dob; | ||
string familyname, firstname; | ||
}; | ||
|
||
|
||
int main() { | ||
Person genius{ { 1879, 3, 14 }, "Einstein", "Albert" }; | ||
cout << genius.getName() << '\n'; | ||
Person genius{ { 1879y, March, 14d }, "Einstein", "Albert" }; | ||
cout << genius.getName() << " was born " << genius.getDob() << '\n'; | ||
} |
Oops, something went wrong.