From 17fceeefd0dbc34df283e54d72967a500edfc540 Mon Sep 17 00:00:00 2001 From: Kajetan Date: Sat, 18 Oct 2025 23:39:26 +0000 Subject: [PATCH 01/11] Add generate function --- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 10 ++++++++++ homework/vector-of-shared-ptrs/vectorFunctions.hpp | 5 +++++ 2 files changed, 15 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..2640b7b4 --- /dev/null +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -0,0 +1,10 @@ +#include +#include "vectorFunctions.hpp" + +std::vector> generate(int count) { + std::vector> vec; + for (int i{0}; i < count; ++i) { + vec.push_back(std::make_shared(i)); + } + 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..df5a32e4 --- /dev/null +++ b/homework/vector-of-shared-ptrs/vectorFunctions.hpp @@ -0,0 +1,5 @@ +#pragma once +#include +#include + +std::vector> generate(int count); \ No newline at end of file From da67a9edb51a87f8f87fa32a3f6acd8dfc1e4228 Mon Sep 17 00:00:00 2001 From: Kajetan Date: Sat, 18 Oct 2025 23:40:13 +0000 Subject: [PATCH 02/11] Add print function --- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 8 ++++++++ homework/vector-of-shared-ptrs/vectorFunctions.hpp | 4 +++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index 2640b7b4..d3a21b22 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -1,3 +1,4 @@ +#include #include #include "vectorFunctions.hpp" @@ -7,4 +8,11 @@ std::vector> generate(int count) { vec.push_back(std::make_shared(i)); } return vec; +} + +void print(std::vector> vec) { + std::cout << "Vector elements: "; + for (const auto& el : vec) { + std::cout << '\t' << *el << '\n'; + } } \ 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 df5a32e4..5717032f 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.hpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.hpp @@ -2,4 +2,6 @@ #include #include -std::vector> generate(int count); \ No newline at end of file +std::vector> generate(int count); + +void print(std::vector> vec); \ No newline at end of file From f089dfed3675defd5ad0f3ccd96177f4fb6d3442 Mon Sep 17 00:00:00 2001 From: Kajetan Date: Sat, 18 Oct 2025 23:43:55 +0000 Subject: [PATCH 03/11] Fix passing argument to print function --- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 2 +- homework/vector-of-shared-ptrs/vectorFunctions.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index d3a21b22..345bcc8c 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -10,7 +10,7 @@ std::vector> generate(int count) { return vec; } -void print(std::vector> vec) { +void print(std::vector>& vec) { std::cout << "Vector elements: "; for (const auto& el : vec) { std::cout << '\t' << *el << '\n'; diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.hpp b/homework/vector-of-shared-ptrs/vectorFunctions.hpp index 5717032f..7884dcb1 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.hpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.hpp @@ -4,4 +4,4 @@ std::vector> generate(int count); -void print(std::vector> vec); \ No newline at end of file +void print(std::vector>& vec); \ No newline at end of file From e2669740be72a8f54e1647db0804902fa8a96d77 Mon Sep 17 00:00:00 2001 From: Kajetan Date: Sun, 19 Oct 2025 00:01:55 +0000 Subject: [PATCH 04/11] Add all others function headers in vectorFunctions.hpp --- homework/vector-of-shared-ptrs/vectorFunctions.hpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.hpp b/homework/vector-of-shared-ptrs/vectorFunctions.hpp index 7884dcb1..c78b70f7 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.hpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.hpp @@ -4,4 +4,10 @@ std::vector> generate(int count); -void print(std::vector>& vec); \ No newline at end of file +void print(std::vector>& vec); + +void add10(std::vector>& vec); + +void sub10(int* const ptr); + +void sub10(std::vector>& vec); \ No newline at end of file From c40c42ce1e65adc21c2a820c7e1ab42cc869b2e4 Mon Sep 17 00:00:00 2001 From: Kajetan Date: Sun, 19 Oct 2025 14:40:39 +0000 Subject: [PATCH 05/11] Add all others function definitions in vectorFunctions.cpp as empty --- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index 345bcc8c..ff65d8da 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -15,4 +15,16 @@ void print(std::vector>& vec) { for (const auto& el : vec) { std::cout << '\t' << *el << '\n'; } +} + +void add10(std::vector>& vec) { + +} + +void sub10(int* const ptr) { + +} + +void sub10(std::vector>& vec) { + } \ No newline at end of file From 6c224f1057751c0ac85a26e7c3c8c4fd0f464ace Mon Sep 17 00:00:00 2001 From: Kajetan Date: Sun, 19 Oct 2025 14:52:39 +0000 Subject: [PATCH 06/11] Fix printing vector elements --- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index ff65d8da..1374ddb0 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -11,7 +11,7 @@ std::vector> generate(int count) { } void print(std::vector>& vec) { - std::cout << "Vector elements: "; + std::cout << "Vector elements: " << '\n'; for (const auto& el : vec) { std::cout << '\t' << *el << '\n'; } @@ -26,5 +26,5 @@ void sub10(int* const ptr) { } void sub10(std::vector>& vec) { - + } \ No newline at end of file From 376c004a305e6b67d126facf88e205cc46568512 Mon Sep 17 00:00:00 2001 From: Kajetan Date: Sun, 19 Oct 2025 15:06:00 +0000 Subject: [PATCH 07/11] Implement add10 function --- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index 1374ddb0..f9e13e9c 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -11,14 +11,19 @@ std::vector> generate(int count) { } void print(std::vector>& vec) { - std::cout << "Vector elements: " << '\n'; + std::cout << "Vector elements: \n"; for (const auto& el : vec) { std::cout << '\t' << *el << '\n'; } } void add10(std::vector>& vec) { - + for (auto& el : vec) { + if (el) { + *el += 10; + } + + } } void sub10(int* const ptr) { From a8c9947436aca6536e9825d5c8a2b4758f4959bd Mon Sep 17 00:00:00 2001 From: Kajetan Date: Sun, 19 Oct 2025 15:07:24 +0000 Subject: [PATCH 08/11] Refactor code for good practices --- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index f9e13e9c..1c42ccb2 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -4,7 +4,7 @@ std::vector> generate(int count) { std::vector> vec; - for (int i{0}; i < count; ++i) { + for (size_t i{0}; i < count; ++i) { vec.push_back(std::make_shared(i)); } return vec; @@ -22,7 +22,6 @@ void add10(std::vector>& vec) { if (el) { *el += 10; } - } } From d2e90d4ed9aff4701488dcaf9fd4d172714b01ef Mon Sep 17 00:00:00 2001 From: Kajetan Date: Sun, 19 Oct 2025 15:12:59 +0000 Subject: [PATCH 09/11] Implement sub10 function for raw pointer --- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index 1c42ccb2..55e7fbd3 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -26,7 +26,9 @@ void add10(std::vector>& vec) { } void sub10(int* const ptr) { - + if (ptr) { + *ptr -= 10; + } } void sub10(std::vector>& vec) { From 565fcaa1a24239046b3b7d19efb9649c02e16987 Mon Sep 17 00:00:00 2001 From: Kajetan Date: Sun, 19 Oct 2025 15:13:52 +0000 Subject: [PATCH 10/11] Implement sub10 function for vector of shared pointers --- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index 55e7fbd3..1195f469 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -32,5 +32,7 @@ void sub10(int* const ptr) { } void sub10(std::vector>& vec) { - + for (auto& el : vec) { + sub10(el.get()); + } } \ No newline at end of file From f24e1b6b254824e5350d7e24720eed513ed7d678 Mon Sep 17 00:00:00 2001 From: Kajetan Date: Sun, 19 Oct 2025 15:21:10 +0000 Subject: [PATCH 11/11] Fix code style to clang-format --- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index 1195f469..38a3891d 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -1,6 +1,6 @@ +#include "vectorFunctions.hpp" #include #include -#include "vectorFunctions.hpp" std::vector> generate(int count) { std::vector> vec;