Stack in C++ STL

Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only. 栈是后进先出,只在一端进行插入删除。
 
The functions associated with stack are:
empty() – Returns whether the stack is empty – Time Complexity : O(1)
size() – Returns the size of the stack – Time Complexity : O(1)
top() – Returns a reference to the top most element of the stack – Time Complexity : O(1)
push(g) – Adds the element ‘g’ at the top of the stack – Time Complexity : O(1)
pop() – Deletes the top most element of the stack – Time Complexity : O(1)

// CPP program to demonstrate working of STL stack 
#include <iostream> 
#include <stack> 
using namespace std; 

void showstack(stack <int> s) 
{ 
	while (!s.empty()) 
	{ 
		cout << '\t' << s.top(); 
		s.pop(); 
	} 
	cout << '\n'; 
} 

int main () 
{ 
	stack <int> s; 
	s.push(10); 
	s.push(30); 
	s.push(20); 
	s.push(5); 
	s.push(1); 

	cout << "The stack is : "; 
	showstack(s); 

	cout << "\ns.size() : " << s.size(); 
	cout << "\ns.top() : " << s.top(); 


	cout << "\ns.pop() : "; 
	s.pop(); 
	showstack(s); 

	return 0; 
} 

Output:

The stack is :     1    5    20    30    10

s.size() : 5
s.top() : 1
s.pop() :     5    20    30    10

List of functions of Stack:

stack push() and pop() in C++ STL

Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only.

stack::push()

push() function is used to insert an element at the top of the stack. The element is added to the stack container and the size of the stack is increased by 1. 在栈的顶端进行插入元素,大小加1.

Syntax :

stackname.push(value)
Parameters :
The value of the element to be inserted is passed as the parameter.
Result :
Adds an element of value same as that of 
the parameter passed at the top of the stack.

Examples:

Input :   mystack
          mystack.push(6);
Output :  6
 
Input :   mystack
          mystack.push(0);
          mystack.push(1);
Output :  0, 1

Errors and Exceptions

1. Shows error if the value passed doesn’t match the stack type.
2. Shows no exception throw guarantee if the parameter doesn’t throw any exception.

// CPP program to illustrate 
// Implementation of push() function 
#include <iostream> 
#include <stack> 
using namespace std; 

int main() 
{ 
	// Empty stack 
	stack<int> mystack; 
	mystack.push(0); 
	mystack.push(1); 
	mystack.push(2); 

	// Printing content of stack 
	while (!mystack.empty()) { 
		cout << ' ' << mystack.top(); 
		mystack.pop(); 
	} 
} 

Output:

2 1 0
Note that output is printed on the basis of LIFO property

stack::pop()

pop() function is used to remove an element from the top of the stack(newest element in the stack). The element is removed to the stack container and the size of the stack is decreased by 1. 在栈的头部删除元素,大小减一。

Syntax :

stackname.pop()
Parameters :
No parameters are passed.
Result :
Removes the newest element in the stack
or basically the top element.

Examples:

Input :   mystack = 0, 1, 2
          mystack.pop();
Output :  0, 1
 
Input :   mystack = 0, 1, 2, 3, 4, 5
          mystack.pop();
Output :  0, 1, 2, 3, 4

Errors and Exceptions

1. Shows error if a parameter is passed.
2. Shows no exception throw guarantee.

// CPP program to illustrate 
// Implementation of pop() function 
#include <iostream> 
#include <stack> 
using namespace std; 

int main() 
{ 
	stack<int> mystack; 
	mystack.push(1); 
	mystack.push(2); 
	mystack.push(3); 
	mystack.push(4); 
	// Stack becomes 1, 2, 3, 4 

	mystack.pop(); 
	mystack.pop(); 
	// Stack becomes 1, 2 

	while (!mystack.empty()) { 
		cout << ' ' << mystack.top(); 
		mystack.pop(); 
	} 
} 

Output:

2 1
Note that output is printed on the basis of LIFO property

Application :
Given a number of integers, add them to the stack and find the size of the stack without using size function.

Input : 5, 13, 0, 9, 4
Output: 5

Algorithm
1. Push the given elements to the stack container one by one.
2. Keep popping the elements of stack until it becomes empty, and increment the counter variable.
3. Print the counter variable.

// CPP program to illustrate 
// Application of push() and pop() function 
#include <iostream> 
#include <stack> 
using namespace std; 

int main() 
{ 
	int c = 0; 
	// Empty stack 
	stack<int> mystack; 
	mystack.push(5); 
	mystack.push(13); 
	mystack.push(0); 
	mystack.push(9); 
	mystack.push(4); 
	// stack becomes 5, 13, 0, 9, 4 

	// Counting number of elements in queue 
	while (!mystack.empty()) { 
		mystack.pop(); 
		c++; 
	} 
	cout << c; 
} 

Output:

5

stack empty() and stack size() in C++ STL

Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only.

stack::empty()

empty() function is used to check if the stack container is empty or not.判断栈是否为空。

Syntax :

stackname.empty()
Parameters :
No parameters are passed.
Returns :
True, if stack is empty
False, Otherwise

Examples:

Input :   mystack
          mystack.empty();
Output :  True
 
Input :   mystack = 1, 2, 3
Output :  False

Errors and Exceptions

1. Shows error if parameter is passed
2. Shows no exception throw guarantee.

// CPP program to illustrate 
// Implementation of empty() function 
#include <iostream> 
#include <stack> 
using namespace std; 

int main() 
{ 
	stack<int> mystack; 
	mystack.push(1); 

	// Stack becomes 1 

	if (mystack.empty()) { 
		cout << "True"; 
	} 
	else { 
		cout << "False"; 
	} 
	return 0; 
} 

Output:

False

Application :
Given a stack of integers, find the sum of the all the integers.

Input : 1, 8, 3, 6, 2
Output: 20

Algorithm
1. Check if the stack is empty, if not add the top element to a variable initialised as 0, and pop the top element.
2. Repeat this step until the stack is empty.
3. Print the final value of the variable.

// CPP program to illustrate 
// Application of empty() function 
#include <iostream> 
#include <stack> 
using namespace std; 

int main() 
{ 
	int sum = 0; 
	stack<int> mystack; 
	mystack.push(1); 
	mystack.push(8); 
	mystack.push(3); 
	mystack.push(6); 
	mystack.push(2); 

	// Stack becomes 1, 8, 3, 6, 2 

	while (!mystack.empty()) { 
		sum = sum + mystack.top(); 
		mystack.pop(); 
	} 
	cout << sum; 
	return 0; 
} 

Output:

20

stack::size()

size() function is used to return the size of the stack container or the number of elements in the stack container.

Syntax :

stackname.size()
Parameters :
No parameters are passed.
Returns :
Number of elements in the container.

Examples:

Input :   mystack = 0, 1, 2
          mystack.size();
Output :  3
 
Input :   mystack = 0, 1, 2, 3, 4, 5
          mystack.size();
Output :  6

Errors and Exceptions

1. Shows error if a parameter is passed.
2. Shows no exception throw guarantee.

// CPP program to illustrate 
// Implementation of size() function 
#include <iostream> 
#include <stack> 
using namespace std; 

int main() 
{ 
	int sum = 0; 
	stack<int> mystack; 
	mystack.push(1); 
	mystack.push(8); 
	mystack.push(3); 
	mystack.push(6); 
	mystack.push(2); 

	// Stack becomes 1, 8, 3, 6, 2 

	cout << mystack.size(); 

	return 0; 
} 

Output:

5

Application :
Given a stack of integers, find the sum of the all the integers.

Input : 1, 8, 3, 6, 2
Output: 20

Algorithm
1. Check if the size of the stack is zero, if not add the top element to a variable initialised as 0, and pop the top element.
2. Repeat this step until the stack size becomes 0.
3. Print the final value of the variable.

// CPP program to illustrate 
// Application of size() function 
#include <iostream> 
#include <stack> 
using namespace std; 

int main() 
{ 
	int sum = 0; 
	stack<int> mystack; 
	mystack.push(1); 
	mystack.push(8); 
	mystack.push(3); 
	mystack.push(6); 
	mystack.push(2); 

	// Stack becomes 1, 8, 3, 6, 2 

	while (mystack.size() > 0) { 
		sum = sum + mystack.top(); 
		mystack.pop(); 
	} 
	cout << sum; 
	return 0; 
} 

Output:

20

stack top() in C++ STL

Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only.

stack::top()

top() function is used to reference the top(or the newest) element of the stack. 获得栈顶第一个元素。

Syntax :

stackname.front()
Parameters :
No value is needed to pass as the parameter.
Returns :
Direct reference to the top element of the stack container.

Examples:

Input  : stackname.push(5);
         stackname.push(1);
         stackname.top();
Output : 1

Input  : stackname.push(5);
         stackname.push(1);
         stackname.push(2);
         stackname.top();
Output : 2

Errors and Exceptions

1. If the stack container is empty, it causes undefined behaviour
2. It has a no exception throw guarantee if the stack is not empty

// CPP program to illustrate 
// Implementation of top() function 
#include <iostream> 
#include <stack> 
using namespace std; 

int main() 
{ 
	stack<int> mystack; 
	mystack.push(5); 
	mystack.push(1); 
	mystack.push(2); 

	// Stack top 
	cout << mystack.top(); 
	return 0; 
} 

Output:

2

Application :
Given a stack of integers, find the sum of the all the integers.

Input : 1, 8, 3, 6, 2
Output: 20

Algorithm
1. Check if the stack is empty, if not add the top element to a variable initialised as 0, and pop the top element.
2. Repeat this step until the stack is empty.
3. Print the final value of the variable.

// CPP program to illustrate 
// Application of top() function 
#include <iostream> 
#include <stack> 
using namespace std; 

int main() 
{ 
	int sum = 0; 
	stack<int> mystack; 
	mystack.push(1); 
	mystack.push(8); 
	mystack.push(3); 
	mystack.push(6); 
	mystack.push(2); 

	// Stack becomes 1, 8, 3, 6, 2 

	while (!mystack.empty()) { 
		sum = sum + mystack.top(); 
		mystack.pop(); 
	} 
	cout << sum; 
	return 0; 
} 

Output:

20

stack swap() in C++ STL

Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only.

stack::swap()

This function is used to swap the contents of one stack with another stack of same type and size.

Syntax :

stackname1.swap(stackname2)
Parameters :
The name of the stack with which
the contents have to be swapped.
Result :
All the elements of the 2 stack are swapped.

Examples:

Input  : mystack1 = {1, 2, 3, 4}
         mystack2 = {3, 5, 7, 9}
         mystack1.swap(mystack2);
Output : mystack1 =  9, 7, 5, 3
         mystack2 =  4, 3, 2, 1

Input  : mystack1 = {1, 3, 5, 7}
         mystack2 = {2, 4, 6, 8}
         mystack1.swap(mystack2);
Output : mystack1 =  8, 6, 4, 2
         mystack2 =  7, 5, 3, 1
Note: In stack container, the elements are printed in reverse order because the top is printed first then moving on to other elements.

Errors and Exceptions

1. It throws an error if the stack are not of the same type.
2. It throws error if the stack are not of the same size.
2. It has a basic no exception throw guarantee otherwise.

// 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

stack emplace() in C++ STL

Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only.

stack::emplace()

This function is used to insert a new element into the stack container, the new element is added on top of the stack.
Syntax :

stackname.emplace(value)
Parameters :
The element to be inserted into the stack
is passed as the parameter.
Result :
The parameter is added to the stack 
at the top position.

Examples:

Input  : mystack{1, 2, 3, 4, 5};
         mystack.emplace(6);
Output : mystack = 6, 5, 4, 3, 2, 1

Input  : mystack{};
         mystack.emplace(4);
Output : mysstack = 4
Note: In stack container, the elements are printed in reverse order because the top is printed first then moving on to other elements.

Errors and Exceptions
1. It has a strong exception guarantee, therefore, no changes are made if an exception is thrown.
2. Parameter should be of same type as that of the container, otherwise an error is thrown.

// CPP program to illustrate 
// Implementation of emplace() function 
#include <iostream> 
#include <stack> 
using namespace std; 

int main() { 
stack<int> mystack; 
mystack.emplace(1); 
mystack.emplace(2); 
mystack.emplace(3); 
mystack.emplace(4); 
mystack.emplace(5); 
mystack.emplace(6); 
// stack becomes 1, 2, 3, 4, 5, 6 

// printing the stack 
cout << "mystack = "; 
while (!mystack.empty()) { 
	cout << mystack.top() << " "; 
	mystack.pop(); 
} 
return 0; 
} 

Output:

6 5 4 3 2 1

Time Complexity : O(1)

Difference between stack::emplace() and stack::push() function.
While push() function inserts a copy of the value or the parameter passed to the function into the container at the top, the emplace() function constructs a new element as the value of the parameter and then adds it to the top of the container.

Application :
Given a number of integers, add them to the stack using emplace() and find the size of the stack without using size function.

Input : 5, 13, 0, 9, 4
Output: 5

Algorithm
1. Insert the given elements to the stack container using emplace() one by one.
2. Keep popping the elements of stack until it becomes empty, and increment the counter variable.
3. Print the counter variable.

// CPP program to illustrate 
// Application of emplace() function 
#include <iostream> 
#include <stack> 
using namespace std; 

int main() { 
int c = 0; 
// Empty stack 
stack<int> mystack; 
mystack.emplace(5); 
mystack.emplace(13); 
mystack.emplace(0); 
mystack.emplace(9); 
mystack.emplace(4); 
// stack becomes 5, 13, 0, 9, 4 

// Counting number of elements in queue 
while (!mystack.empty()) { 
	mystack.pop(); 
	c++; 
} 
cout << c; 
} 

Output:

5

猜你喜欢

转载自blog.csdn.net/qq_27009517/article/details/86594637