forked from learncppnow/9E
-
Notifications
You must be signed in to change notification settings - Fork 0
/
17.8 DequeInsertionsDeletions.cpp
61 lines (48 loc) · 1.46 KB
/
17.8 DequeInsertionsDeletions.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
#include <deque>
#include <iostream>
#include <algorithm>
int main()
{
using namespace std;
// Define a deque of integers
deque<int> intDeque;
// Insert integers at the bottom of the array
intDeque.push_back(3);
intDeque.push_back(4);
intDeque.push_back(5);
// Insert integers at the top of the array
intDeque.push_front(2);
intDeque.push_front(1);
intDeque.push_front(0);
cout << "The contents of the deque after inserting elements ";
cout << "at the top and bottom are:" << endl;
// Display contents on the screen
for(size_t count = 0
; count < intDeque.size()
; ++ count)
{
cout << "Element [" << count << "] = ";
cout << intDeque [count] << endl;
}
cout << endl;
// Erase an element at the top
intDeque.pop_front();
// Erase an element at the bottom
intDeque.pop_back();
cout << "The contents of the deque after erasing an element ";
cout << "from the top and bottom are:" << endl;
// Display contents again: this time using iterators
// if on older compilers, remove auto and uncomment next line
// deque <int>::iterator element;
for(auto element = intDeque.begin()
; element != intDeque.end()
; ++ element)
{
size_t Offset = distance(intDeque.begin(), element);
cout << "Element [" << Offset << "] = " << *element << endl;
}
intDeque.clear();
if(intDeque.empty())
cout << "The container is now empty" << endl;
return 0;
}