C++ 关于map容器的一道编程题请教
展开全部
#include <map>
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<string, vector<string>> Families;
vector<string> Children;
Children.push_back("红娃");
Children.push_back("橙娃");
Children.push_back("黄娃");
Children.push_back("绿娃");
Children.push_back("青娃");
Children.push_back("蓝娃");
Children.push_back("紫娃");
Families.insert(make_pair<string, vector<string>>("Zhao",Children));
Families.insert(make_pair<string, vector<string>>("Qian",Children));
Families.insert(make_pair<string, vector<string>>("Sun",Children));
Families.insert(make_pair<string, vector<string>>("Li",Children));
Families.insert(make_pair<string, vector<string>>("Zhou",Children));
Families.insert(make_pair<string, vector<string>>("Wu",Children));
Families.insert(make_pair<string, vector<string>>("Zheng",Children));
Families.insert(make_pair<string, vector<string>>("Wang",Children));
//
string Xing;
cin >> Xing;
//查找葫芦娃
auto iter = Families.find(Xing);
if (iter==Families.end())
cout << "没有找到姓" + Xing + "的葫芦娃" << endl;
else
{
vector<string> Huluwa = iter->second;
cout << "找到姓" + Xing + "的葫芦娃:" << endl;
for (unsigned i=0; i<Huluwa.size(); i++)
{
cout << Xing + Huluwa[i] << endl;
}
}
system("pause");
return 0;
}
#include <vector>
#include <string>
#include <iostream>
using namespace std;
int main()
{
map<string, vector<string>> Families;
vector<string> Children;
Children.push_back("红娃");
Children.push_back("橙娃");
Children.push_back("黄娃");
Children.push_back("绿娃");
Children.push_back("青娃");
Children.push_back("蓝娃");
Children.push_back("紫娃");
Families.insert(make_pair<string, vector<string>>("Zhao",Children));
Families.insert(make_pair<string, vector<string>>("Qian",Children));
Families.insert(make_pair<string, vector<string>>("Sun",Children));
Families.insert(make_pair<string, vector<string>>("Li",Children));
Families.insert(make_pair<string, vector<string>>("Zhou",Children));
Families.insert(make_pair<string, vector<string>>("Wu",Children));
Families.insert(make_pair<string, vector<string>>("Zheng",Children));
Families.insert(make_pair<string, vector<string>>("Wang",Children));
//
string Xing;
cin >> Xing;
//查找葫芦娃
auto iter = Families.find(Xing);
if (iter==Families.end())
cout << "没有找到姓" + Xing + "的葫芦娃" << endl;
else
{
vector<string> Huluwa = iter->second;
cout << "找到姓" + Xing + "的葫芦娃:" << endl;
for (unsigned i=0; i<Huluwa.size(); i++)
{
cout << Xing + Huluwa[i] << endl;
}
}
system("pause");
return 0;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询