forked from Mooophy/Cpp-Primer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex10_07.cpp
48 lines (42 loc) · 1.15 KB
/
ex10_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
//
// ex10_07.cpp
// Exercise 10.7
//
// Created by pezy on 12/9/14.
//
// @Brief Determine if there are any errors in the following programs and, if so, correct the error(s)
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using std::vector; using std::cout; using std::endl; using std::list; using std::cin; using std::fill_n;
template<typename Sequence>
void print(Sequence const& seq)
{
for (const auto& i : seq)
cout << i << " ";
cout << endl;
}
int main()
{
// (a)
vector<int> vec;
list<int> lst;
int i;
while (cin >> i)
lst.push_back(i);
vec.resize(lst.size());
// ^ Fixed: added this statement
// Cause Algorithms that write to a destination iterator assume
// the destination is large enough to hold the number of elements being written.
copy(lst.cbegin(), lst.cend(), vec.begin());
// (b)
vector<int> v;
v.reserve(10);
fill_n(v.begin(), 10, 0);
// ^ (b)No error, but not any sense. v.size() still equal zero.
// Fixed: 1. use `v.resize(10);`
// or 2. use `fill_n(std::back_inserter(v), 10, 0)`
print(v);
print(vec);
}