forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex12_06.cpp
69 lines (59 loc) · 1.74 KB
/
ex12_06.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
/***************************************************************************
* @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.
//!
#include <iostream>
#include <vector>
#include <string>
#include <memory>
std::vector<int>* dynamic_vector_generator();
void dynamic_vector_processor(std::vector<int>* ptr_v);
void dynamic_vector_printer(std::vector<int>* ptr_v);
int main()
{
/**
* testing the 3 functions
*/
std::vector<int>* ptr_vi = dynamic_vector_generator();
dynamic_vector_processor(ptr_vi);
dynamic_vector_printer(ptr_vi);
delete ptr_vi;
return 0;
}
/**
* @brief return a pointer to dynamicly allocated vector of ints
*/
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
* @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
* @param ptr_v
*/
void dynamic_vector_printer(std::vector<int>* ptr_v)
{
for (const auto& e : *ptr_v) std::cout << e << " ";
std::cout << "\n";
}