C++/Map Multimap/map find
Содержание
find all elements in a map by 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(); map<int,PC>::const_iterator site; const PC::PC_type desired_type = PC::IBM; // find all elements by value bool found = false; for( site = sold.begin(); site != sold_end; ++site ) if( site->second.appliance() == desired_type ){ found = true; site->second.print(); } if( !found ) cout << "No IBMs sold\n";
} inline bool greater_model( const pair<PC::PC_type,PC> p,int min_model ) { return p.second.model() >= min_model; }
</source>
Find product in a map by its number
<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}; vector<PC> v; for( int i = 0; i < 3; ++i ) v.push_back( PC( kind[i], i, i ) ); map<int,PC> sold; transform( kind, kind+3, v.begin(),inserter( sold, sold.end() ),make_pair<int,PC> ); map<int,PC>::const_iterator sold_end = sold.end(); map<int,PC>::const_iterator site; for( site = sold.begin(); site != sold_end; ++site ) site->second.print(); const int desired_serial = 33447; site = sold.find( desired_serial ); if( site != sold_end ) site->second.print(); else cout << "There is no appliance with serial number " << desired_serial << endl;
} inline bool greater_model( const pair<PC::PC_type,PC> p,int min_model ) { return p.second.model() >= min_model; }
</source>
Find product in a map by its type
<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(); map<int,PC>::const_iterator site; for( site = sold.begin(); site != sold_end; ++site ) site->second.print(); const PC::PC_type desired_type = PC::IBM; for( site = sold.begin(); site != sold_end; ++site ) if( site->second.appliance() == desired_type ) break; if( site != sold_end ) site->second.print(); else cout << "No IBMs sold\n";
} inline bool greater_model( const pair<PC::PC_type,PC> p,int min_model ) { return p.second.model() >= min_model; }
</source>
Find the value with a given key
<source lang="cpp">
- include <iostream>
- include <map>
using namespace std;
int main() {
map<char, int> m; int i; for(i=0; i<26; i++) { m.insert(pair<char, int>("A"+i, 65+i)); } char ch; cout << "Enter key: "; cin >> ch; map<char, int>::iterator p; p = m.find(ch); if(p != m.end()) cout << "Its ASCII value is " << p->second; else cout << "Key not in map.\n"; return 0;
}
</source>
Map find
<source lang="cpp">
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
- include <algorithm>
- include <map>
using namespace std; /* function object to check the value of a map element
*/
template <class K, class V> class value_equals {
private: V value; public: // constructor (initialize value to compare with) value_equals (const V& v) : value(v) { } // comparison bool operator() (pair<const K, V> elem) { return elem.second == value; }
}; int main() {
typedef map<float,float> FloatFloatMap; FloatFloatMap coll; FloatFloatMap::iterator pos; // fill container coll[1]=7; coll[2]=4; coll[3]=2; coll[4]=3; coll[5]=6; coll[6]=1; coll[7]=3; // search an element with key 3.0 pos = coll.find(3.0); // logarithmic complexity if (pos != coll.end()) { cout << pos->first << ": " << pos->second << endl; }
}
/*
3: 2
*/ </source>
map: find_if
<source lang="cpp">
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference" * by Nicolai M. Josuttis, Addison-Wesley, 1999 * * (C) Copyright Nicolai M. Josuttis 1999. * Permission to copy, use, modify, sell and distribute this software * is granted provided this copyright notice appears in all copies. * This software is provided "as is" without express or implied * warranty, and with no claim as to its suitability for any purpose. */
- include <iostream>
- include <algorithm>
- include <map>
using namespace std; /* function object to check the value of a map element
*/
template <class K, class V> class value_equals {
private: V value; public: // constructor (initialize value to compare with) value_equals (const V& v) : value(v) { } // comparison bool operator() (pair<const K, V> elem) { return elem.second == value; }
}; int main() {
typedef map<float,float> FloatFloatMap; FloatFloatMap coll; FloatFloatMap::iterator pos; // fill container coll[1]=7; coll[2]=4; coll[3]=2; coll[4]=3; coll[5]=6; coll[6]=1; coll[7]=3; // search an element with value 3.0 pos = find_if(coll.begin(),coll.end(), // linear complexity value_equals<float,float>(3.0)); if (pos != coll.end()) { cout << pos->first << ": " << pos->second << endl; }
} /* 4: 3
*/ </source>
map find with custom object
<source lang="cpp">
- include <map>
- include <iostream>
using namespace std; class Data { public:
Data(int val = 0) { mVal = val; } int getVal() const { return mVal; } void setVal(int val) {mVal = val; }
protected:
int mVal;
}; int main(int argc, char** argv) {
map<int, Data> dataMap; dataMap[1] = Data(4); dataMap[1] = Data(6); map<int, Data>::iterator it = dataMap.find(1); if (it != dataMap.end()) { it->second.setVal(100); } return (0);
}
</source>
map look up
<source lang="cpp">
- include <map>
- include <iostream>
using namespace std; class Data { public:
Data(int val = 0) { mVal = val; } int getVal() const { return mVal; } void setVal(int val) {mVal = val; }
protected:
int mVal;
}; int main(int argc, char** argv) {
map<int, Data> dataMap; dataMap[1] = Data(4); dataMap[1] = Data(6); dataMap[1].setVal(100); return (0);
}
</source>