[转] STL multimap 使用

看了好多关于STL multimap的使用方法,但就这个还是较简洁。现转过来,算是笔记吧:

 

#include <string.h> #include <iostream> #include <map> #include <utility> 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<string, int, compare> m; multimap<string, int> m; m.insert(pair<string, int>("a", 1)); m.insert(pair<string, int>("c", 2)); m.insert(pair<string, int>("b", 3)); m.insert(pair<string, int>("b", 4)); m.insert(pair<string, int>("a", 5)); m.insert(pair<string, int>("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<string, int>::iterator it = m.begin(); it != m.end(); ++it) { cout << " [" << (*it).first << ", " << (*it).second << "]" << endl; } pair<multimap<string, int>::iterator, multimap<string, int>::iterator> ppp; // equal_range(b) returns pair<iterator,iterator> 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<string, int>::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(); }  

结果:

Compile: g++ testMultimap.cpp 
Run: ./a.out

Number of elements with key a: 2
Number of elements with key b: 3
Number of elements with key c: 1
Elements in m:
[a, 1]
[a, 5]
[b, 3]
[b, 4]
[b, 6]
[c, 2]

Range of "b" elements:
[b, 3]
[b, 4]
[b, 6]
 
来源:http://www.yolinux.com/TUTORIALS/CppStlMultiMap.html

你可能感兴趣的:([转] STL multimap 使用)