diff --git a/homework/vector-of-shared-ptrs/vectorFunctions.cpp b/homework/vector-of-shared-ptrs/vectorFunctions.cpp new file mode 100644 index 00000000..156beed5 --- /dev/null +++ b/homework/vector-of-shared-ptrs/vectorFunctions.cpp @@ -0,0 +1,67 @@ +#include "vectorFunctions.hpp" +#include + +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; +} + +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"; + } + } +} + +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"; + } + } +} + +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 new file mode 100644 index 00000000..6adea679 --- /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(const std::vector>& vec); +void add10(std::vector>& vec); +void sub10(int* const ptr); +void sub10(std::vector>& vec);