Skip to content
38 changes: 38 additions & 0 deletions homework/vector-of-shared-ptrs/vectorFunctions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "vectorFunctions.hpp"
#include <iostream>
#include <memory>

std::vector<std::shared_ptr<int>> generate(int count) {
std::vector<std::shared_ptr<int>> vec;
for (size_t i{0}; i < count; ++i) {
vec.push_back(std::make_shared<int>(i));
}
return vec;
}

void print(std::vector<std::shared_ptr<int>>& vec) {
std::cout << "Vector elements: \n";
for (const auto& el : vec) {
std::cout << '\t' << *el << '\n';
}
}

void add10(std::vector<std::shared_ptr<int>>& vec) {
for (auto& el : vec) {
if (el) {
*el += 10;
}
}
}

void sub10(int* const ptr) {
if (ptr) {
*ptr -= 10;
}
}

void sub10(std::vector<std::shared_ptr<int>>& vec) {
for (auto& el : vec) {
sub10(el.get());
}
}
13 changes: 13 additions & 0 deletions homework/vector-of-shared-ptrs/vectorFunctions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once
#include <memory>
#include <vector>

std::vector<std::shared_ptr<int>> generate(int count);

void print(std::vector<std::shared_ptr<int>>& vec);

void add10(std::vector<std::shared_ptr<int>>& vec);

void sub10(int* const ptr);

void sub10(std::vector<std::shared_ptr<int>>& vec);