C++/Map Multimap/map search
Содержание
search for all occurrences of key
<source lang="cpp">
- include <algorithm>
- include <functional>
- include <iomanip>
- include <iostream>
- include <map>
- include <string>
- include <utility>
- include <vector>
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_;
}; inline PC::PC( PC::PC_type appliance, int model,
int serial ) : appliance_( appliance ), model_( model ), serial_( serial )
{} // empty inline bool PC::operator<( const PC& rhs ) const { return appliance() < rhs.appliance() ||
( appliance() == rhs.appliance() && model() < rhs.model() );
} inline PC::PC_type PC::appliance() const { return appliance_; } inline int 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;
} inline void PC::print() const {
char oldfill = cout.fill(); cout << name() << " - Model " << model() << ", Serial number " << serial() << endl;
} inline int PC::serial() const { return serial_; } bool greater_model( const pair<PC::PC_type,PC> 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<PC> v; for( int i = 0; i < num_appliances; ++i ) v.push_back( PC( kind[i], i, i ) ); map<int,PC> sold; transform( kind, kind+num_appliances, v.begin(),inserter( sold, sold.end() ),make_pair<int,PC> ); map<int,PC>::const_iterator sold_end = sold.end(); // work with a multimap. key is appliance type, value is PC typedef multimap<PC::PC_type,PC> PC_multimap_type; PC_multimap_type stock; const PC desired( PC::HP ); // load the appliances into the multimap transform( kind, kind+num_appliances, v.begin(),inserter( stock, stock.end() ), make_pair<PC::PC_type,PC> ); PC_multimap_type::const_iterator stock_end = stock.end(); // search for first occurrence of key PC_multimap_type::const_iterator spot = stock.find( desired.appliance() ); if( spot != stock_end ) spot->second.print(); else cout << "Don"t have a " << desired.name() << " in stock\n";
} inline bool greater_model( const pair<PC::PC_type,PC> p,int min_model ) { return p.second.model() >= min_model; }
</source>
search for all occurrences of value
<source lang="cpp">
- include <algorithm>
- include <functional>
- include <iomanip>
- include <iostream>
- include <map>
- include <string>
- include <utility>
- include <vector>
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_;
}; inline PC::PC( PC::PC_type appliance, int model,int serial )
: appliance_( appliance ), model_( model ), serial_( serial )
{} // empty inline bool PC::operator<( const PC& rhs ) const { return appliance() < rhs.appliance() ||
( appliance() == rhs.appliance() && model() < rhs.model() );
} inline PC::PC_type PC::appliance() const { return appliance_; } inline int 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;
} inline void PC::print() const {
char oldfill = cout.fill(); cout << name() << " - Model " << model() << ", Serial number " << serial() << endl;
} inline int PC::serial() const { return serial_; } bool greater_model( const pair<PC::PC_type,PC> p,int min_model ); int main( ) {
const PC::PC_type kind[] = { PC::IBM,PC::Dell,PC::HP}; const int num_appliances = 3; vector<PC> v; for( int i = 0; i < num_appliances; ++i ) v.push_back( PC( kind[i], i, i ) ); map<int,PC> sold; transform( kind, kind+num_appliances, v.begin(),inserter( sold, sold.end() ),make_pair<int,PC> ); map<int,PC>::const_iterator sold_end = sold.end(); // work with a multimap. key is appliance type, value is PC typedef multimap<PC::PC_type,PC> PC_multimap_type; PC_multimap_type stock; const PC desired( PC::HP ); // load the appliances into the multimap transform( kind, kind+num_appliances, v.begin(),inserter( stock, stock.end() ), make_pair<PC::PC_type,PC> ); PC_multimap_type::const_iterator stock_end = stock.end(); // search for all occurrences of value cout << min_model << endl; bool found = false; for( spot = stock.begin(); spot != stock_end; ++spot ) if( greater_model( *spot, min_model ) ){ found = true; spot->second.print(); } if( !found ) cout << "No appliance with model number at least " << min_model << endl;
} inline bool greater_model( const pair<PC::PC_type,PC> p,int min_model ) { return p.second.model() >= min_model; }
</source>
search for first occurrence of value with find_if
<source lang="cpp">
- include <algorithm>
- include <functional>
- include <iomanip>
- include <iostream>
- include <map>
- include <string>
- include <utility>
- include <vector>
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_;
}; inline PC::PC( PC::PC_type appliance, int model,
int serial ) : appliance_( appliance ), model_( model ), serial_( serial )
{} // empty inline bool PC::operator<( const PC& rhs ) const { return appliance() < rhs.appliance() ||
( appliance() == rhs.appliance() && model() < rhs.model() );
} inline PC::PC_type PC::appliance() const { return appliance_; } inline int 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;
} inline void PC::print() const {
char oldfill = cout.fill(); cout << name() << " - Model " << model() << ", Serial number " << serial() << endl;
} inline int PC::serial() const { return serial_; } bool greater_model( const pair<PC::PC_type,PC> p,int min_model ); int main( ) {
const PC::PC_type kind[] = { PC::IBM,PC::Dell,PC::HP}; const int num_appliances = 3; vector<PC> v; for( int i = 0; i < num_appliances; ++i ) v.push_back( PC( kind[i], i, i ) ); map<int,PC> sold; transform( kind, kind+num_appliances, v.begin(),inserter( sold, sold.end() ),make_pair<int,PC> ); map<int,PC>::const_iterator sold_end = sold.end(); // work with a multimap. key is appliance type, value is PC typedef multimap<PC::PC_type,PC> PC_multimap_type; PC_multimap_type stock; const PC desired( PC::HP ); // load the appliances into the multimap transform( kind, kind+num_appliances, v.begin(),inserter( stock, stock.end() ), make_pair<PC::PC_type,PC> ); PC_multimap_type::const_iterator stock_end = stock.end(); // search for first occurrence of value const int min_model = 221; spot = find_if( stock.begin(), stock.end(), bind2nd( ptr_fun( greater_model ), min_model ) ); cout << min_model; if( spot != stock_end ) spot->second.print(); else cout << "No appliance with model at least than " << min_model << endl;
} inline bool greater_model( const pair<PC::PC_type,PC> p,int min_model ) { return p.second.model() >= min_model; }
</source>
search for last occurrence of value
<source lang="cpp">
- include <algorithm>
- include <functional>
- include <iomanip>
- include <iostream>
- include <map>
- include <string>
- include <utility>
- include <vector>
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_;
}; inline PC::PC( PC::PC_type appliance, int model,
int serial ) : appliance_( appliance ), model_( model ), serial_( serial )
{} // empty inline bool PC::operator<( const PC& rhs ) const { return appliance() < rhs.appliance() ||
( appliance() == rhs.appliance() && model() < rhs.model() );
} inline PC::PC_type PC::appliance() const { return appliance_; } inline int 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;
} inline void PC::print() const {
char oldfill = cout.fill(); cout << name() << " - Model " << model() << ", Serial number " << serial() << endl;
} inline int PC::serial() const { return serial_; } bool greater_model( const pair<PC::PC_type,PC> p,int min_model ); int main( ) {
const PC::PC_type kind[] = { PC::IBM,PC::Dell,PC::HP}; const int num_appliances = 3; vector<PC> v; for( int i = 0; i < num_appliances; ++i ) v.push_back( PC( kind[i], i, i ) ); map<int,PC> sold; transform( kind, kind+num_appliances, v.begin(),inserter( sold, sold.end() ),make_pair<int,PC> ); map<int,PC>::const_iterator sold_end = sold.end(); // work with a multimap. key is appliance type, value is PC typedef multimap<PC::PC_type,PC> PC_multimap_type; PC_multimap_type stock; const PC desired( PC::HP ); // load the appliances into the multimap transform( kind, kind+num_appliances, v.begin(),inserter( stock, stock.end() ), make_pair<PC::PC_type,PC> ); PC_multimap_type::const_iterator stock_end = stock.end(); // search for last occurrence of value multimap<PC::PC_type,PC>::reverse_iterator j = find_if( stock.rbegin(), stock.rend(),bind2nd( ptr_fun( greater_model ), min_model ) ); cout << "\nLAST APPLIANCE WITH MODEL AT LEAST " << min_model << endl; if( j != stock.rend() ) j->second.print(); else cout << "No appliance with model number at least " << min_model << endl;
} inline bool greater_model( const pair<PC::PC_type,PC> p,int min_model ) { return p.second.model() >= min_model; }
</source>