Skip to content

Commit 9705725

Browse files
authored
tweaks for tutorial/cpp (acs-pa#19)
1 parent 1ac27a9 commit 9705725

File tree

19 files changed

+38
-30
lines changed

19 files changed

+38
-30
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@
3434
*.class
3535
*bin*
3636
*.project
37+
38+
*.zip

skel/lab01/cpp/task-1/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ int main() {
5454
// * se apeleaza metoda solve()
5555
// (citire, rezolvare, printare)
5656
// * se distruge obiectul si se elibereaza memoria
57-
auto* task = new (std::nothrow) Task{}; // hint: cppreference/nothrow
57+
auto* task = new (std::nothrow) Task(); // hint: cppreference/nothrow
5858
if (!task) {
5959
std::cerr << "new failed: WTF are you doing? Throw your PC!\n";
6060
return -1;

skel/lab01/cpp/task-2/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ int main() {
3737
// * se apeleaza metoda solve()
3838
// (citire, rezolvare, printare)
3939
// * se distruge obiectul si se elibereaza memoria
40-
auto* task = new (std::nothrow) Task{}; // hint: cppreference/nothrow
40+
auto* task = new (std::nothrow) Task(); // hint: cppreference/nothrow
4141
if (!task) {
4242
std::cerr << "new failed: WTF are you doing? Throw your PC!\n";
4343
return -1;

skel/lab01/cpp/task-3/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ int main() {
3838
// * se apeleaza metoda solve()
3939
// (citire, rezolvare, printare)
4040
// * se distruge obiectul si se elibereaza memoria
41-
auto* task = new (std::nothrow) Task{}; // hint: cppreference/nothrow
41+
auto* task = new (std::nothrow) Task(); // hint: cppreference/nothrow
4242
if (!task) {
4343
std::cerr << "new failed: WTF are you doing? Throw your PC!\n";
4444
return -1;

skel/lab01/cpp/task-4/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ int main() {
3838
// * se apeleaza metoda solve()
3939
// (citire, rezolvare, printare)
4040
// * se distruge obiectul si se elibereaza memoria
41-
auto* task = new (std::nothrow) Task{}; // hint: cppreference/nothrow
41+
auto* task = new (std::nothrow) Task(); // hint: cppreference/nothrow
4242
if (!task) {
4343
std::cerr << "new failed: WTF are you doing? Throw your PC!\n";
4444
return -1;

skel/lab02/cpp/task-1/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ int main() {
5151
// * se apeleaza metoda solve()
5252
// (citire, rezolvare, printare)
5353
// * se distruge obiectul si se elibereaza memoria
54-
auto* task = new (std::nothrow) Task{}; // hint: cppreference/nothrow
54+
auto* task = new (std::nothrow) Task(); // hint: cppreference/nothrow
5555
if (!task) {
5656
std::cerr << "new failed: WTF are you doing? Throw your PC!\n";
5757
return -1;

skel/lab02/cpp/task-2/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ int main() {
4343
// * se apeleaza metoda solve()
4444
// (citire, rezolvare, printare)
4545
// * se distruge obiectul si se elibereaza memoria
46-
auto* task = new (std::nothrow) Task{}; // hint: cppreference/nothrow
46+
auto* task = new (std::nothrow) Task(); // hint: cppreference/nothrow
4747
if (!task) {
4848
std::cerr << "new failed: WTF are you doing? Throw your PC!\n";
4949
return -1;

skel/lab02/cpp/task-3/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ int main() {
5252
// * se apeleaza metoda solve()
5353
// (citire, rezolvare, printare)
5454
// * se distruge obiectul si se elibereaza memoria
55-
auto* task = new (std::nothrow) Task{}; // hint: cppreference/nothrow
55+
auto* task = new (std::nothrow) Task(); // hint: cppreference/nothrow
5656
if (!task) {
5757
std::cerr << "new failed: WTF are you doing? Throw your PC!\n";
5858
return -1;

skel/lab03/cpp/task-1/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int main() {
4444
// * se apeleaza metoda solve()
4545
// (citire, rezolvare, printare)
4646
// * se distruge obiectul si se elibereaza memoria
47-
auto* task = new (std::nothrow) Task{}; // hint: cppreference/nothrow
47+
auto* task = new (std::nothrow) Task(); // hint: cppreference/nothrow
4848
if (!task) {
4949
std::cerr << "new failed: WTF are you doing? Throw your PC!\n";
5050
return -1;

skel/lab03/cpp/task-2/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ int main() {
7070
// * se apeleaza metoda solve()
7171
// (citire, rezolvare, printare)
7272
// * se distruge obiectul si se elibereaza memoria
73-
auto* task = new (std::nothrow) Task{}; // hint: cppreference/nothrow
73+
auto* task = new (std::nothrow) Task(); // hint: cppreference/nothrow
7474
if (!task) {
7575
std::cerr << "new failed: WTF are you doing? Throw your PC!\n";
7676
return -1;

skel/lab04/cpp/task-1/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ int main() {
5050
// * se apeleaza metoda solve()
5151
// (citire, rezolvare, printare)
5252
// * se distruge obiectul si se elibereaza memoria
53-
auto* task = new (std::nothrow) Task{}; // hint: cppreference/nothrow
53+
auto* task = new (std::nothrow) Task(); // hint: cppreference/nothrow
5454
if (!task) {
5555
std::cerr << "new failed: WTF are you doing? Throw your PC!\n";
5656
return -1;

skel/lab04/cpp/task-2/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int main() {
4747
// * se apeleaza metoda solve()
4848
// (citire, rezolvare, printare)
4949
// * se distruge obiectul si se elibereaza memoria
50-
auto* task = new (std::nothrow) Task{}; // hint: cppreference/nothrow
50+
auto* task = new (std::nothrow) Task(); // hint: cppreference/nothrow
5151
if (!task) {
5252
std::cerr << "new failed: WTF are you doing? Throw your PC!\n";
5353
return -1;

skel/lab05/cpp/task-1/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ int main() {
4949
// * se apeleaza metoda solve()
5050
// (citire, rezolvare, printare)
5151
// * se distruge obiectul si se elibereaza memoria
52-
auto* task = new (std::nothrow) Task{}; // hint: cppreference/nothrow
52+
auto* task = new (std::nothrow) Task(); // hint: cppreference/nothrow
5353
if (!task) {
5454
std::cerr << "new failed: WTF are you doing? Throw your PC!\n";
5555
return -1;

skel/lab05/cpp/task-2/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ int main() {
4848
// * se apeleaza metoda solve()
4949
// (citire, rezolvare, printare)
5050
// * se distruge obiectul si se elibereaza memoria
51-
auto* task = new (std::nothrow) Task{}; // hint: cppreference/nothrow
51+
auto* task = new (std::nothrow) Task(); // hint: cppreference/nothrow
5252
if (!task) {
5353
std::cerr << "new failed: WTF are you doing? Throw your PC!\n";
5454
return -1;

skel/lab05/cpp/task-3/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ int main() {
5454
// * se apeleaza metoda solve()
5555
// (citire, rezolvare, printare)
5656
// * se distruge obiectul si se elibereaza memoria
57-
auto* task = new (std::nothrow) Task{}; // hint: cppreference/nothrow
57+
auto* task = new (std::nothrow) Task(); // hint: cppreference/nothrow
5858
if (!task) {
5959
std::cerr << "new failed: WTF are you doing? Throw your PC!\n";
6060
return -1;

skel/lab05/cpp/task-4/main.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ int main() {
6363
// * se apeleaza metoda solve()
6464
// (citire, rezolvare, printare)
6565
// * se distruge obiectul si se elibereaza memoria
66-
auto* task = new (std::nothrow) Task{}; // hint: cppreference/nothrow
66+
auto* task = new (std::nothrow) Task(); // hint: cppreference/nothrow
6767
if (!task) {
6868
std::cerr << "new failed: WTF are you doing? Throw your PC!\n";
6969
return -1;

tutorial/cpp/cpp.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -181,12 +181,10 @@ class Gigel {
181181
182182
int main() {
183183
Gigel g; // print "Gigel()"
184-
Gigel g2{}; // print "Gigel()"
185-
Gigel g3{10}; // print "Gigel(10)"
184+
Gigel g2(); // print "Gigel()"
186185
Gigel g4(5); // print "Gigel(5)"
187186
}
188187
```
189-
Note: In C++ we call the constructor for a class `Gigel` by using `Gigel()` or `Gigel{}`. It's strongly recommended to only use the `{}` notation in order to avoid some weird compile error (see [Most vexing parse C++](https://en.wikipedia.org/wiki/Most_vexing_parse)).
190188

191189
#### Destructor
192190
A destructor is a **special method** that is called `when` the lifetime of an object ends. The purpose
@@ -268,7 +266,7 @@ A function which may have generic parameter(s) or return type.
268266
template<typename T>
269267
T func(...) {
270268
// ...
271-
return T{...};
269+
return T(...);
272270
}
273271
// usage example for int
274272
int x = func<int>();
@@ -296,7 +294,7 @@ void func(K &a, V &v) {
296294
// ...
297295
}
298296
// usage example for int and struct Gigel
299-
func<int, Gigel>(123, Gigel{});
297+
func<int, Gigel>(123, Gigel());
300298
```
301299

302300
```cpp
@@ -359,7 +357,7 @@ std::cout << mc_int.get_member() << "\n"; // print 123
359357

360358
// usage example for class Gigel
361359
MyClass<Gigel> mc_gigel;
362-
mc_gigel.set_member(Gigel{}); // set a Gigel value
360+
mc_gigel.set_member(Gigel()); // set a Gigel value
363361
std::cout << mc_gigel.get_member() << "\n"; // print the Gigel value
364362
```
365363

tutorial/cpp/tldr.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
## PA skel
44
During the PA laboratories, you'll receive a skeleton for each task.
55

6+
NOTE: `should` on this page it's considered to be something like tips & tricks for PA (it may not be applicable in other contexts).
7+
68
```cpp
79
#include <bits/stdc++.h>
810
using namespace std;
@@ -42,7 +44,7 @@ int main() {
4244
// * se apeleaza metoda solve()
4345
// (citire, rezolvare, printare)
4446
// * se distruge obiectul si se elibereaza memoria
45-
auto* task = new (std::nothrow) Task{}; // hint: cppreference/nothrow
47+
auto* task = new (std::nothrow) Task(); // hint: cppreference/nothrow
4648
if (!task) {
4749
std::cerr << "new failed: WTF are you doing? Throw your PC!\n";
4850
return -1;
@@ -75,7 +77,7 @@ Notes:
7577

7678
## C++ tweaks
7779
### IO
78-
You should always use the C++ skel.
80+
You should always use the C++ `skel`.
7981

8082
```cpp
8183
// reading example: assume the input stream is named fin
@@ -112,7 +114,7 @@ You should always use `std::vector<T>`.
112114
// unitialized vector:
113115
// * use this when you don't know the size
114116
// * use push_back and pop_back to insert/remove elements
115-
std::vector<int> v{};
117+
std::vector<int> v;
116118
// read example
117119
for (int i = 0; i < n; ++i) {
118120
int x;
@@ -121,15 +123,15 @@ for (int i = 0; i < n; ++i) {
121123
}
122124

123125
// vector with n zeros (0-indexed)
124-
std::vector<int> v{n, 0}; // use: v[0], v[1], ... , v[n - 1]
126+
std::vector<int> v(n, 0); // use: v[0], v[1], ... , v[n - 1]
125127
// read example
126128
for (int i = 0; i < n; ++i) {
127129
fin >> v[i];
128130
}
129131

130132
// vector with n zeros (1-indexed)
131-
std::vector<int> v{n + 1, 0}; // use: v[1], v[2], ..., v[n] - e.g. for dynamic programming
132-
// note: v[0] it's allocated, but maybe not used
133+
std::vector<int> v(n + 1, 0); // use: v[1], v[2], ..., v[n] - e.g. for dynamic programming
134+
// note: v[0] it's allocated, but not used
133135
// read example:
134136
for (int i = 1; i <= n; ++i) {
135137
fin >> v[i];
@@ -142,7 +144,7 @@ You should always use `std::vector<std::vector<T>>`.
142144
```cpp
143145
// most common usage at PA it's 1-indexed matrix for DP
144146
// n x m matrix filled with zeros
145-
std::vector<std::vector<int>> dp{n + 1, std::vector<int>{m + 1, 0}};
147+
std::vector<std::vector<int>> dp(n + 1, std::vector<int>(m + 1, 0));
146148
// use:
147149
// dp[1][1], dp[1][2], ..., dp[1][m]
148150
// ...

utils/base_check.sh

+8-2
Original file line numberDiff line numberDiff line change
@@ -279,12 +279,18 @@ cleanup() {
279279
done
280280
}
281281

282+
282283
pack() {
284+
set -x
285+
if [[ "${PACK_SOL}" = "yes" ]]; then
286+
PACK_EXCLUDES=
287+
else
288+
PACK_EXCLUDES="--exclude **sol_**_**.cpp --exclude **sol_**_**.java"
289+
fi
283290
skel_dir=$(basename "$(pwd)")
284291
command pushd .. &>/dev/null
285292
zip -FSr "${1}" "${skel_dir}/" \
286-
--exclude **sol_**_**.cpp --exclude **sol_**_**.java \
287-
--exclude **sol_diff.sh --exclude **sol_run.sh \
293+
${PACK_EXCLUDES} \
288294
&>/dev/null && echo "Done packing!"
289295
command popd &>/dev/null
290296
}

0 commit comments

Comments
 (0)