初学C++,遇到这一问题。网上Java版本的一堆,C++版本的很少见,特此分享出来。
欢迎大神补充。
#includeusing namespace std; #include"map" #include"string" class Person { public: string name; int age; string tel; double salary; }; void test01() { Person p1, p2, p3, p4, p5; p1.name = "王1"; p1.age = 31; p2.name = "王2"; p2.age = 32; p3.name = "张3"; p3.age = 33; p4.name = "张4"; p4.age = 34; p5.name = "赵5"; multimap map2; map2.insert(make_pair("sale", p1)); map2.insert(make_pair("sale", p2)); map2.insert(make_pair("development", p3)); map2.insert(make_pair("development", p4)); map2.insert(make_pair("Financial", p5)); for (multimap ::iterator it = map2.begin(); it != map2.end(); it++) { cout << it->first << "t" << it->second.name << endl; } cout << "遍历结束" << endl; cout << "development部分人数: " << map2.count("development") << endl; cout << "development部门员工信息" << endl; multimap ::iterator it2 = map2.find("development"); //multimap ::iterator it2; //第一种方法输出某一部们的所有人 //int num2 = map2.count("development"); //int tag = 0; //while (it2 != map2.end()&&tag first; // cout << it2->first << "t" << it2->second.name << endl; // it2++; // tag++; //} //第二种方法输出某一部们的所有人 string tmp = it2->first; while (it2 != map2.end()) { cout << it2->first << "t" << it2->second.name << endl; it2++; if (it2->first!= tmp)//如果不是同一部门就中断退出 { break; } } } void main() { test01(); system("pause"); return ; }



