You don't say which operation you wish to perform, however a stack has relatively few members:
construct: e.g. default and copy (C++11 includes move construct)
operator=: copy assignment (C++11 includes move assignment)
empty: test whether the stack is empty or not
pop: extract the top element from the stack
push: insert an element on top of the stack
size: return the size of the stack, count of elements
top (return the top element)
Additional members since C++11:
emplace: construct and push a new element from argument
swap: exchange elements with another stack
Non-member operator overloads are limited to the standard relational operators (e.g. ==, !=, <, <=, > and >=).
Stacks are typically used in backtracking algorithms because the last element pushed becomes the top element and is therefore the first element to be popped. Usually referred to as a LIFO (last in first out) sequence.
An example usage of a stack:
#include<iOStream>
#include<stack>
#include<string>
#include<cassert>
int main ()
{
std::stack<std::string> mystack;
assert (mystack.empty());
mystack.push ("First element");
mystack.push ("Second element");
assert (mystack.size()==2);
std::cout << "mystack contains:\n";
while (!mystack.empty())
{
std::cout << mystack.top() << '\n';
mystack.pop();
}
}
Copyright © 2026 eLLeNow.com All Rights Reserved.