// File: MultimapString2Int // Multimaps permit multiple values for a key #include #include #include using namespace std; int main() { // Compare (<) function not required since it is built into string class. // else declaration would comparison function in multimap definition. // i.e. multimap m; multimap m; m.insert(pair("a", 1)); m.insert(pair("c", 2)); m.insert(pair("b", 3)); m.insert(pair("b", 4)); m.insert(pair("a", 5)); m.insert(pair("b", 6)); cout << "Number of elements with key a: " << m.count("a") << endl; cout << "Number of elements with key b: " << m.count("b") << endl; cout << "Number of elements with key c: " << m.count("c") << endl; cout << "Elements in m: " << endl; for (multimap::iterator it = m.begin(); it != m.end(); ++it) { cout << " [" << (*it).first << ", " << (*it).second << "]" << endl; } pair::iterator, multimap::iterator> ppp; // equal_range(b) returns pair representing the range // of element with key b ppp = m.equal_range("b"); // Loop through range of maps of key "b" cout << endl << "Range of \"b\" elements:" << endl; for (multimap::iterator it2 = ppp.first; it2 != ppp.second; ++it2) { cout << " [" << (*it2).first << ", " << (*it2).second << "]" << endl; } // Can't do this (??) // cout << ppp.first << endl; // cout << ppp.second << endl; m.clear(); }