English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
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
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.
The following list gives the member types of the stack and provides a brief description of them.
Member type | Description |
---|---|
value_type | Specifies the element type. |
container_type | Specifies the base container type. |
size_type | It specifies the size range of the elements. |
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:
Function | Description |
---|---|
(constructor) | This function is used to construct the stack container. |
empty | This function is used to test whether the stack is empty. If the stack is empty, this function returns true; otherwise, it returns false. |
size | This function returns the size of the stack container, which is a measure of the number of elements stored in the stack. |
top | This 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. |
push | This function is used to insert a new element at the top of the stack. |
pop | This function is used to delete an element, removing the element from the top of the stack. |
emplace | This function is used to insert a new element into the stack above the current top element. |
swap | This function is used to swap the contents of the two containers referenced by the references. |
relational operators | Non-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. |
#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