Introduction
In the previous article, we looked into Stacks in Cpp and their implementation. In this article, we will be looking at some of the in-built functions of stacks and accessing elements in a stack.
Accessing Elements to the stack
We access the elements of a stack using the method top().
For example,
#include <iostream>
#include <stack>
using namespace std;
int main() {
// create a stack of strings
stack<string> colors;
// push element into the stack
colors.push("Red");
colors.push("Orange");
colors.push("Blue");
// get top element
string top = colors.top();
cout << "Top Element: " << top;
return 0;
}
Output
Top Element: Blue
In the above example, we have created a stack of strings and added some elements, and accessed them using the top() method.
string top = colors.top();
Getting the size of the stack
We use the size() method to get the size i.e. the number of elements in the stack.
For example,
#include <iostream>
#include <stack>
using namespace std;
int main() {
// create a stack of int
stack<int> prime_nums;
// push elements into the stack
prime_nums.push(2);
prime_nums.push(3);
prime_nums.push(5);
// get the size of the stack
int size = prime_nums.size();
cout << "Size of the stack: " << size;
return 0;
}
Output
Size of the stack: 3
In the above example, we created a stack of integers and added three elements to it. Then we used the size() method to find the number of elements in the stack
prime_nums.size();
Since we added three elements to the stack the method returns 3.
Checking if the stack is Empty
We use the empty() method to check if a stack is empty or not.
This method returns either 0 or 1.
0 – if the stack is not empty
1 – if the stack is empty
For example,
#include <iostream>
#include <stack>
using namespace std;
int main() {
// create a stack of double
stack<double> nums;
cout << "Is the stack empty? ";
// check if the stack is empty
if (nums.empty()) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
cout << "Pushing elements..." << endl;
// push element into the stack
nums.push(2.3);
nums.push(9.7);
cout << "Is the stack empty? ";
// check if the stack is empty
if (nums.empty()) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Output
Is the stack empty? Yes
Pushing elements...
Is the stack empty? No
There are still lots of functions that can be used with stack, some of them are
List of functions of Stack:
stack::top() in C++ STL
stack::empty() and stack::size() in C++ STL
stack::push() and stack::pop() in C++ STL
stack::swap() in C++ STL
stack::emplace() in C++ STL
swap() in Stacks
This function is used to swap the contents of one stack with another stack of the same type but the size may vary.
Syntax :
stackname1.swap(stackname2)
Examples:
contents of the stack from top to bottom are
Input : mystack1 = {4, 3, 2, 1}
mystack2 = {9, 7 ,5, 3}
mystack1.swap(mystack2);
Output : mystack1 = 9, 7, 5, 3
mystack2 = 4, 3, 2, 1
Input : mystack1 = {7, 5, 3, 1}
mystack2 = {8, 6, 4, 2}
mystack1.swap(mystack2);
Output : mystack1 = 8, 6, 4, 2
mystack2 = 7, 5, 3, 1
In the above examples, the functions helped us to swap the contents of one stack to another.
Lets look at the implementation
// CPP program to illustrate
// Implementation of swap() function
#include <stack>
#include <iostream>
using namespace std;
int main()
{
// stack container declaration
stack<int> mystack1;
stack<int> mystack2;
// pushing elements into first stack
mystack1.push(1);
mystack1.push(2);
mystack1.push(3);
mystack1.push(4);
// pushing elements into 2nd stack
mystack2.push(3);
mystack2.push(5);
mystack2.push(7);
mystack2.push(9);
// using swap() function to swap elements of stacks
mystack1.swap(mystack2);
// printing the first stack
cout<<"mystack1 = ";
while (!mystack1.empty()) {
cout<<mystack1.top()<<" ";
mystack1.pop();
}
// printing the second stack
cout<<endl<<"mystack2 = ";
while (!mystack2.empty()) {
cout<<mystack2.top()<<" ";
mystack2.pop();
}
return 0;
}
Output:
mystack1 = 9 7 5 3
mystack2 = 4 3 2 1
Conclusion
That is all for this article we learn more about the STL containers in the coming ones.
If you have any queries regarding stacks, leave a comment below.
Happy Coding!