-
Notifications
You must be signed in to change notification settings - Fork 0
/
StdListIterator.cpp
101 lines (81 loc) · 2.6 KB
/
StdListIterator.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <list>
#include <unordered_map>
using namespace std;
int main()
{
{
// Insert will happen at the END
// Delete will happen at the FRONT
list<int> myList;
list<int>::iterator it = myList.begin();
unordered_map<int, list<int>::iterator> myListMap;
// Insert 5 elements
myList.insert(it, 10);
myListMap[10] = it;
it = myList.begin();
myList.insert(it, 20);
myListMap[20] = it;
it = myList.begin();
myList.insert(it, 30);
myListMap[30] = it;
it = myList.begin();
myList.insert(it, 40);
myListMap[40] = it;
it = myList.begin();
myList.insert(it, 50);
myListMap[50] = it;
it = myList.begin();
for (list<int>::iterator i1 = myList.begin(); i1 != myList.end(); ++i1)
{
cout << " " << *i1;
}
cout << endl;
// Remove 3 by using the Map
auto itr = myListMap.find(30);
cout << "Itr: " << *(itr->second) << endl;
myList.erase(itr->second);
myListMap.erase(itr);
for (list<int>::iterator i1 = myList.begin(); i1 != myList.end(); ++i1)
{
cout << " " << *i1;
}
cout << endl;
auto itr2 = myListMap.find(10);
myList.splice(myList.begin(), myList, itr2->second);
for (list<int>::iterator i1 = myList.begin(); i1 != myList.end(); ++i1)
{
cout << " " << *i1;
}
cout << endl;
}
{
std::list<int> mylist1, mylist2;
std::list<int>::iterator it;
// set some initial values:
for (int i=1; i<=4; ++i)
mylist1.push_back(i); // mylist1: 1 2 3 4
for (int i=1; i<=3; ++i)
mylist2.push_back(i*10); // mylist2: 10 20 30
it = mylist1.begin();
++it; // points to 2
mylist1.splice (it, mylist2); // mylist1: 1 10 20 30 2 3 4
// mylist2 (empty)
// "it" still points to 2 (the 5th element)
mylist2.splice (mylist2.begin(),mylist1, it);
std::cout << "mylist1 contains:";
for (it=mylist1.begin(); it!=mylist1.end(); ++it)
{
std::cout << ' ' << *it;
}
std::cout << '\n';
std::cout << "mylist2 contains:";
for (it=mylist2.begin(); it!=mylist2.end(); ++it)
{
std::cout << ' ' << *it;
}
std::cout << '\n';
}
cout << endl;
return 0;
}