C++/Map Multimap/map iterator
Содержание
- 1 const_iterator for map if integer and user-define object
- 2 const_reverse_iterator from a map
- 3 Create int string map and print all element pair
- 4 Get iterator from a map
- 5 Loop through map and print all the key/value pair
- 6 Map Iterators
- 7 Use iterator to loop through map and print all elements
- 8 Use while to loop through a map
const_iterator for map if integer and user-define object
<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}; int num_appliances = 3; 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+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();
} inline bool greater_model( const pair<PC::PC_type,PC> p,int min_model ) { return p.second.model() >= min_model; }
</source>
const_reverse_iterator from a map
<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(); map<int,PC>::const_iterator site; for( site = sold.begin(); site != sold_end; ++site ) site->second.print(); map<int,PC>::const_reverse_iterator reverse_site; map<int,PC>::const_reverse_iterator sold_rend = sold.rend(); const PC::PC_type desired_type = PC::IBM; for( reverse_site = sold.rbegin(); reverse_site != sold_rend;++reverse_site ) if( reverse_site->second.appliance() == desired_type ) break; if( reverse_site != sold_rend ) reverse_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>
Create int string map and print all element pair
<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 <map>
- include <string>
using namespace std; int main() {
// type of the collection typedef multimap<int,string> IntStringMMap; IntStringMMap coll; // container for int/string values // insert some elements in arbitrary order // - a value with key 1 gets inserted twice coll.insert(make_pair(5,"tagged")); coll.insert(make_pair(2,"a")); coll.insert(make_pair(1,"this")); coll.insert(make_pair(4,"of")); coll.insert(make_pair(6,"strings")); coll.insert(make_pair(1,"is")); coll.insert(make_pair(3,"multimap")); /* print all element values * - iterate over all elements * - element member second is the value */ IntStringMMap::iterator pos; for (pos = coll.begin(); pos != coll.end(); ++pos) { cout << pos->second << " "; } cout << endl;
}
/*
this is a multimap of tagged strings
*/ </source>
Get iterator from a map
<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>
Loop through map and print all the key/value pair
<source lang="cpp">
- include <iostream>
using std::cout; using std::endl;
- include <map> // map class-template definition
int main() {
std::map< int, double, std::less< int > > pairs; pairs.insert( std::map< int, double, std::less< int > >::value_type( 15, 2.7 ) ); pairs.insert( std::map< int, double, std::less< int > >::value_type( 30, 111.11 ) ); pairs.insert( std::map< int, double, std::less< int > >::value_type( 0, 1010.1 ) ); pairs.insert( std::map< int, double, std::less< int > >::value_type( 10, 22.22 ) ); pairs.insert( std::map< int, double, std::less< int > >::value_type( 25, 33.333 ) ); pairs.insert( std::map< int, double, std::less< int > >::value_type( 0, 77.54 ) ); // dup ignored pairs.insert( std::map< int, double, std::less< int > >::value_type( 20, 9.345 ) ); pairs.insert( std::map< int, double, std::less< int > >::value_type( 15, 99.3 ) ); // dup ignored cout << "pairs contains:\nKey\tValue\n"; // use const_iterator to walk through elements of pairs for ( std::map< int, double, std::less< int > >::const_iterator iter = pairs.begin(); iter != pairs.end(); ++iter ) cout << iter->first << "\t" << iter->second << "\n"; cout << endl; return 0;
} /* pairs contains: Key Value 0 1010.1 10 22.22 15 2.7 20 9.345 25 33.333 30 111.11
*/ </source>
Map Iterators
<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); for (map<int, Data>::iterator it = dataMap.begin(); it != dataMap.end(); ++it) { cout << it->second.getVal() << endl; } return (0);
}
</source>
Use iterator to loop through map and print all elements
<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 <map>
- include <string>
using namespace std; int main() {
/* type of the container: * - map: elements key/value pairs * - string: keys have type string * - float: values have type float */ typedef map<string,float> StringFloatMap; StringFloatMap coll; // insert some elements into the collection coll["VAT"] = 0.15; coll["Pi"] = 3.1415; coll["an arbitrary number"] = 4983.223; coll["Null"] = 0; /* print all elements * - iterate over all elements * - element member first is the key * - element member second is the value */ StringFloatMap::iterator pos; for (pos = coll.begin(); pos != coll.end(); ++pos) { cout << "key: \"" << pos->first << "\" " << "value: " << pos->second << endl; }
}
/*
key: "Null" value: 0 key: "Pi" value: 3.1415 key: "VAT" value: 0.15 key: "an arbitrary number" value: 4983.22
*/ </source>
Use while to loop through a map
<source lang="cpp">
- include <map>
- include <iostream>
- include <string>
using namespace std; typedef map<string, int> STRING2INT; int main(void) {
STRING2INT DateMap; STRING2INT::iterator DateIterator; string DateBuffer; DateMap["January"] = 1; DateMap["February"] = 2; DateMap["March"] = 3; DateMap["April"] = 4; DateMap["May"] = 5; DateMap["June"] = 6; DateMap["July"] = 7; DateMap["August"] = 8; DateMap["September"] = 9; DateMap["October"] = 10; DateMap["November"] = 11; DateMap["December"] = 12; DateIterator = DateMap.end(); while(DateIterator == DateMap.end()) { cout << "Enter a Month :"; cin >> DateBuffer; if((DateIterator = DateMap.find(DateBuffer)) != DateMap.end()) cout << (*DateIterator).first << " is Month Number " << (*DateIterator).second << endl; else cout << "Enter a Valid Month (example: March)" << endl; }
}
</source>