├── .gitignore ├── c++14 ├── order.txt ├── Generic_Lambda-description.md ├── return_type_deduction-description.md ├── Generic_Lambda-solution.md └── return_type_deduction-solution.md ├── c++17 ├── order.txt ├── filesystem-description.md ├── Chrono-description.md ├── constexpr_if-description.md ├── Chrono_2-description.md ├── constexpr_if-solution.md ├── Chrono-solution.md ├── Structured_bindings-description.md ├── filesystem-solution.md ├── Chrono_2-solution.md └── Structured_bindings-solution.md ├── c++11 ├── Lambda_2-description.md ├── order.txt ├── trailing_return_type-description.md ├── type_inference_auto_decltype-description.md ├── Lambda-description.md ├── type_inference_auto-description.md ├── trailing_return_type-solution.md ├── Lambda_2-solution.md ├── type_inference_auto_decltype-solution.md ├── Lambda-solution.md └── type_inference_auto-solution.md ├── c++20 ├── Spaceship_Operator-description.md ├── Abbreviated_function_template-description.md ├── order.txt ├── Abbreviated_function_template-solution.md ├── source_location-description.md ├── Designated_initializers-description.md ├── Span-description.md ├── ranges-description.md ├── init_statements_and_initializers_in_range_for-description.md ├── init_statements_and_initializers_in_range_for-solution.md ├── Designated_initializers-solution.md ├── Chrono_Calendar-description.md ├── ranges-solution.md ├── source_location-solution.md ├── Span-solution.md ├── Spaceship_Operator-solution.md ├── Attribute_specifier-description.md ├── Attribute_specifier-solution.md └── Chrono_Calendar-solution.md └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea -------------------------------------------------------------------------------- /c++14/order.txt: -------------------------------------------------------------------------------- 1 | Generic_Lambda 2 | return_type_deduction 3 | -------------------------------------------------------------------------------- /c++17/order.txt: -------------------------------------------------------------------------------- 1 | constexpr_if 2 | Structured_bindings 3 | Chrono 4 | Chrono_2 5 | filesystem 6 | -------------------------------------------------------------------------------- /c++11/Lambda_2-description.md: -------------------------------------------------------------------------------- 1 | # Lambda 2 2 | 3 | What does the compiler create out of a lambda (approximately) 4 | -------------------------------------------------------------------------------- /c++11/order.txt: -------------------------------------------------------------------------------- 1 | type_inference_auto 2 | Lambda 3 | Lambda_2 4 | trailing_return_type 5 | type_inference_auto_decltype -------------------------------------------------------------------------------- /c++17/filesystem-description.md: -------------------------------------------------------------------------------- 1 | # \ 2 | 3 | Create a function that returns all file names in a directory. 4 | -------------------------------------------------------------------------------- /c++11/trailing_return_type-description.md: -------------------------------------------------------------------------------- 1 | # Trailing return type 2 | 3 | Create a method ```add(int a, int b)``` with trailing return type 4 | -------------------------------------------------------------------------------- /c++20/Spaceship_Operator-description.md: -------------------------------------------------------------------------------- 1 | # Spaceship Operator 2 | 3 | Create a struct "AnInt" that holds an int as is comparable like a normal int 4 | 5 | -------------------------------------------------------------------------------- /c++14/Generic_Lambda-description.md: -------------------------------------------------------------------------------- 1 | # Generic Lambda 2 | 3 | Create a generic lambda named ```add``` that adds two values of any type and returns the result. 4 | 5 | -------------------------------------------------------------------------------- /c++14/return_type_deduction-description.md: -------------------------------------------------------------------------------- 1 | # Keyword auto return value 2 | 3 | Create a function ```add``` that adds two values of any type and returns the result. 4 | 5 | -------------------------------------------------------------------------------- /c++11/type_inference_auto_decltype-description.md: -------------------------------------------------------------------------------- 1 | # Keyword auto return value 2 | 3 | Create a function ```add``` that adds two values of any type and returns the result. 4 | 5 | -------------------------------------------------------------------------------- /c++20/Abbreviated_function_template-description.md: -------------------------------------------------------------------------------- 1 | # Abbreviated function template 2 | 3 | Create a function that accepts a parameter of any type without the keyword template. 4 | -------------------------------------------------------------------------------- /c++11/Lambda-description.md: -------------------------------------------------------------------------------- 1 | # Lambda 2 | 3 | ### Part 1 4 | Write the shortest possible lambda 5 | 6 | 7 | ### Part 2 8 | Use a lambda to calculate the sum of two numbers and return the result -------------------------------------------------------------------------------- /c++17/Chrono-description.md: -------------------------------------------------------------------------------- 1 | # Chrono 2 | 3 | Measure the time in milliseconds this function needs to execute. 4 | 5 | ```cpp 6 | int test() { 7 | return 0; 8 | } 9 | 10 | int main() { 11 | test(); 12 | } 13 | ``` -------------------------------------------------------------------------------- /c++20/order.txt: -------------------------------------------------------------------------------- 1 | Abbreviated_function_template 2 | Span 3 | Chrono_Calendar 4 | ranges 5 | Spaceship_Operator 6 | Designated_initializers 7 | Attribute_specifier 8 | init_statements_and_initializers_in_range_for 9 | source_location 10 | 11 | -------------------------------------------------------------------------------- /c++20/Abbreviated_function_template-solution.md: -------------------------------------------------------------------------------- 1 | 2 | ## Solution 3 | 4 | ```cpp 5 | void f(auto b) { 6 | } 7 | ``` 8 | 9 | ## Links 10 | - [Cpp Reference](https://en.cppreference.com/w/cpp/language/function_template#Abbreviated_function_template) 11 | -------------------------------------------------------------------------------- /c++20/source_location-description.md: -------------------------------------------------------------------------------- 1 | # source_location 2 | 3 | Create a log function that prints the line number where the log occurred. 4 | (remember: depending on the current gcc version used, source_location might be in the experimental directory) 5 | 6 | 7 | -------------------------------------------------------------------------------- /c++20/Designated_initializers-description.md: -------------------------------------------------------------------------------- 1 | # Designated initializers 2 | 3 | Initialize a variable of type struct with designators. 4 | 5 | ```cpp 6 | struct AwesomeStruct { 7 | int anInt; 8 | int anotherInt; 9 | float aFloat; 10 | } 11 | ``` 12 | -------------------------------------------------------------------------------- /c++20/Span-description.md: -------------------------------------------------------------------------------- 1 | # Span 2 | 3 | Use span to create and call a function that adds the values of 4 | the following four variables: 5 | 6 | ```cpp 7 | int ca[]{2,3,4,5} 8 | std::vector v{1,2,3,4,5}; 9 | std::array a{1,2,3,4,5}; 10 | int *p = ca; 11 | ``` -------------------------------------------------------------------------------- /c++11/type_inference_auto-description.md: -------------------------------------------------------------------------------- 1 | # Keyword auto 2 | 3 | Iterate over vector ```data``` using the keyword ```auto``` 4 | ``` 5 | #include 6 | #include 7 | 8 | int main() { 9 | std::vector data(10); 10 | return 0; 11 | } 12 | ``` 13 | -------------------------------------------------------------------------------- /c++20/ranges-description.md: -------------------------------------------------------------------------------- 1 | # ranges 2 | 3 | Create the following vector `std::vector ints{0,1,2,3,4,5};` 4 | 5 | Use ranges (filter, transform...) to square the even numbers and print them out. 6 | 7 | (Example taken from [cppreference](https://en.cppreference.com/w/cpp/ranges)) 8 | -------------------------------------------------------------------------------- /c++20/init_statements_and_initializers_in_range_for-description.md: -------------------------------------------------------------------------------- 1 | # init-statements and initializers in range-for 2 | 3 | Iterate over the names vector and print name plus position in vector; 4 | 5 | ```cpp 6 | std::vector names = {"lisa","sven","karl","hugo"}; 7 | ``` 8 | 9 | 10 | -------------------------------------------------------------------------------- /c++11/trailing_return_type-solution.md: -------------------------------------------------------------------------------- 1 | # Trailing return type 2 | 3 | ``` 4 | auto add(int a, int b) -> int { 5 | return a+b; 6 | } 7 | 8 | int main() { 9 | return add(2,3); 10 | } 11 | ``` 12 | 13 | ## Links 14 | - [Cpp Reference](https://en.cppreference.com/w/cpp/language/function) (Second function definition) -------------------------------------------------------------------------------- /c++17/constexpr_if-description.md: -------------------------------------------------------------------------------- 1 | # constexpr-if 2 | 3 | Create an add function with the help of `if constexpr` that accepts all those cases: 4 | 5 | ```cpp 6 | std::string test{"hello"}; 7 | auto val1 = add(1,1); 8 | auto val2 = add(1.1,3); 9 | auto val3 = add(test,1.0); 10 | auto val4 = add("hello",1.0); 11 | ``` 12 | -------------------------------------------------------------------------------- /c++17/Chrono_2-description.md: -------------------------------------------------------------------------------- 1 | # Chrono-2 2 | 3 | With the help of chrono, count how many frames the execution of function test took, given a framerate of 60FPS. 4 | 5 | ```cpp 6 | int test() { 7 | std::this_thread::sleep_for(std::chrono::seconds(2)); 8 | return 0; 9 | } 10 | 11 | int main() { 12 | test(); 13 | 14 | return 0; 15 | } 16 | ``` -------------------------------------------------------------------------------- /c++14/Generic_Lambda-solution.md: -------------------------------------------------------------------------------- 1 | # Generic Lambda 2 | 3 | ``` 4 | #include 5 | #include 6 | 7 | int main() { 8 | auto add = [](auto x, auto y){return x+y;}; 9 | std::string str = add(std::string("3"),std::string("2.3")); 10 | float f = add(3,2.3); 11 | return 0; 12 | } 13 | ``` 14 | 15 | ##Links 16 | - [isocpp](https://isocpp.org/wiki/faq/cpp14-language#generic-lambdas) -------------------------------------------------------------------------------- /c++20/init_statements_and_initializers_in_range_for-solution.md: -------------------------------------------------------------------------------- 1 | ## Solution 2 | 3 | ```cpp 4 | int main() { 5 | std::vector names = {"lisa","sven","karl","hugo"}; 6 | for (int i = 0; auto name : names) // the init-statement (C++20) 7 | std::cout << i++ << ':' << name<<"\n"; 8 | } 9 | ``` 10 | 11 | ## Links 12 | 13 | - [Cpp Reference](https://en.cppreference.com/w/cpp/language/range-for) 14 | -------------------------------------------------------------------------------- /c++20/Designated_initializers-solution.md: -------------------------------------------------------------------------------- 1 | ## Solution 2 | 3 | ```cpp 4 | struct AwesomeStruct { 5 | int anInt; 6 | int anotherInt; 7 | float aFloat; 8 | }; 9 | 10 | int main() { 11 | AwesomeStruct{.anInt = 1, .anotherInt = 2, .aFloat = 3.2f}; 12 | return 0; 13 | } 14 | ``` 15 | 16 | ## Links 17 | 18 | - [Cpp Reference](https://en.cppreference.com/w/cpp/language/aggregate_initialization#Designated_initializers) 19 | -------------------------------------------------------------------------------- /c++20/Chrono_Calendar-description.md: -------------------------------------------------------------------------------- 1 | # chrono / calendar 2 | 3 | Create a function called `getAgeInYears` that expects the dayOfBirth as parameter 4 | and returns the age in year (with day precision) 5 | 6 | ## Info 7 | The current compiler version does not include all chrono featured yet, so you have to use 8 | the header \ installed from [https://github.com/HowardHinnant/date](https://github.com/HowardHinnant/date) 9 | 10 | -------------------------------------------------------------------------------- /c++17/constexpr_if-solution.md: -------------------------------------------------------------------------------- 1 | ## Solution 2 | 3 | ```cpp 4 | template 5 | auto add(T t1, M t2) { 6 | if constexpr(std::is_same_v || 7 | std::is_same_v) { 8 | return t1 + std::to_string(t2); 9 | } else { 10 | return t1+t2; 11 | } 12 | } 13 | ``` 14 | 15 | ## Links 16 | 17 | - [Simplify Code With 'if constexpr' in C++17](https://dzone.com/articles/simplify-code-with-if-constexpr-in-c17-1) 18 | - [Cpp Reference](https://en.cppreference.com/w/cpp/language/if) 19 | -------------------------------------------------------------------------------- /c++14/return_type_deduction-solution.md: -------------------------------------------------------------------------------- 1 | # Keyword auto return value 2 | 3 | ``` 4 | template 5 | auto add(T1 t1, T2 t2) { 6 | return t1+t2; 7 | } 8 | 9 | int main() { 10 | return add(4,3); 11 | } 12 | ``` 13 | 14 | ## Links 15 | - [Cpp Reference](https://en.cppreference.com/w/cpp/language/auto) 16 | 17 | ### Note 18 | Since C++20, the trailing return "trick" with ```decltype``` is not necessary anymore. 19 | (Abbreviated function template) 20 | - [Cpp Reference](https://en.cppreference.com/w/cpp/language/function_template#Abbreviated_function_template) 21 | 22 | 23 | -------------------------------------------------------------------------------- /c++17/Chrono-solution.md: -------------------------------------------------------------------------------- 1 | ## Solution 2 | 3 | ```cpp 4 | #include 5 | 6 | int test() { 7 | return 0; 8 | } 9 | 10 | 11 | int main() { 12 | auto t1 = std::chrono::system_clock::now(); 13 | test(); 14 | auto t2 = std::chrono::system_clock::now(); 15 | auto nanoseconds = std::chrono::duration_cast(t2-t1).count(); 16 | 17 | } 18 | ``` 19 | 20 | ## Links 21 | 22 | - [Cpp Reference](https://en.cppreference.com/w/cpp/header/chrono) 23 | - [(youtube) Meeting C++ 2019 Opening Keynote - Howard Hinnant](https://www.youtube.com/watch?v=adSAN282YIwo) 24 | -------------------------------------------------------------------------------- /c++17/Structured_bindings-description.md: -------------------------------------------------------------------------------- 1 | # Structured bindings 2 | 3 | ### Part 1 4 | Create expressive names within a range base for loop 5 | with the help of structured bindings 6 | 7 | ```cpp 8 | std::map portServiceMap = {{21,"FTP"},{80,"http"},{22,ssh}} 9 | for(? : portServiceMap) { 10 | ? 11 | std::cout << "Port: " << ? << "Service:" << ?; 12 | } 13 | ``` 14 | 15 | ### Part 2 16 | Create expressive names for the following variables 17 | 18 | ```cpp 19 | std::pair portMap = {21,"Ftp"}; 20 | const auto tuple = std::make_tuple(21, "Ftp", "TCP"); 21 | ``` -------------------------------------------------------------------------------- /c++11/Lambda_2-solution.md: -------------------------------------------------------------------------------- 1 | ## Solution 2 | 3 | ```cpp 4 | int main() 5 | { 6 | int a = 5; 7 | 8 | class __lambda_4_16 9 | { 10 | public: 11 | inline /*constexpr */ int operator()(int b) const 12 | { 13 | return a + b; 14 | } 15 | 16 | private: 17 | int & a; 18 | 19 | public: 20 | __lambda_4_16(int & _a) 21 | : a{_a} 22 | {} 23 | 24 | }; 25 | 26 | __lambda_4_16 sum = __lambda_4_16{a}; 27 | } 28 | ``` 29 | 30 | ## Links 31 | 32 | - [Cpp Reference](https://de.cppreference.com/w/cpp/language/lambda) 33 | - [cppinsights](https://cppinsights.io/) -------------------------------------------------------------------------------- /c++11/type_inference_auto_decltype-solution.md: -------------------------------------------------------------------------------- 1 | # Keyword auto return value 2 | 3 | ``` 4 | template 5 | auto add(T1 t1, T2 t2) -> decltype(t1 + t2) { 6 | return t1+t2; 7 | } 8 | 9 | int main() { 10 | return add(4,3); 11 | } 12 | ``` 13 | 14 | ## Links 15 | - [Cpp Reference](https://en.cppreference.com/w/cpp/language/auto) 16 | 17 | ### Note 18 | Since C++20, the trailing return "trick" with ```decltype``` is not necessary anymore. 19 | (Abbreviated function template) 20 | - [Cpp Reference](https://en.cppreference.com/w/cpp/language/function_template#Abbreviated_function_template) 21 | 22 | 23 | -------------------------------------------------------------------------------- /c++20/ranges-solution.md: -------------------------------------------------------------------------------- 1 | ## Solution 2 | 3 | ```cpp 4 | #include 5 | #include 6 | #include 7 | 8 | int main() 9 | { 10 | std::vector ints{0,1,2,3,4,5}; 11 | auto even = [](int i){ return 0 == i % 2; }; 12 | auto square = [](int i) { return i * i; }; 13 | 14 | for (int i : ints | std::views::filter(even) | std::views::transform(square)) { 15 | std::cout << i << ' '; 16 | } 17 | } 18 | ``` 19 | 20 | ## Links 21 | 22 | - [Cpp Reference](https://en.cppreference.com/w/cpp/ranges) 23 | - [https://ericniebler.github.io/range-v3/](https://ericniebler.github.io/range-v3/) 24 | -------------------------------------------------------------------------------- /c++17/filesystem-solution.md: -------------------------------------------------------------------------------- 1 | ## Solution 2 | 3 | ```cpp 4 | #include 5 | #include 6 | 7 | 8 | std::vector listFiles(std::string const& path) { 9 | std::string path_name = "."; 10 | 11 | std::vector names; 12 | 13 | for (auto &entry : std::filesystem::directory_iterator(path_name)) { 14 | names.push_back(entry.path().filename()); 15 | } 16 | return names; 17 | } 18 | 19 | int main() { 20 | auto files = listFiles("."); 21 | return 0; 22 | } 23 | ``` 24 | 25 | ## Links 26 | 27 | - [Cpp Reference](https://en.cppreference.com/w/cpp/header/filesystem) 28 | -------------------------------------------------------------------------------- /c++20/source_location-solution.md: -------------------------------------------------------------------------------- 1 | ## Solution 2 | 3 | ```cpp 4 | #include 5 | #include 6 | 7 | void log(std::string_view message, 8 | const std::experimental::source_location& location = std::experimental::source_location::current()) 9 | { 10 | std::cout << "info:" 11 | << location.file_name() << ":" 12 | << location.line() << ' ' 13 | << message << '\n'; 14 | } 15 | 16 | int main() 17 | { 18 | log("Hello world!"); 19 | } 20 | ``` 21 | 22 | ## Links 23 | 24 | - [Cpp Reference](https://en.cppreference.com/w/cpp/utility/source_location) 25 | -------------------------------------------------------------------------------- /c++20/Span-solution.md: -------------------------------------------------------------------------------- 1 | ## Solution 2 | 3 | ```cpp 4 | #include 5 | #include 6 | 7 | int add(std::span vals) { 8 | int sum=0; 9 | for(auto const& val : vals) { 10 | sum += val; 11 | } 12 | return sum; 13 | } 14 | 15 | 16 | int main() { 17 | int ca[]{2,3,4,5}; 18 | std::vector v{1,2,3,4,5}; 19 | std::array a{1,2,3,4,5}; 20 | int *p = ca; 21 | 22 | add(ca); 23 | add(v); 24 | add(a); 25 | 26 | add({p,4}); 27 | // or 28 | add(std::span{p,4}); 29 | 30 | return 0; 31 | } 32 | ``` 33 | 34 | ## Links 35 | 36 | - [Cpp Reference](https://en.cppreference.com/w/cpp/container/span) 37 | -------------------------------------------------------------------------------- /c++20/Spaceship_Operator-solution.md: -------------------------------------------------------------------------------- 1 | ## Solution 2 | 3 | ```cpp 4 | #include 5 | struct AnInt { 6 | int value; 7 | constexpr AnInt(int value): value{value} { } 8 | std::strong_ordering operator<=>(const AnInt&) const = default; 9 | }; 10 | 11 | int main() { 12 | AnInt test1(3); 13 | AnInt test2(4); 14 | 15 | if (test1 < test1) { 16 | return 1; 17 | } 18 | return 0; 19 | } 20 | ``` 21 | 22 | ## Links 23 | 24 | - [Cpp Reference - Three way comparison](https://en.cppreference.com/w/cpp/language/operator_comparison#Three-way_comparison) 25 | - [Cpp Reference - compare header](https://en.cppreference.com/w/cpp/header/compare) 26 | -------------------------------------------------------------------------------- /c++11/Lambda-solution.md: -------------------------------------------------------------------------------- 1 | ## Solution part 1 2 | 3 | ```cpp 4 | auto lambda = []{}; 5 | ``` 6 | 7 | ## Solution part 2 8 | 9 | ```cpp 10 | int main (){ 11 | auto sum = [](auto x, auto y) { return x + y; }; 12 | sum(2,3); 13 | } 14 | ``` 15 | 16 | ## Links 17 | 18 | - [Cpp Reference](https://de.cppreference.com/w/cpp/language/lambda) 19 | 20 | 21 | ## general knowledge 22 | structure of a lambda 23 | ```cpp 24 | [captures] (params) {body} 25 | ``` 26 | 27 | captures: 28 | - `[]` captures nothing 29 | - `[&]` captures all variables used in the lambda by reference 30 | - `[=]` captures all variables used in the lambda by value 31 | - `[this]` captures `this` pointer by value 32 | -------------------------------------------------------------------------------- /c++17/Chrono_2-solution.md: -------------------------------------------------------------------------------- 1 | ## Solution 2 | 3 | ```cpp 4 | #include 5 | #include 6 | 7 | int test() { 8 | std::this_thread::sleep_for(std::chrono::seconds(2)); 9 | return 0; 10 | } 11 | 12 | using chrono_frames = std::chrono::duration>; 13 | 14 | int main() { 15 | auto t1 = std::chrono::system_clock::now(); 16 | test(); 17 | auto t2 = std::chrono::system_clock::now(); 18 | auto myFrames = std::chrono::duration_cast(t2-t1).count(); 19 | 20 | return 0; 21 | } 22 | ``` 23 | 24 | ## Links 25 | 26 | - [Cpp Reference](https://en.cppreference.com/w/cpp/header/chrono) 27 | - [(youtube) Meeting C++ 2019 Opening Keynote - Howard Hinnant](https://www.youtube.com/watch?v=adSAN282YIwo) 28 | -------------------------------------------------------------------------------- /c++20/Attribute_specifier-description.md: -------------------------------------------------------------------------------- 1 | # Attribute specifier 2 | 3 | Use Attributes (`likely, unlikly and fallthrough`) to optimize the given code and make them compile. 4 | 5 | - `case 1:` has no preference 6 | - `case 2:` is the most likely branch 7 | - `case 3:` is tht most unlikely branch 8 | 9 | ```cpp 10 | int calcSquare(int number){ 11 | return (number * number); 12 | } 13 | 14 | int f(int i) 15 | { 16 | int res = 0; 17 | switch(i) 18 | { 19 | case 1: 20 | res = calcSquare(i); 21 | case 2: 22 | res = calcSquare(i); 23 | break; 24 | case 3: 25 | res = calcSquare(i); 26 | break; 27 | } 28 | return res; 29 | } 30 | 31 | int main() { 32 | f(1); 33 | f(2); 34 | f(3); 35 | return 0; 36 | } 37 | ``` 38 | 39 | 40 | -------------------------------------------------------------------------------- /c++20/Attribute_specifier-solution.md: -------------------------------------------------------------------------------- 1 | ## Solution 2 | 3 | ```cpp 4 | int calcSquare(int number){ 5 | return (number * number); 6 | } 7 | 8 | int f(int i) 9 | { 10 | int res = 0; 11 | switch(i) 12 | { 13 | case 1: 14 | res = calcSquare(i); 15 | [[fallthrough]]; 16 | [[likely]] case 2: 17 | res = calcSquare(i); 18 | break; 19 | [[unlikely]] case 3: 20 | res = calcSquare(i); 21 | break; 22 | } 23 | return res; 24 | } 25 | 26 | int main() { 27 | f(1); 28 | f(2); 29 | f(3); 30 | return 0; 31 | } 32 | ``` 33 | 34 | ## Note 35 | ```[fallthrough]``` was actually implemented in C++17, just added it here for completion. 36 | 37 | ## Links 38 | 39 | - [Cpp Reference: attribute specifier sequence](https://en.cppreference.com/w/cpp/language/attributes) 40 | -------------------------------------------------------------------------------- /c++17/Structured_bindings-solution.md: -------------------------------------------------------------------------------- 1 | ## Solution part 1 2 | ```cpp 3 | #include 4 | #include 5 | #include 6 | 7 | int main() { 8 | std::unordered_map portServiceMap = {{21,"FTP"},{80,"http"},{22,"ssh"}}; 9 | for(auto const&[port, service] : portServiceMap) { 10 | std::cout << "Port: " << port << "Service:" << service; 11 | } 12 | return 0; 13 | } 14 | ``` 15 | 16 | ## Solution part 2 17 | ``` 18 | #include 19 | #include 20 | #include 21 | 22 | int main() { 23 | 24 | std::pair pair = {21,"Ftp"}; 25 | const auto tuple = std::make_tuple(21, "Ftp", "TCP"); 26 | 27 | auto const& [port, service] = pair; 28 | auto const& [port2,service2,protocol] = tuple; 29 | 30 | return 0; 31 | } 32 | ``` 33 | 34 | ## Links 35 | 36 | - [Cpp Reference](https://en.cppreference.com/w/cpp/language/structured_binding) 37 | -------------------------------------------------------------------------------- /c++20/Chrono_Calendar-solution.md: -------------------------------------------------------------------------------- 1 | ## Solution 2 | 3 | ```cpp 4 | #include 5 | #include 6 | 7 | int getAgeInYears(date::year_month_day dayOfBirth) { 8 | // timestamp 9 | auto now = std::chrono::system_clock::now(); 10 | // convert to sys_days 11 | date::sys_days nowSysDays = date::floor(now); 12 | // convert to year_month_day (same as dayOfBirth) 13 | date::year_month_day nowYmd = date::year_month_day{nowSysDays}; 14 | auto ageInDays = date::local_days{nowYmd} - date::local_days{dayOfBirth}; 15 | date::years ageInYears = std::chrono::duration_cast(ageInDays); 16 | return ageInYears.count(); 17 | } 18 | 19 | int main() 20 | { 21 | using namespace date; 22 | constexpr auto x1 = 1981_y/March/11; 23 | constexpr auto x2 = October/07/1991; 24 | constexpr auto x3 = 26_d/October/2015; 25 | std::cout << "You are " << getAgeInYears(x1) << " years old\n"; 26 | std::cout << "You are " << getAgeInYears(x2) << " years old\n"; 27 | std::cout << "You are " << getAgeInYears(x3) << " years old\n"; 28 | return 0; 29 | } 30 | ``` 31 | 32 | ## Links 33 | 34 | [https://en.cppreference.com/w/cpp/chrono](cppreference) 35 | [https://github.com/HowardHinnant/date](https://github.com/HowardHinnant/date) 36 | -------------------------------------------------------------------------------- /c++11/type_inference_auto-solution.md: -------------------------------------------------------------------------------- 1 | 2 | ### C++98 3 | 4 | Before C++ 11 we had to use iterators which in some examples could be really 5 | long and incomprehensible 6 | ```cpp 7 | std::vector data(10); 8 | for (std::vector::iterator it = data.begin() ; it != data.end(); ++it) { 9 | std::cout << ' ' << *it; 10 | } 11 | ``` 12 | 13 | ### C++11 14 | Now with the new keyword ```auto``` we can do: 15 | 16 | ```cpp 17 | std::vector data(10); 18 | for(auto it = data.begin(); it != data.end(); it ++) { 19 | std::cout << ' ' << *it; 20 | } 21 | ``` 22 | 23 | Or even simpler: 24 | 25 | ```cpp 26 | std::vector data(10); 27 | for(auto const& val : data) { 28 | std::cout << ' ' << val; 29 | } 30 | ``` 31 | 32 | ### All together 33 | ``` 34 | #include 35 | #include 36 | 37 | int main() { 38 | std::vector data(10); 39 | 40 | for (std::vector::iterator it = data.begin() ; it != data.end(); ++it) { 41 | std::cout << ' ' << *it; 42 | } 43 | for(auto it = data.begin(); it != data.end(); it ++) { 44 | std::cout << ' ' << *it; 45 | } 46 | 47 | for(auto const& val : data) { 48 | std::cout << ' ' << val; 49 | } 50 | return 0; 51 | } 52 | 53 | ``` 54 | 55 | ## Links 56 | - [Cpp Reference](https://en.cppreference.com/w/cpp/language/auto) 57 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | Coding Cards 4 |

5 | 6 | -------- 7 | 8 | ## What is Coding Cards 9 | 10 | **[Coding Cards](https://codingcards.org)** is a web application for learning C++ features in the style of flashcards. 11 | It is also possible to use the built-in editor/compiler to test your solution and play around. 12 | 13 | It started as a hobby project with a static set of cards, but we got so much positive feedback that we decided to add an option for the community to create new cards. 14 | 15 | 16 | Website: https://codingcards.org 17 | 18 | 19 | ## How to add Cards to the Project 20 | The project structure: 21 | 22 | . 23 | ├── C++11 24 | │ ├── card_name-description.md #Short description (exercise) 25 | │ ├── card_name-solution.md #The solution and a link to cppreference.com 26 | │ ├── order.txt #File with the order of the cards... don't modify this file 27 | │ └── ... 28 | ├── C++14 29 | │ ├── card_name-description.md 30 | │ ├── card_name-solution.md 31 | │ ├── order.txt 32 | │ └── ... 33 | ├── C++17 34 | │ ├── card_name-description.md 35 | │ ├── card_name-solution.md 36 | │ ├── order.txt 37 | │ └── ... 38 | └── ... 39 | 40 | To add a new card: 41 | 42 | * Choose a C++ version like **C++20** and add 2 files to the subdirectory. 43 | * **card_name-description.md** 44 | * **card_name-solution.md** 45 | * Required content of card_name-description.md 46 | * Headline (# Lambda) 47 | * Short and pregnant description/instruction 48 | * Required content of card_name-solution.md 49 | * Headline (# Solution) 50 | * Show the solution as code 51 | * Headline (# Links) 52 | * Add a link to cppreference.com 53 | 54 | After we accept your pull request we will add the card to the order.txt 55 | --------------------------------------------------------------------------------