English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

C++ Stack (Stack)

In the field of computer science, we are committed to various programs. Each of them has its own domain and utility. Depending on the purpose and environment of program creation, we have a large number of data structures to choose from. One of them is 'stack'. Before discussing this data type, let's take a look at its syntax.

Syntax

template<class T, class Container = deque<T> > class stack;

This data structure uses LIFO technology, where LIFO stands for Last In First Out. The first element inserted will be extracted from the end, and so on. There is an element named 'top', which is the element at the top position. All insertion and deletion operations are performed on the element itself at the top of the stack.

The stack implied in the application area is a container adapter.

The container should support the following list of operations:

  • empty

  • size

  • back

  • push_back

  • pop_back

Template parameters

T: The parameter specifies the type of elements that the container adapter will retain.

Container: The parameter specifies the internal object of the container used to contain the elements of the stack.

Member types

The following list gives the member types of the stack and provides a brief description of them.

Member typeDescription
value_typeSpecifies the element type.
container_typeSpecifies the base container type.
size_typeIt specifies the size range of the elements.

Function

With functions, objects or variables can be used in the programming field. The stack provides a large number of functions that can be used or embedded in a program. The list is as follows:

FunctionDescription
(constructor)This function is used to construct the stack container.
emptyThis function is used to test whether the stack is empty. If the stack is empty, this function returns true; otherwise, it returns false.
sizeThis function returns the size of the stack container, which is a measure of the number of elements stored in the stack.
topThis function is used to access the top element of the stack. This element plays a very important role because all insertion and deletion operations are performed on the top element.
pushThis function is used to insert a new element at the top of the stack.
popThis function is used to delete an element, removing the element from the top of the stack.
emplaceThis function is used to insert a new element into the stack above the current top element.
swapThis function is used to swap the contents of the two containers referenced by the references.
relational operatorsNon-member functions specify the relational operators required for the stack.
uses allocator<stack>As the name suggests, non-member functions use allocators for the stack.

Example: A simple program demonstrating the usage of basic stack functions.

#include <iostream>
#include <stack>
using namespace std;
void newstack(stack<int> ss)
{
	stack<int> sg = ss;
	while (!sg.empty())
	{
		cout << '\t' << sg.top();
		sg.pop();
	}
	cout << '\n';
}
int main()
{
	stack<int> newst;
	newst.push(55);
	newst.push(44);
	newst.push(33);
	newst.push(22);
	newst.push(11);
	cout << "The latest stack is : ";
	newstack(newst);
	cout << "\n newst.size() : " << newst.size();
	cout << "\n newst.top() : " << newst.top();
	cout << "\n newst.pop() : ";
	newst.pop();
	newstack(newst); 
	return 0;
}

Output:

The latest stack is : 	11	22	33	44	55
 newst.size() : 5
 newst.top() : 11
 newst.pop() : 	22	33	44	55