
複数の list 演算の使用を説明するために、単純な在庫管理システムを使用します。WorldWideWidgetWorks という名前のビジネスで、ウィジェットの供給を管理するためのソフトウェアシステムが必要であると仮定します。
注: ウィジェット ワーク プログラムの実行可能バージョンは、widwork.cpp というファイルにあります。
ウィジェット (Widget) とは、さまざまな識別番号によって識別される単純なデバイスです。
class Widget {
public:
Widget(int a = 0) : id(a) { }
void operator = (const Widget& rhs) { id = rhs.id; }
int id;
friend ostream & operator << (ostream & out,const Widget & w)
{ return out << "Widget " << w.id; }
friend bool operator == (const Widget& lhs, const Widget& rhs)
{ return lhs.id == rhs.id; }
friend bool operator< (const Widget& lhs, const Widget& rhs)
{ return lhs.id < rhs.id; }
};
在庫の状態は 2 つの list で表されます。ウィジェットの list はウィジェットの在庫を表し、ウィジェット識別型の list は顧客が注文したウィジェットの型を表します。在庫の処理のためには、2 つのコマンドがあります。order() は受注を処理し、receive()は新しいウィジェットの出荷を処理します。
class inventory {
public:
void order (int wid); // ウィジェット型 wid の注文を処理する
void receive (int wid); // ウィジェット型 wid の出荷を受信する
private:
list<Widget> on_hand;
list<int> on_order;
};
新しいウィジェット型が出荷担当部署に到着すると、ウィジェットの識別番号と受注しているウィジェット型のリストが比較されます。find() を使用して、受注リストを検索し、必要があれば、ウィジェットを直ちに出荷します。それ以外の場合は、在庫に追加されます。
void inventory::receive (int wid)
{
cout << "Received shipment of widget type " << wid << endl;
list<int>::iterator weneed =
find (on_order.begin(), on_order.end(), wid);
if (weneed != on_order.end())
{
cout << "Ship " << Widget(wid)
<< " to fill back order" << endl;
on_order.erase(weneed);
}
else
on_hand.push_front(Widget(wid));
}
顧客が新しいウィジェットを注文すると、担当者はウィジェットの在庫リストに目を通し、注文をすぐに処理できるかどうかを確認します。リストの検索には、関数 find_if() を使用することができます。これを行うには、引数に widget を使用し、ウィジェットが要求された型と一致するかどうかを判断する 2 項関数が必要です。この関数は汎用 2 項ウィジェットテスト関数を使用して、2 番目の引数を特定のウィジェット型にバインドすることによって作成します。ただし、関数 bind2nd() を使用するには、2 項関数がクラス binary_function のインスタンスであることが必要です。汎用ウィジェットテスト関数は、次のように書かれています。
class WidgetTester : public binary_function<Widget, int, bool> {
public:
bool operator () (const Widget & wid, int testid) const
{ return wid.id == testid; }
};
ウィジェット受注関数は、次のように書かれます。
void inventory::order (int wid)
{
cout << "Received order for widget type " << wid << endl;
list<Widget>::iterator wehave =
find_if (on_hand.begin(), on_hand.end(),
bind2nd(WidgetTester(), wid));
if (wehave != on_hand.end())
{
cout << "Ship " << *wehave << endl;
on_hand.erase(wehave);
}
else
{
cout << "Back order widget of type " << wid << endl;
on_order.push_front(wid);
}
}