-
Notifications
You must be signed in to change notification settings - Fork 1
/
STL_Derived_container_stack_1.cpp
49 lines (40 loc) · 2.04 KB
/
STL_Derived_container_stack_1.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
/*
LIFO stack
Stacks are a type of container adaptor, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from one end of the container.
stacks are implemented as container adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed/popped from the "back" of the specific container, which is known as the top of the stack.
The underlying container may be any of the standard container class templates or some other specifically designed container class. The container shall support the following operations:
The standard container classes vector, deque and list fulfill these requirements. By default, if no container class is specified for a particular stack class instantiation, the standard container deque is used.
Member functions:
(constructor) Construct stack (public member function)
empty Test whether container is empty (public member function)
size Return size (public member function)
top Access next element (public member function)
push Insert element (public member function)
emplace Construct and insert element (public member function)
pop Remove top element (public member function)
swap Swap contents (public member function)
Non-member function overloads:
relational operators Relational operators for stack (function)
swap (stack) Exchange contents of stacks (public member function)
Non-member class specializations:
uses_allocator<stack> Uses allocator for stack (class template)
*/
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> stack;
stack.push(21);// The values pushed in the stack should be of the same data which is written during declaration of stack
stack.push(22);
stack.push(24);
stack.push(25);
int num=0;
stack.push(num);
stack.pop();
stack.pop();
stack.pop();
while (!stack.empty()) {
cout << stack.top() <<" ";
stack.pop();
}
}