Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions homework/vector-of-shared-ptrs/vectorFunctions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "vectorFunctions.hpp"

#include <iostream>

std::vector<std::shared_ptr<int>> generate(int count) {
std::vector<std::shared_ptr<int>> result;
result.reserve(count);

for (size_t i = 0; i < count; ++i) {
result.push_back(std::make_shared<int>(i));
}

return result;
}

void print(const std::vector<std::shared_ptr<int>>& vec) {
for (const auto& ptr : vec) {
std::cout << *ptr << ' ';
}

std::cout << '\n';
}
void add10(std::vector<std::shared_ptr<int>>& vec) {
for (const auto& ptr : vec) {
if (ptr) {
*ptr += 10;
}
}
}

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

void sub10(std::vector<std::shared_ptr<int>>& vec) {
for (const auto& ptr : vec) {
sub10(ptr.get());
}
}
14 changes: 14 additions & 0 deletions homework/vector-of-shared-ptrs/vectorFunctions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef VECTOR_FUNCTIONS_H
#define VECTOR_FUNCTIONS_H

#include <memory>
#include <vector>

std::vector<std::shared_ptr<int>> generate(int count);
void print(const 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);

#endif
Loading