Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) std::stack

 

std::stack is an STL container for a stack (that is: First-In-Last-Out storage).

 

#include <cassert>
#include <stack>

int main()
{
  //Create a stack
  std::stack<int> s;
  //Put three values on stack
  s.push(1);
  s.push(2);
  s.push(3);
  //Reading and emptying stack
  const int first = s.top();
  s.pop();
  const int second = s.top();
  s.pop();
  const int third = s.top();
  s.pop();

  assert(s.empty());
  assert(first == 3);
  assert(second == 2);
  assert(third == 1);
}

 

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict