Previous fileTop of DocumentContentsIndex pageNext file
Apache C++ Standard Library User's Guide

10.2 The stack Data Abstraction

As a data abstraction, a stack is traditionally defined as any object that implements the operations defined in Table 16:

Table 16: Stack operations

Function Implemented operation


Returns true if the collection is empty


Returns number of elements in collection


Returns (but does not remove) the topmost element in the stack


Pushes a new element onto the stack


Removes (but does not return) the topmost element from the stack

Note that removing the element at the top of the stack does not return a value. If the value of the element is important, you must retrieve the element before you remove the element.

10.2.1 Include Files

Programs that use the stack data abstraction should include the stack header:

10.2.2 Declaration and Initialization of stack

A stack can use either a vector or deque as the underlying container. When deciding which to use, consider the data which is to be stored. If the number of elements will vary widely, a deque may decrease the total amount of memory used when the stack has few elements. A vector has less overhead, so may be more efficient in cases where the stack will tend to remain at approximately the same size. However, increasing the memory allocated to a vector is relatively slow, so a vector may be inefficient if the size of the container will tend to increase. These are, of course, generalized statements which may not apply to the problem at hand. In cases where efficiency of either speed or memory is important, you should try both underlying containers and compare the results.

The following are sample declarations for a stack:

The last example creates a stack from a user-defined type named Customer.

10.2.3 Example Program: An RPN Calculator

A classic application of a stack is in the implementation of this calculator.

NOTE -- The executable version of this program is in the file calc.cpp.

Input to the calculator consists of a standard string that represents an expression written in reverse polish notation (RPN). Operands, called integer constants, are pushed onto a stack of values. As operators are encountered, the appropriate number of operands are popped off the stack, the operation is performed, and the result is pushed back onto the stack.

We can divide the development of our stack simulation into two parts, a calculator engine and a calculator program. A calculator engine is concerned with the actual work involved in the simulation, but does not perform any input or output operations. The name is intended to suggest an analogy to a car engine or a computer processor: the mechanism performs the actual work, but the user of the mechanism does not normally directly interact with it. Wrapped around this is the calculator program, which interacts with the user and passes appropriate instructions to the calculator engine.

We can use the following class definition for our calculator engine. Inside the class declaration we define an enumerated list of values to represent each of the possible operators that the calculator is prepared to accept. We have made two simplifying assumptions: all operands will be integer values, and only binary operators will be handled.

The member function doOperator() performs the actual work. It pops values from the stack, performs the operation, then pushes the result back onto the stack:

(This code is simplified for demonstration purposes. A more robust program would check to see if the stack were empty before attempting to pop() elements from it.)

The main program reads values in reverse polish notation, invoking the calculator engine to do the actual work:

Previous fileTop of DocumentContentsIndex pageNext file