From 03f151f91be6e860fba81c044dccc5ae4393c9be Mon Sep 17 00:00:00 2001 From: Maciej Cieplucha Date: Sun, 9 Mar 2025 18:07:09 +0100 Subject: [PATCH 1/8] create files --- homework/vector-of-shared-ptrs/main.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homework/vector-of-shared-ptrs/main.cpp b/homework/vector-of-shared-ptrs/main.cpp index b9f75ffd..518fac37 100644 --- a/homework/vector-of-shared-ptrs/main.cpp +++ b/homework/vector-of-shared-ptrs/main.cpp @@ -3,12 +3,12 @@ #include "vectorFunctions.hpp" int main() { - auto vec = generate(10); + /*auto vec = generate(10); print(vec); add10(vec); print(vec); sub10(vec); - print(vec); + print(vec);*/ return 0; } From 816cd9648c2575277c12eea3b48cda0524be8022 Mon Sep 17 00:00:00 2001 From: Maciej Cieplucha Date: Sun, 9 Mar 2025 23:33:01 +0100 Subject: [PATCH 2/8] implementation of the vector generating function --- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 8 ++++++++ homework/vector-of-shared-ptrs/vectorFunctions.hpp | 5 +++++ 2 files changed, 13 insertions(+) create mode 100644 homework/vector-of-shared-ptrs/vectorFunctions.cpp create mode 100644 homework/vector-of-shared-ptrs/vectorFunctions.hpp diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp new file mode 100644 index 00000000..58acffdf --- /dev/null +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -0,0 +1,8 @@ +std::vector> generate(int count) { + std::vector> vec; + for (int i = 0; i < count; i++) { + auto ptr = std::make_shared(i); + vec.push_back(ptr); + } + return vec; +} \ No newline at end of file diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.hpp b/homework/vector-of-shared-ptrs/vectorFunctions.hpp new file mode 100644 index 00000000..1e37f2b0 --- /dev/null +++ b/homework/vector-of-shared-ptrs/vectorFunctions.hpp @@ -0,0 +1,5 @@ +#pragma once +#include +#include + +std::vector> generate(int); \ No newline at end of file From 9795c9b90f575fde7f9ac4b5760863578e240c7d Mon Sep 17 00:00:00 2001 From: Maciej Cieplucha Date: Sun, 9 Mar 2025 23:46:57 +0100 Subject: [PATCH 3/8] implementation of the vector printing function --- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 8 ++++++++ homework/vector-of-shared-ptrs/vectorFunctions.hpp | 3 ++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index 58acffdf..5b11b792 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -1,3 +1,6 @@ +#include +#include +#include std::vector> generate(int count) { std::vector> vec; for (int i = 0; i < count; i++) { @@ -5,4 +8,9 @@ std::vector> generate(int count) { vec.push_back(ptr); } return vec; +} + +void print(std::vector> vector) { + for(auto element : vector) + std::cout << *element << " "; } \ No newline at end of file diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.hpp b/homework/vector-of-shared-ptrs/vectorFunctions.hpp index 1e37f2b0..2a96f41d 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.hpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.hpp @@ -2,4 +2,5 @@ #include #include -std::vector> generate(int); \ No newline at end of file +std::vector> generate(int); +void print(std::vector>); \ No newline at end of file From 78aa0ed9ea39e8e89e52078816dfabaf60c92fa2 Mon Sep 17 00:00:00 2001 From: Maciej Cieplucha Date: Sun, 9 Mar 2025 23:55:42 +0100 Subject: [PATCH 4/8] function adding 10 to all elements of a vector --- homework/vector-of-shared-ptrs/main.cpp | 4 ++-- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 5 +++++ homework/vector-of-shared-ptrs/vectorFunctions.hpp | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/homework/vector-of-shared-ptrs/main.cpp b/homework/vector-of-shared-ptrs/main.cpp index 518fac37..6835dacc 100644 --- a/homework/vector-of-shared-ptrs/main.cpp +++ b/homework/vector-of-shared-ptrs/main.cpp @@ -3,9 +3,9 @@ #include "vectorFunctions.hpp" int main() { - /*auto vec = generate(10); + auto vec = generate(10); print(vec); - add10(vec); + /*add10(vec); print(vec); sub10(vec); print(vec);*/ diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index 5b11b792..dd55ff17 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -13,4 +13,9 @@ std::vector> generate(int count) { void print(std::vector> vector) { for(auto element : vector) std::cout << *element << " "; +} + +void add10(std::vector> vector) { + for (auto element : vector) + *element += 10; } \ No newline at end of file diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.hpp b/homework/vector-of-shared-ptrs/vectorFunctions.hpp index 2a96f41d..f1d9c7c8 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.hpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.hpp @@ -3,4 +3,5 @@ #include std::vector> generate(int); -void print(std::vector>); \ No newline at end of file +void print(std::vector>); +void add10(std::vector>); \ No newline at end of file From 8be775162175c8966155574b8c6d80721105a56d Mon Sep 17 00:00:00 2001 From: Maciej Cieplucha Date: Mon, 10 Mar 2025 00:03:29 +0100 Subject: [PATCH 5/8] function that subtracts 10 from an element --- homework/vector-of-shared-ptrs/main.cpp | 4 ++-- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 4 ++++ homework/vector-of-shared-ptrs/vectorFunctions.hpp | 3 ++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/homework/vector-of-shared-ptrs/main.cpp b/homework/vector-of-shared-ptrs/main.cpp index 6835dacc..b986db4c 100644 --- a/homework/vector-of-shared-ptrs/main.cpp +++ b/homework/vector-of-shared-ptrs/main.cpp @@ -5,9 +5,9 @@ int main() { auto vec = generate(10); print(vec); - /*add10(vec); + add10(vec); print(vec); - sub10(vec); + /*sub10(vec); print(vec);*/ return 0; diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index dd55ff17..5ceb59a2 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -18,4 +18,8 @@ void print(std::vector> vector) { void add10(std::vector> vector) { for (auto element : vector) *element += 10; +} + +void sub10(int* const element) { + *element -= 10; } \ No newline at end of file diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.hpp b/homework/vector-of-shared-ptrs/vectorFunctions.hpp index f1d9c7c8..76450ea4 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.hpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.hpp @@ -4,4 +4,5 @@ std::vector> generate(int); void print(std::vector>); -void add10(std::vector>); \ No newline at end of file +void add10(std::vector>); +void sub10(int* const); \ No newline at end of file From e0aefcfa95ed500b9076dbe83e9ceaa57564ba29 Mon Sep 17 00:00:00 2001 From: Maciej Cieplucha Date: Mon, 10 Mar 2025 00:11:04 +0100 Subject: [PATCH 6/8] function that subtracts 10 from all elements of vector --- homework/vector-of-shared-ptrs/main.cpp | 4 ++-- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 13 +++++++++---- homework/vector-of-shared-ptrs/vectorFunctions.hpp | 3 ++- 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/homework/vector-of-shared-ptrs/main.cpp b/homework/vector-of-shared-ptrs/main.cpp index b986db4c..b9f75ffd 100644 --- a/homework/vector-of-shared-ptrs/main.cpp +++ b/homework/vector-of-shared-ptrs/main.cpp @@ -7,8 +7,8 @@ int main() { print(vec); add10(vec); print(vec); - /*sub10(vec); - print(vec);*/ + sub10(vec); + print(vec); return 0; } diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index 5ceb59a2..516025e1 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -10,16 +10,21 @@ std::vector> generate(int count) { return vec; } -void print(std::vector> vector) { - for(auto element : vector) +void print(std::vector> vec) { + for(auto element : vec) std::cout << *element << " "; } -void add10(std::vector> vector) { - for (auto element : vector) +void add10(std::vector> vec) { + for (auto element : vec) *element += 10; } void sub10(int* const element) { *element -= 10; +} + +void sub10(std::vector> vec) { + for (auto element : vec) + sub10(element.get()); } \ No newline at end of file diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.hpp b/homework/vector-of-shared-ptrs/vectorFunctions.hpp index 76450ea4..b243e379 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.hpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.hpp @@ -5,4 +5,5 @@ std::vector> generate(int); void print(std::vector>); void add10(std::vector>); -void sub10(int* const); \ No newline at end of file +void sub10(int* const); +void sub10(std::vector>); \ No newline at end of file From 5a9bf913d4104af20792dd2daf71b1eb405af2ad Mon Sep 17 00:00:00 2001 From: Maciej Cieplucha Date: Mon, 10 Mar 2025 00:18:07 +0100 Subject: [PATCH 7/8] checking if the pointer is not nullptr fix --- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index 516025e1..b351844a 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -17,11 +17,13 @@ void print(std::vector> vec) { void add10(std::vector> vec) { for (auto element : vec) - *element += 10; + if(element!=nullptr) + *element += 10; } void sub10(int* const element) { - *element -= 10; + if (element != nullptr) + *element -= 10; } void sub10(std::vector> vec) { From f4c9a26f6384c6823ba9507eac8ed3df1bc4513a Mon Sep 17 00:00:00 2001 From: Maciej Cieplucha Date: Mon, 10 Mar 2025 00:19:14 +0100 Subject: [PATCH 8/8] formatting fixes --- .../vector-of-shared-ptrs/vectorFunctions.cpp | 34 +++++++++---------- .../vector-of-shared-ptrs/vectorFunctions.hpp | 2 +- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index b351844a..453a6001 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -1,32 +1,32 @@ -#include -#include #include +#include +#include std::vector> generate(int count) { - std::vector> vec; - for (int i = 0; i < count; i++) { - auto ptr = std::make_shared(i); - vec.push_back(ptr); - } - return vec; + std::vector> vec; + for (int i = 0; i < count; i++) { + auto ptr = std::make_shared(i); + vec.push_back(ptr); + } + return vec; } void print(std::vector> vec) { - for(auto element : vec) - std::cout << *element << " "; + for (auto element : vec) + std::cout << *element << " "; } void add10(std::vector> vec) { - for (auto element : vec) - if(element!=nullptr) - *element += 10; + for (auto element : vec) + if (element != nullptr) + *element += 10; } void sub10(int* const element) { - if (element != nullptr) - *element -= 10; + if (element != nullptr) + *element -= 10; } void sub10(std::vector> vec) { - for (auto element : vec) - sub10(element.get()); + for (auto element : vec) + sub10(element.get()); } \ No newline at end of file diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.hpp b/homework/vector-of-shared-ptrs/vectorFunctions.hpp index b243e379..13cc4ef4 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.hpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.hpp @@ -1,6 +1,6 @@ #pragma once -#include #include +#include std::vector> generate(int); void print(std::vector>);