Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vector of shared ptrs #1382

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
42 changes: 42 additions & 0 deletions homework/vector-of-shared-ptrs/vectorFunctions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <iostream>
#include "vectorFunctions.hpp"

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

// Done

void print (const std::vector<std::shared_ptr<int>>& vector){
for (auto& element : vector){
std::cout << element << std::endl;
}
}

// Done

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

// Done

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

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

std::vector<std::shared_ptr<int>> generate (const int count);
void print (const std::vector<std::shared_ptr<int>>& vector);
void add10 (std::vector<std::shared_ptr<int>>&);
void sub10 (int* const ptr);
void sub10 (std::vector<std::shared_ptr<int>>&);
Loading