forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex12_07.cpp
110 lines (95 loc) · 2.78 KB
/
ex12_07.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/***************************************************************************
* @file The code is for the exercises in C++ Primmer 5th Edition
* @author Alan.W
* @date 22 DEC 2013
* @remark
***************************************************************************/
//!
//! Exercise 12.6:
//! Write a function that returns a dynamically allocated vector of ints.
//! Pass that vector to another function that reads the standard input to
//! give values to the elements. Pass the vector to another function to print
//! the values that were read.
//! Remember to delete the vector at the appropriate time.
//!
//! Exercise 12.7:
//! Redo the previous exercise, this time using shared_ptr.
//!
#include <iostream>
#include <vector>
#include <string>
#include <memory>
/**
* @brief functions for ex12.6
*/
std::vector<int>* dynamic_vector_generator();
void dynamic_vector_processor(std::vector<int>* ptr_v);
void dynamic_vector_printer(std::vector<int>* ptr_v);
/**
* @brief functions for ex12.7
*/
std::shared_ptr<std::vector<int>> dynamic_vector_generator_sptr();
void dynamic_vector_processor_sptr(std::shared_ptr<std::vector<int>> sptr_vi);
void dynamic_vector_printer_sptr(
const std::shared_ptr<std::vector<int>> sptr_vi);
int main()
{
auto sptr = dynamic_vector_generator_sptr();
dynamic_vector_processor_sptr(sptr);
dynamic_vector_printer_sptr(sptr);
return 0;
}
/**
* @brief return a pointer to dynamicly allocated vector of ints
* ex12.6
*/
std::vector<int>* dynamic_vector_generator()
{
std::vector<int>* ptr_v = new std::vector<int>();
return ptr_v;
}
/**
* @brief return a pointer to vector of ints
* ex12.6
* @param ptr_v pointer to vector of ints
*/
void dynamic_vector_processor(std::vector<int>* ptr_v)
{
int i;
std::cout << "plz enter:\n";
while (std::cin >> i && i != 999) ptr_v->push_back(i);
}
/**
* @brief print the content of the vector that ptr_v points to
* ex12.6
* @param ptr_v
*/
void dynamic_vector_printer(std::vector<int>* ptr_v)
{
for (const auto& e : *ptr_v) std::cout << e << " ";
std::cout << "\n";
}
/**
* @brief return a shared_prt to vector of ints
*/
std::shared_ptr<std::vector<int>> dynamic_vector_generator_sptr()
{
return std::make_shared<std::vector<int>>();
}
/**
* @brief using std IO adding elelements into the vetor and
* return a shared_ptr pointing to it
* @param sptr_vi
*/
void dynamic_vector_processor_sptr(std::shared_ptr<std::vector<int>> sptr_vi)
{
int i;
std::cout << "plz enter:\n";
while (std::cin >> i && i != 999) sptr_vi->push_back(i);
}
void dynamic_vector_printer_sptr(
const std::shared_ptr<std::vector<int>> sptr_vi)
{
for (const auto& e : *sptr_vi) std::cout << e << " ";
std::cout << "\n";
}