forked from learncppnow/9E
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18.3 ListInsert.cpp
58 lines (43 loc) · 1.51 KB
/
18.3 ListInsert.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
#include <list>
#include <iostream>
using namespace std;
template <typename T>
void DisplayContents(const T& container)
{
for(auto element = container.cbegin();
element != container.cend();
++ element )
cout << *element << ' ';
cout << endl;
}
int main()
{
list<int> linkInts1;
// Inserting elements at the beginning...
linkInts1.insert(linkInts1.begin(), 2);
linkInts1.insert(linkInts1.begin(), 1);
// Inserting an element at the end...
linkInts1.insert(linkInts1.end(), 3);
cout << "The contents of list 1 after inserting elements:" << endl;
DisplayContents(linkInts1);
list<int> linkInts2;
// Inserting 4 elements of the same value 0...
linkInts2.insert(linkInts2.begin(), 4, 0);
cout << "The contents of list 2 after inserting '";
cout << linkInts2.size() << "' elements of a value:" << endl;
DisplayContents(linkInts2);
list<int> linkInts3;
// Inserting elements from another list at the beginning...
linkInts3.insert(linkInts3.begin(),
linkInts1.begin(), linkInts1.end());
cout << "The contents of list 3 after inserting the contents of ";
cout << "list 1 at the beginning:" << endl;
DisplayContents(linkInts3);
// Inserting elements from another list at the end...
linkInts3.insert(linkInts3.end(),
linkInts2.begin(), linkInts2.end());
cout << "The contents of list 3 after inserting ";
cout << "the contents of list 2 at the end:" << endl;
DisplayContents(linkInts3);
return 0;
}