From 2ca0b799049500b59e41d53b1ddc5efca1bb47aa Mon Sep 17 00:00:00 2001 From: ctrl-daan Date: Wed, 19 Mar 2025 12:22:23 +0100 Subject: [PATCH 1/2] generate, print, add10 functions added --- .../vector-of-shared-ptrs/vectorFunctions.cpp | 35 +++++++++++++++++++ .../vector-of-shared-ptrs/vectorFunctions.hpp | 9 +++++ 2 files changed, 44 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..fd59d270 --- /dev/null +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -0,0 +1,35 @@ +#include "vectorFunctions.hpp" +#include + +std::vector> generate (int count) +{ + std::vector> myVec; + + for (size_t i = 0; i < count; i++) + { + std::shared_ptr ptr = std::make_shared(i); + myVec.push_back(ptr); + } + + return myVec; +} +void print(std::vector> ptr_vec) +{ + for (auto c : ptr_vec) + {std::cout<<"COTO JEST:"<<*c<<"\n";} +} +void add10(std::vector> ptr_vec) +{ + for (auto c : ptr_vec) + if (c != nullptr) + *c += 10; + +} +void sub10(int*ptr ) +{ + +} +void sub10(std::vector> ptr_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..6b333086 --- /dev/null +++ b/homework/vector-of-shared-ptrs/vectorFunctions.hpp @@ -0,0 +1,9 @@ +#pragma once +#include +#include + +std::vector> generate (int count); +void print(std::vector>); +void add10(std::vector>); +void sub10(const int *ptr); +void sub10(std::vector>); \ No newline at end of file From bc7667f8070753a3714ef9b6d754b834ca26a31e Mon Sep 17 00:00:00 2001 From: ctrl-daan Date: Wed, 19 Mar 2025 12:28:21 +0100 Subject: [PATCH 2/2] sub10 function implemented, all tests are passing, code refractored --- .../vector-of-shared-ptrs/vectorFunctions.cpp | 38 +++++++++---------- .../vector-of-shared-ptrs/vectorFunctions.hpp | 5 ++- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp index fd59d270..e9c1fb15 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.cpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -1,35 +1,35 @@ #include "vectorFunctions.hpp" #include -std::vector> generate (int count) -{ +std::vector> generate(int count) { std::vector> myVec; - - for (size_t i = 0; i < count; i++) - { + for (size_t i = 0; i < count; i++) { std::shared_ptr ptr = std::make_shared(i); myVec.push_back(ptr); } - + return myVec; } -void print(std::vector> ptr_vec) -{ - for (auto c : ptr_vec) - {std::cout<<"COTO JEST:"<<*c<<"\n";} + +void print(std::vector> ptr_vec) { + for (auto c : ptr_vec) { + std::cout << *c << "\n"; + } } -void add10(std::vector> ptr_vec) -{ + +void add10(std::vector> ptr_vec) { for (auto c : ptr_vec) - if (c != nullptr) + if (c != nullptr) { *c += 10; - + } } -void sub10(int*ptr ) -{ +void sub10(int* ptr) { + if (ptr != nullptr) + *ptr -= 10; } -void sub10(std::vector> ptr_vec) -{ -} \ No newline at end of file +void sub10(std::vector> ptr_vec) { + for (auto c : ptr_vec) + sub10(c.get()); +} diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.hpp b/homework/vector-of-shared-ptrs/vectorFunctions.hpp index 6b333086..dca14c30 100644 --- a/homework/vector-of-shared-ptrs/vectorFunctions.hpp +++ b/homework/vector-of-shared-ptrs/vectorFunctions.hpp @@ -5,5 +5,6 @@ std::vector> generate (int count); void print(std::vector>); void add10(std::vector>); -void sub10(const int *ptr); -void sub10(std::vector>); \ No newline at end of file +void sub10(int *ptr); +void sub10(std::vector>); +