forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex3_16.cpp
73 lines (63 loc) · 1.98 KB
/
ex3_16.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
//
//@Author PEZY
//@Date Aug. 2014
//@Brief
// print the size and contents of the vectors from exercise 3.13.
// Check whether your answers to that exercise were correct.
// If not, restudy § 3.3.1 (p. 97) until you understand why you were wrong.
//
#include <iostream>
#include <vector>
#include <string>
using std::vector;
using std::string;
using std::cout;
using std::endl;
int main()
{
vector<int> v1;
cout << "{\n \"v1\":{\"size\":\"" << v1.size() << "\",\"value\":[";
for (auto i : v1)
cout << i << ",";
if (!v1.empty()) cout << "\b";
cout << "]}" << endl;
vector<int> v2(10);
cout << " \"v2\":{\"size\":\"" << v2.size() << "\",\"value\":[";
for (auto i : v2)
cout << i << ",";
if (!v2.empty()) cout << "\b";
cout << "]}" << endl;
vector<int> v3(10, 42);
cout << " \"v3\":{\"size\":\"" << v3.size() << "\",\"value\":[";
for (auto i : v3)
cout << i << ",";
if (!v3.empty()) cout << "\b";
cout << "]}" << endl;
vector<int> v4{ 10 };
cout << " \"v4\":{\"size\":\"" << v4.size() << "\",\"value\":[";
for (auto i : v4)
cout << i << ",";
if (!v4.empty()) cout << "\b";
cout << "]}" << endl;
vector<int> v5{ 10, 42 };
cout << " \"v5\":{\"size\":\"" << v5.size() << "\",\"value\":[";
for (auto i : v5)
cout << i << ",";
if (!v5.empty()) cout << "\b";
cout << "]}" << endl;
vector<string> v6{ 10 };
cout << " \"v6\":{\"size\":\"" << v6.size() << "\",\"value\":[";
for (auto i : v6)
if (i.empty()) cout << "(null)" << ",";
else cout << i << ",";
if (!v6.empty()) cout << "\b";
cout << "]}" << endl;
vector<string> v7{ 10, "hi" };
cout << " \"v7\":{\"size\":\"" << v7.size() << "\",\"value\":[";
for (auto i : v7)
if (i.empty()) cout << "(null)" << ",";
else cout << i << ",";
if (!v7.empty()) cout << "\b";
cout << "]}\n}" << endl;
return 0;
}