From 74ae32c459070cd8d6e7b1eb6d2e07995f9d95f7 Mon Sep 17 00:00:00 2001 From: Bartosz Rybarczyk Date: Fri, 14 Aug 2026 15:18:46 +0200 Subject: [PATCH 1/5] Add shared_ptr vector generation function Create a utility that returns a vector of shared pointers initialized from 0 to count-1, with safe handling of non-positive counts. --- homework/vector-of-shared-ptrs/main.cpp | 5 ++++- homework/vector-of-shared-ptrs/test.cpp | 3 ++- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 14 ++++++++++++++ homework/vector-of-shared-ptrs/vectorFunctions.hpp | 5 +++++ 4 files changed, 25 insertions(+), 2 deletions(-) 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/main.cpp b/homework/vector-of-shared-ptrs/main.cpp index b9f75ffd..2100cf0c 100644 --- a/homework/vector-of-shared-ptrs/main.cpp +++ b/homework/vector-of-shared-ptrs/main.cpp @@ -4,11 +4,14 @@ int main() { auto vec = generate(10); + /* print(vec); add10(vec); print(vec); sub10(vec); print(vec); - +*/ return 0; + } + diff --git a/homework/vector-of-shared-ptrs/test.cpp b/homework/vector-of-shared-ptrs/test.cpp index 277090b4..ee7b67cd 100644 --- a/homework/vector-of-shared-ptrs/test.cpp +++ b/homework/vector-of-shared-ptrs/test.cpp @@ -24,7 +24,7 @@ TEST_F(Homework, ShouldGenerateVectorOfProperSize) { EXPECT_EQ(*result[i], i); } } - +/* TEST_F(Homework, ShouldAdd10ToEachElement) { auto result = generate(count); add10(result); @@ -85,3 +85,4 @@ TEST_F(Homework, BehavioralTest) { EXPECT_EQ(vec[i], copyVec[i]); } } +*/ \ No newline at end of file diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp new file mode 100644 index 00000000..cc9762fa --- /dev/null +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -0,0 +1,14 @@ +#include "vectorFunctions.hpp" + +std::vector> generate(int count) { + std::vector> vec; + if (count <= 0) { + return vec; + } + + vec.reserve(count); + for (auto 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 0b32cc5eb028751afa315b1d82a61a1b1929956e Mon Sep 17 00:00:00 2001 From: Bartosz Rybarczyk Date: Fri, 14 Aug 2026 15:43:15 +0200 Subject: [PATCH 2/5] Add function to print shared_ptr vector --- homework/vector-of-shared-ptrs/main.cpp | 2 +- .../vector-of-shared-ptrs/vectorFunctions.cpp | 16 ++++++++++++++++ .../vector-of-shared-ptrs/vectorFunctions.hpp | 3 ++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/homework/vector-of-shared-ptrs/main.cpp b/homework/vector-of-shared-ptrs/main.cpp index 2100cf0c..a7d088ae 100644 --- a/homework/vector-of-shared-ptrs/main.cpp +++ b/homework/vector-of-shared-ptrs/main.cpp @@ -4,8 +4,8 @@ int main() { auto vec = generate(10); - /* print(vec); + /* add10(vec); print(vec); sub10(vec); diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index cc9762fa..6c482836 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -1,4 +1,5 @@ #include "vectorFunctions.hpp" +#include std::vector> generate(int count) { std::vector> vec; @@ -11,4 +12,19 @@ std::vector> generate(int count) { vec.push_back(std::make_shared(i)); } return vec; +} + +void print(const std::vector>& vec) { + if (vec.empty()) { + std::cout << "vector is empty" << "\n"; + return; + } + + for (const auto& el : vec) { + if (el) { + std::cout << *el << "\n"; + } else { + std::cout << "this element not exist" << "\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..8bd530df 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 count); \ No newline at end of file +std::vector> generate(int count); +void print(const std::vector>& vec); \ No newline at end of file From 665ee847f2b793be7c978963e1ea444ce28e19f4 Mon Sep 17 00:00:00 2001 From: Bartosz Rybarczyk Date: Fri, 14 Aug 2026 15:59:37 +0200 Subject: [PATCH 3/5] Implement add10 utility for vector elements --- homework/vector-of-shared-ptrs/main.cpp | 2 +- homework/vector-of-shared-ptrs/test.cpp | 4 ++-- homework/vector-of-shared-ptrs/vectorFunctions.cpp | 14 ++++++++++++++ homework/vector-of-shared-ptrs/vectorFunctions.hpp | 3 ++- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/homework/vector-of-shared-ptrs/main.cpp b/homework/vector-of-shared-ptrs/main.cpp index a7d088ae..4470dbc6 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); print(vec); +/* sub10(vec); print(vec); */ diff --git a/homework/vector-of-shared-ptrs/test.cpp b/homework/vector-of-shared-ptrs/test.cpp index ee7b67cd..04921c31 100644 --- a/homework/vector-of-shared-ptrs/test.cpp +++ b/homework/vector-of-shared-ptrs/test.cpp @@ -24,7 +24,7 @@ TEST_F(Homework, ShouldGenerateVectorOfProperSize) { EXPECT_EQ(*result[i], i); } } -/* + TEST_F(Homework, ShouldAdd10ToEachElement) { auto result = generate(count); add10(result); @@ -44,7 +44,7 @@ TEST_F(Homework, ShouldNotAdd10WhenNullptr) { EXPECT_EQ(vec[i], nullptr); } } - +/* TEST_F(Homework, ShouldSubtract10ForPtr) { auto ptr = std::make_shared(10); sub10(ptr.get()); diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index 6c482836..5385179c 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -27,4 +27,18 @@ void print(const std::vector>& vec) { std::cout << "this element not exist" << "\n"; } } +} + +void add10(std::vector>& vec) { + if (vec.empty()) { + std::cout << "vector is empty" << "\n"; + return; + } + for (auto& el : vec) { + if (el) { + *el += 10; + } else { + std::cout << "this element not exist" << "\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 8bd530df..38dd55c0 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 count); -void print(const std::vector>& vec); \ No newline at end of file +void print(const std::vector>& vec); +void add10(std::vector>& vec); \ No newline at end of file From 87c07cf1053bd15fb14e2cba29549353741e2aef Mon Sep 17 00:00:00 2001 From: Bartosz Rybarczyk Date: Fri, 14 Aug 2026 16:36:08 +0200 Subject: [PATCH 4/5] Add overloaded sub10 functions - First overload takes a raw pointer and subtracts 10 from its value. - Second overload takes a vector and calls the first sub10 on each element. --- homework/vector-of-shared-ptrs/main.cpp | 5 +--- homework/vector-of-shared-ptrs/test.cpp | 3 +-- .../vector-of-shared-ptrs/vectorFunctions.cpp | 23 +++++++++++++++++++ .../vector-of-shared-ptrs/vectorFunctions.hpp | 4 +++- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/homework/vector-of-shared-ptrs/main.cpp b/homework/vector-of-shared-ptrs/main.cpp index 4470dbc6..b9f75ffd 100644 --- a/homework/vector-of-shared-ptrs/main.cpp +++ b/homework/vector-of-shared-ptrs/main.cpp @@ -7,11 +7,8 @@ int main() { print(vec); add10(vec); print(vec); -/* sub10(vec); print(vec); -*/ - return 0; + return 0; } - diff --git a/homework/vector-of-shared-ptrs/test.cpp b/homework/vector-of-shared-ptrs/test.cpp index 04921c31..277090b4 100644 --- a/homework/vector-of-shared-ptrs/test.cpp +++ b/homework/vector-of-shared-ptrs/test.cpp @@ -44,7 +44,7 @@ TEST_F(Homework, ShouldNotAdd10WhenNullptr) { EXPECT_EQ(vec[i], nullptr); } } -/* + TEST_F(Homework, ShouldSubtract10ForPtr) { auto ptr = std::make_shared(10); sub10(ptr.get()); @@ -85,4 +85,3 @@ TEST_F(Homework, BehavioralTest) { EXPECT_EQ(vec[i], copyVec[i]); } } -*/ \ No newline at end of file diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index 5385179c..156beed5 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -41,4 +41,27 @@ void add10(std::vector>& vec) { std::cout << "this element not exist" << "\n"; } } +} + +void sub10(int* const ptr) { + if (ptr) { + *ptr -= 10; + } else { + std::cout << "pointer is null" << "\n"; + } +} + +void sub10(std::vector>& vec) { + if (vec.empty()) { + std::cout << "vector is empty" << "\n"; + return; + } + + for (auto& el : vec) { + if (el) { + sub10(el.get()); + } else { + std::cout << "this element not exist" << "\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 38dd55c0..f889c78c 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.hpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.hpp @@ -4,4 +4,6 @@ std::vector> generate(int count); void print(const std::vector>& vec); -void add10(std::vector>& vec); \ No newline at end of file +void add10(std::vector>& vec); +void sub10(int * const ptr); +void sub10(std::vector>& vec); \ No newline at end of file From 0139eed2ccd39d9ea1a2f76b2ebf7d30b1342cc7 Mon Sep 17 00:00:00 2001 From: Bartekr100 Date: Fri, 14 Aug 2026 16:42:17 +0200 Subject: [PATCH 5/5] Fix formatting of sub10 function declaration --- homework/vector-of-shared-ptrs/vectorFunctions.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.hpp b/homework/vector-of-shared-ptrs/vectorFunctions.hpp index f889c78c..6adea679 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.hpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.hpp @@ -5,5 +5,5 @@ std::vector> generate(int count); void print(const std::vector>& vec); void add10(std::vector>& vec); -void sub10(int * const ptr); -void sub10(std::vector>& vec); \ No newline at end of file +void sub10(int* const ptr); +void sub10(std::vector>& vec);