forked from Bhupesh-V/30-seconds-of-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swap.cpp
45 lines (39 loc) · 1.06 KB
/
swap.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
// Style Guide => https://github.com/Bhupesh-V/30-Seconds-Of-STL/blob/master/CONTRIBUTING.md/#Style Guide
/*
Author : Soumya Lahiri
Date : 01/10/2019
Time : 01:00 AM
Description : swap() function is used to swap the contents of one stack with another stack of same type and size.
*/
#include <stack>
#include <iostream>
int main() {
// stack container declaration
std::stack<int> stackOne;
std::stack<int> stackTwo;
// pushing elements into first stack
stackOne.push(1);
stackOne.push(2);
stackOne.push(3);
stackOne.push(4);
// pushing elements into 2nd stack
stackTwo.push(3);
stackTwo.push(5);
stackTwo.push(7);
stackTwo.push(9);
// using swap() function to swap elements of stacks
stackOne.swap(stackTwo);
// printing the first stack
std::cout << "stackOne = ";
while (!stackOne.empty()) {
std::cout << stackOne.top() << " ";
stackOne.pop();
}
// printing the second stack
std::cout << std::endl <<"stackTwo = ";
while (!stackTwo.empty()) {
std::cout << stackTwo.top() << " ";
stackTwo.pop();
}
return 0;
}