// KeyedCollection1.h // template class KeyedCollection - Sequential search #include #include #include #include "customer.h" using namespace std; // TEMPLATE CLASS DECLARATION SECTION ======================= template class KeyedCollection { private: vector CustList; KTYPE key; int maxElements; int numElements; int comparisons; public: // Constructors KeyedCollection(); void insert(KTYPE key, TYPE object); // inserts a new object, with key value 'key', into the collection bool find(KTYPE key, TYPE &object); // attempts to find an object with key value 'key' in the collection // if not found, returns false. if found, stores found object in reference // parameter 'object' and returns true int getNumComparisons(); // returns the number of comparisons used in the last 'insert' or 'find' operation bool empty(); // returns true if collection is empty, false otherwise }; // TEMPLATE CLASS IMPLEMENTATION SECTION ======================= template KeyedCollection::KeyedCollection() { key = 0; numElements = 0; comparisons = 0; } template void KeyedCollection::insert(KTYPE k, TYPE o) { CustList.push_back(o); return; } template bool KeyedCollection::find(KTYPE k, TYPE & o) { comparisons =0 ; for (int i=0; i < CustList.size(); i++) { comparisons++; if ( CustList[i].GetCustID() == k ) { o = CustList[i]; return true; } } return false; } template int KeyedCollection::getNumComparisons() { return comparisons; } template bool KeyedCollection::empty() { if (CustList.empty()) return true; else return false; }