#include #include #include #include #include #include #include #include using namespace std;class PC{ public: enum PC_type { Dell, HP, IBM, Compaq }; PC( PC_type appliance = Dell, int model = 220, int serial = 0 ); bool operator<( const PC& rhs ) const; PC_type appliance() const; int model() const; string name() const; void print() const; int serial() const; private: PC_type appliance_; int model_; int serial_;};inlinePC::PC( PC::PC_type appliance, int model, int serial ) : appliance_( appliance ), model_( model ), serial_( serial ){}inlinebool PC::operator<( const PC& rhs ) const{ return appliance() < rhs.appliance() || ( appliance() == rhs.appliance() && model() < rhs.model() );}inlinePC::PC_type PC::appliance() const{ return appliance_; }inlineint PC::model() const{ return model_; }string PC::name() const{ string what; switch( appliance() ) { case Dell: what = "Dell"; break; case HP: what = "HP"; break; case IBM: what = "IBM"; break; case Compaq: what = "Compaq"; break; default: what = "Unknown appliance"; break; } return what;}inlinevoid PC::print() const{ char oldfill = cout.fill(); cout << name() << " - Model " << model() << ", Serial number " << serial() << endl;}inlineint PC::serial() const{ return serial_; }bool greater_model( const pair p,int min_model );int main( ){ const PC::PC_type kind[] = { PC::IBM, PC::IBM, PC::Dell, PC::HP, PC::HP, PC::HP }; const int num_appliances = 3; vector v; for( int i = 0; i < num_appliances; ++i ) v.push_back( PC( kind[i], i, i ) ); map sold; transform( kind, kind+num_appliances, v.begin(),inserter( sold, sold.end() ),make_pair ); map::const_iterator sold_end = sold.end(); map::const_iterator site; for( site = sold.begin(); site != sold_end; ++site ) site->second.print(); // work with a multimap. key is appliance type, value is PC typedef multimap PC_multimap_type; PC_multimap_type stock; // appliance that customer desires const PC desired( PC::HP ); cout << desired.name(); // load the appliances into the multimap transform( kind, kind+num_appliances, v.begin(),inserter( stock, stock.end() ), make_pair ); PC_multimap_type::const_iterator stock_end = stock.end();}inlinebool greater_model( const pair p,int min_model ){ return p.second.model() >= min_model; }