C++ Stack Library - pop() Function



Description

The C++ function std::stack::pop() removes top element from the stack and reduces size of stack by one. This function calls destructor on removed element.

Declaration

Following is the declaration for std::stack::pop() function form std::stack header.

C++98

void pop();

Parameters

None

Return value

None

Exceptions

Depends upon underlying container.

Time complexity

Constant i.e. O(1)

Example

The following example shows the usage of std::stack::pop() function.

#include <iostream>
#include <stack>

using namespace std;

int main(void) {
   stack<int> s;

   for (int i = 0; i < 5; ++i)
      s.emplace(i + 1);

   while (!s.empty()) {
      cout << s.top() << endl;
      s.pop();
   }

   return 0;
}

Let us compile and run the above program, this will produce the following result −

Stack contents are
5
4
3
2
1
set.htm
Advertisements