-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex9_26.cpp
70 lines (65 loc) · 1.58 KB
/
ex9_26.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
/**
* Exercise 9.26: Using the following definition of ia, copy ia into a vector
* and into a list. Use the single-iterator form of erase to remove the
* elements with odd values from your list and the even values from your
* vector.
* int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
*
* Exercise 9.27: Write a program to find and remove the odd-valued
* elements in a forward_list<int>.
*/
#include "iostream"
#include "vector"
#include "list"
#include "forward_list"
int main() {
const int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
std::vector<int> vi;
std::list<int> li;
std::forward_list<int> fli;
auto fli_it = fli.before_begin();
/// initialization
for (const int &n : ia) {
vi.emplace_back(n);
li.emplace_back(n);
fli.emplace_after(fli_it, n);
if (fli_it == fli.before_begin()) {
fli_it = fli.begin();
continue;
}
++fli_it;
}
for (auto it = vi.begin(); it != vi.cend(); ) {
if (*it % 2) {
++it;
continue;
}
it = vi.erase(it);
}
std::cout << "vector with evens: ";
for (const int &n : vi)
std::cout << n << " ";
std::cout << std::endl;
for (auto it = li.begin(); it != li.cend(); ) {
if (*it % 2) {
it = li.erase(it);
continue;
}
++it;
}
std::cout << "list with odds: ";
for (const int &n : li)
std::cout << n << " ";
std::cout << std::endl;
for (auto cur = fli.begin(), pre = fli.before_begin(); cur != fli.cend();) {
if (*cur % 2) {
cur = fli.erase_after(pre);
continue;
}
pre = cur++;
}
std::cout << "forward_list with evens: ";
for (const int &n : fli)
std::cout << n << " ";
std::cout << std::endl;
}