forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex3_21.cpp
61 lines (54 loc) · 1.22 KB
/
ex3_21.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
///
/// Redo the first exercise from § 3.3.3 (p. 105) using iterators.
///
#include <vector>
#include <iterator>
#include <string>
#include <iostream>
using std::vector;
using std::string;
using std::cout;
using std::endl;
void check(const vector<int>& vec)
{
if (vec.empty())
cout << "size: 0; no values." << endl;
else {
cout << "size: " << vec.size() << "; values:";
for (auto it = vec.begin(); it != vec.end(); ++it) cout << *it << ",";
cout << "\b." << endl;
}
}
void check(const vector<string>& vec)
{
if (vec.empty())
cout << "size: 0; no values." << endl;
else {
cout << "size: " << vec.size() << "; values:";
for (auto it = vec.begin(); it != vec.end(); ++it)
if (it->empty())
cout << "(null)"
<< ",";
else
cout << *it << ",";
cout << "\b." << endl;
}
}
int main()
{
vector<int> v1;
vector<int> v2(10);
vector<int> v3(10, 42);
vector<int> v4{10};
vector<int> v5{10, 42};
vector<string> v6{10};
vector<string> v7{10, "hi"};
check(v1);
check(v2);
check(v3);
check(v4);
check(v5);
check(v6);
check(v7);
return 0;
}