-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack_string.cpp
More file actions
48 lines (38 loc) · 1 KB
/
Copy pathstack_string.cpp
File metadata and controls
48 lines (38 loc) · 1 KB
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
#include <iostream>
#include <vector>
#include <stack>
using namespace std;
int main()
{
stack<string> myStack;
myStack.push(" Hehe ");
myStack.push(" Fras ");
myStack.push(" Is ");
myStack.push(" Name ");
myStack.push(" My ");
cout << "Top element is" << myStack.top() << endl;
// removes the element that was most recently added (the top element)
myStack.pop();
// removes Hehe (top element)
cout << "Top element is" << myStack.top() << endl;
// Pushing more elements
myStack.push(" Hi! ");
cout << "Top element is" << myStack.top() << endl;
// Check if the stack is empty
if (myStack.empty())
{
std::cout << "The stack is empty." << std::endl;
}
else
{
std::cout << "The stack is not empty." << std::endl;
}
// Print all elements in the stack
std::cout << "Elements in the stack: ";
while (!myStack.empty())
{
std::cout << myStack.top() << " ";
myStack.pop();
}
return 0;
}