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

6.3 Example Program: An Inventory System

Let's use a simple inventory management system to illustrate the use of several list operations. Assume a business, named WorldWideWidgetWorks, requires a software system to manage their supply of widgets.

NOTE -- The complete version of the widget works program is in file widwork.cpp.

Widgets are simple devices, distinguished by different identification numbers:

The state of the inventory is represented by two lists: a list of widgets represents the stock of widgets on hand, and a list of widget identification types represents the type of widgets that customers have backordered.

To handle our inventory we have two commands: order() processes orders, and receive() processes the shipment of a new widget.

When a new widget arrives in shipment, we compare the widget identification number with the list of widget types on backorder. We use find() to search the backorder list, immediately shipping the widget if necessary. Otherwise it is added to the stock on hand:

When a customer orders a new widget, we scan the list of widgets in stock to determine if the order can be processed immediately. We can use the standard algorithm std::find_if() to search the list. To do so, we need a binary function that takes as its argument a widget and determines whether the widget matches the type requested. We create this function by taking a general binary widget-testing function, and binding the second argument to the specific widget type. To use the function std::bind2nd(), however, requires that the binary function be an instance of the class binary_function. The general widget-testing function is written as follows:

The widget order function is then written as follows:

Previous fileTop of DocumentContentsIndex pageNext file