forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex9_26.cpp
72 lines (57 loc) · 1.39 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
71
72
//! @Alan
//!
//! 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.
// note the subtle difference between list and vector.
//!
#include <iostream>
#include <string>
#include <vector>
#include <deque>
#include <list>
int main()
{
int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
//! cal the lenth of the array.
int size = sizeof(ia)/sizeof(int);
std::vector<int> v;
std::list<int> l;
// copy in to v and l
for (auto *p = ia; p != ia + size ; ++p)
{
v.push_back(*p);
l.push_back(*p);
}
// ease even value element from v
for (auto it = v.begin(); it != v.end(); ++it)
{
if((*it)%2 == 0)
{
v.erase(it);
}
}
//print content of v
for(auto it = v.begin(); it != v.end(); ++it)
{
std::cout << *it <<" ";
}
std::cout << "\n";
//ease odd value element from l
for(auto it = l.begin(); it != l.end(); ++it)
{
if((*it)%2 == 1)
{
l.erase(it);
//! @note without this statement it does not work
--it;
}
}
//print l
for (auto it = l.begin(); it != l.end(); ++it)
{
std::cout << *it << " ";
}
return 0;
}