// 1018.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <map> #include <string> #include <vector> #include <iostream> using namespace std; int _tmain(int argc, _TCHAR* argv[]) { map< string,vector<string> > name_map; string surname,childname; cout << "Enter the last name for map:"; while ( cin >> surname ) { vector<string> child_vector; pair <map< string,vector<string> >::iterator,bool> ret = name_map.insert(make_pair(surname,child_vector)); if(!ret.second) cout << surname << " has been existed! Add the childname:" << endl; else cout << "Enter the childname for " << surname <<":" <<endl; while (cin >> childname) ret.first->second.push_back(childname); cin.clear(); } cin.clear(); cout << "enter the surname to be searched:"; while (cin >> surname) { map< string,vector<string> >::iterator iter = name_map.find(surname); if (iter == name_map.end()) { cout << "surname is not existed,enter again:" << endl; continue; } for (vector<string>::iterator it = iter->second.begin();it != iter->second.end();it++) cout << *it << " "; cout << endl; } return 0; }