-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy path24.2 StackPushPop.cpp~
44 lines (34 loc) · 1.03 KB
/
24.2 StackPushPop.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
#include <stack>
#include <iostream>
#include <algorithm>
using namespace std;
template <typename T>
void DisplayContents(const T& Input)
{
for_each( Input.cbegin() // auto, cbegin: c++11
, Input.cend() // cend() is new in C++11
, [](const T::value_type& element) { cout << element << ' ';});
cout << "| Number of elements: " << Input.size() << endl;
}
int main()
{
stack<int> stackInts;
stackInts.push(25);
stackInts.push(10);
stackInts.push(-1);
stackInts.push(5);
cout << "Elements in stack are: " << endl;
DisplayContents(stackInts._Get_container());
// pop = remove the topmost element
cout << endl << "Popping them one after another..." << endl;
while(stackInts.size() != 0)
{
cout << "The element at the top is: " << stackInts.top();
cout << endl << "Removing it from the stack!" << endl;
// Remove the topmost element
stackInts.pop();
}
if(stackInts.empty())
cout << endl << "The stack is now empty!";
return 0;
}