Skip to content

Commit 2321608

Browse files
author
dennis
committed
Initial commit
0 parents  commit 2321608

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+826
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

README.md

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<h1 align="center">
2+
<img align="center" alt="Coding Cards" width="50%" src="https://codingcards.org/static/img/logo/Coding_Cards_Logo_and_Text.svg" />
3+
</h1>
4+
5+
--------
6+
7+
## What is Coding Cards
8+
9+
**[Coding Cards](https://codingcards.org)** is a web application for learning C++ features in the style of flashcards.
10+
It is also possible to use the built-in editor/compiler to test your solution and play around.
11+
12+
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.
13+
14+
15+
Website: https://codingcards.org
16+
17+
18+
## How to add Cards to the Project
19+
The project structure:
20+
21+
.
22+
├── C++11
23+
│ ├── card_name-description.md #Short description (exercise)
24+
│ ├── card_name-solution.md #The solution and a link to the cppreferenz page
25+
│ ├── order.txt #File with the order of the cards... dont modify this file
26+
│ └── ...
27+
├── C++14
28+
│ ├── card_name-description.md
29+
│ ├── card_name-solution.md
30+
│ ├── order.txt
31+
│ └── ...
32+
├── C++17
33+
│ ├── card_name-description.md
34+
│ ├── card_name-solution.md
35+
│ ├── oder.txt
36+
│ └── ...
37+
└── ...
38+
39+
To add a new card:
40+
41+
* Choose a C++ version like **C++20** and add 2 files to the subdirectory.
42+
* **card_name-description.md**
43+
* **card_name-solution.md**
44+
* Required content of card_name-description.md
45+
* Headline (# Lambda)
46+
* Short and pregnant description/instruction
47+
* Required content of card_name-solution.md
48+
* Headline (# Solution)
49+
* Show the solution as code
50+
* Headline (# Links)
51+
* Add a link to cppreference
52+
53+
After we accept your pull request we will add the card to the order.txt

c++11/Lambda-description.md

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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++11/Lambda-solution.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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

c++11/Lambda_2-description.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Lambda 2
2+
3+
What does the compiler create out of a lambda (approximately)

c++11/Lambda_2-solution.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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/order.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
type_inference_auto
2+
Lambda
3+
Lambda_2
4+
trailing_return_type
5+
type_inference_auto_decltype
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Trailing return type
2+
3+
Create a method ```add(int a, int b)``` with trailing return type
4+
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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)
+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Keyword auto
2+
3+
Iterate over vector ```data``` using the keyword ```auto```
4+
```
5+
#include <vector>
6+
#include <iostream>
7+
8+
int main() {
9+
std::vector<int> data(10);
10+
return 0;
11+
}
12+
```

c++11/type_inference_auto-solution.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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<int> data(10);
8+
for (std::vector<int>::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<int> 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<int> data(10);
27+
for(auto const& val : data) {
28+
std::cout << ' ' << val;
29+
}
30+
```
31+
32+
### All together
33+
```
34+
#include <vector>
35+
#include <iostream>
36+
37+
int main() {
38+
std::vector<int> data(10);
39+
40+
for (std::vector<int>::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)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Keyword auto return value
2+
3+
Create a function ```add``` that adds two values of any type and returns the result.
4+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Keyword auto return value
2+
3+
```
4+
template <typename T1, typename T2>
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+

c++14/Generic_Lambda-description.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Generic Lambda
2+
3+
Create a generic lambda named ```add``` that adds two values of any type and returns the result.
4+

c++14/Generic_Lambda-solution.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Generic Lambda
2+
3+
```
4+
#include <iostream>
5+
#include <string>
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++14/order.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Generic_Lambda
2+
return_type_deduction
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Keyword auto return value
2+
3+
Create a function ```add``` that adds two values of any type and returns the result.
4+
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Keyword auto return value
2+
3+
```
4+
template <typename T1, typename T2>
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+

c++17/Chrono-description.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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++17/Chrono-solution.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
## Solution
2+
3+
```cpp
4+
#include <chrono>
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<std::chrono::nanoseconds>(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)

c++17/Chrono_2-description.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
```

0 commit comments

Comments
 (0)