-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy path10.07.txt
29 lines (22 loc) · 946 Bytes
/
10.07.txt
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
Exercise 10.7: Determine if there are any errors in the following programs and,
if so, correct the error(s):
(a) vector<int> vec; list<int> lst; int i;
while (cin >> i)
lst.push_back(i);
copy(lst.cbegin(), lst.cend(), vec.begin());
(b) vector<int> vec;
vec.reserve(10); // reserve is covered in § 9.4 (p. 356)
fill_n(vec.begin(), 10, 0);
by Faisal Saadatmand
(a) vec is an empty vector, which means the algorith copy cannot write to it.
The size of lst is determined by the number of inputs from istream, which means
it is only known at runtime. We should use an insert iterator for vec if we
wanted to use copy:
vector<int> vec; list<int> lst; int i;
while (cin >> i)
lst.push_back(i);
copy(lst.cbegin(), lst.cend(), back_inserter(vec));
(b) The operation reserve sets the capacity, NOT the size, of vec to 10. We
would have to declare vec as a vector with size 10:
vector<int> vec(10);
fill_n(vec.begin(), 10, 0);