下面这段忘记从哪里Copy的了。
1: #include
2: #include
3: #include <string>
4: using namespace std;
5: int main()
6: {
7: multimap<string, string> names;
8: string n;
9: names.insert(pair<string, string>("Z", "F"));
10: names.insert(pair<string, string>("Z", "A"));
11: names.insert(pair<string, string>("S", "T"));
12: names.insert(pair<string, string>("S", "A"));
13: names.insert(pair<string, string>("S", "J"));
14: names.insert(pair<string, string>("D", "H"));
15: names.insert(pair<string, string>("D", "W"));
16: names.insert(pair<string, string>("D", "R"));
17: multimap<string, string>::iterator p;
18: cout << "Enter last name: ";
19: cin >> n;
20: p = names.find(n);
21: if(p != names.end()) { // found a name
22: do {
23: cout << n << ", " << p->second;
24: cout << endl;
25: p++;
26: } while (p != names.upper_bound(n));
27: }
28: else{
29: cout << "Name not found./n";
30: }
31: return 0;
32: }
根据这个的提示,我的一段代码中这样使用:
multimap npmap; multimap::iterator it_partid; vector::iterator it_dc; vector::iterator it_rack; vector::iterator it_switch; vector::iterator it_node; char ipstr[32]; char pidstr[48]; char* file_name; FILE* topo_file; Backend be; if (be.init() == 0) { printf("ERR!/n"); ret = false; goto _EXIT_; } …… for(uint64 i = 0; i < NP; i++) { /* generate the partid */ memset(partid, 0, sizeof(PartID)); partid->bits = SL; byte *id = (byte *)&i; for(int k = 0; k < 8; k++) { byte *pid = partid->id + 19 - k; *pid = *(id+k); } uint32 ip; /* fill the rgmap entry with ip */ for(int j = 0; j < RN; j++) { ip = be.next(); rgent->r[j].ip = ip; PartID* tmppid = NEW(PartID); pid_assign(tmppid, partid); npmap.insert(pair(ip, tmppid)); } rgmap_insert(rg, partid, rgent); } file_name = get_absolute_pathname("rgstruct.txt"); topo_file = fopen(file_name, "w"); if (topo_file==NULL) topo_file=stdout; else { fprintf(stdout, " - RGMAP structure had been printed for checking at %s./n", file_name); } free(file_name); for (it_dc=be.begin(); it_dc != be.end(); ++it_dc) { fprintf(topo_file, " Data Center %d. (%d racks)/n", it_dc->get_id(), it_dc->get_num()); for (it_rack=it_dc->begin(); it_rack != it_dc->end(); ++it_rack) { fprintf(topo_file, " /tRack %d. (%d switches)/n", it_rack->get_id(), it_rack->get_num()); for (it_switch=it_rack->begin(); it_switch != it_rack->end(); ++it_switch) { fprintf(topo_file, " /t/tSwitch %d. (%d nodes)/n", it_switch->get_id(), it_switch->get_num()); for (it_node=it_switch->begin(); it_node != it_switch->end(); ++it_node) { fprintf(topo_file, " /t/t/tNode %s./n", ip_htoa(*it_node, ipstr, 32)); it_partid = npmap.find(*it_node); if(it_partid != npmap.end()) { // found a name do { fprintf(topo_file, " /t/t/t/t%s/n", pid_str(it_partid->second, pidstr)); it_partid++; } while (it_partid != npmap.upper_bound(*it_node)); } else{ fprintf(topo_file, " /t/t/t/t(no partition)./n"); } } } } } if (topo_file!=stdout) fclose(topo_file); for (it_partid=npmap.begin(); it_partid != npmap.end(); it_partid++) { FREE(it_partid->second); }