- Stacks are a type of container adaptors.
- Operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from the end of the container.
- Stacks are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed/popped from the "back" of the specific container, which is known as the top of the stack.
- The underlying container may be any of the standard container class templates or some other specifically designed container class. The only requirement is that it supports the following operations:
- back()
- push_back()
- pop_back()
- Therefore, the standard container class templates vector, deque and list can be used. By default, if no container class is specified for a particular stack class, the standard container class template deque is used.
EXAMPLE
// stack::top
# include < iostream >
# include < stack >
using namespace std;
int main ()
{
stack< int > mystack;
mystack.push(10);
mystack.push(20);
mystack.top() -= 5;
cout << "mystack.top() is now " << mystack.top() << endl;
return 0;
}
Output:
mystack.top() is now 15 |
No comments:
Post a Comment