定义一个 map,关键字是家庭的姓,值是一个 vector,保存家中孩子(们)的名。编写代码,实现添加新的家庭以及向已有家庭中添加新的孩子。(
时间: 2023-05-11 19:04:37 浏览: 52
以下是代码实现:
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main() {
map<string, vector<string>> familyMap;
// 添加新的家庭
familyMap["张"] = {"小明", "小红"};
familyMap["李"] = {"小刚"};
// 向已有家庭中添加新的孩子
familyMap["张"].push_back("小华");
familyMap["李"].push_back("小丽");
// 输出家庭成员
for (auto& family : familyMap) {
cout << family.first << "家的孩子们:";
for (auto& child : family.second) {
cout << child << " ";
}
cout << endl;
}
return 0;
}
相关问题
定义一个 map,关键字是家庭的姓,值是一个 vector,保存家中孩子(们) 的名。编写代码,实现添加新的家庭以及向已有家庭中添加新的孩子。
好的,下面是 C++ 的代码实现:
```c++
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int main() {
map<string, vector<string>> family; // 定义一个 map,关键字是家庭的姓,值是一个 vector,保存家中孩子(们)的名
// 添加新的家庭以及向已有家庭中添加新的孩子
family["张"].push_back("三");
family["张"].push_back("四");
family["李"].push_back("小明");
family["李"].push_back("小红");
// 遍历 map 输出结果
for (auto iter = family.begin(); iter != family.end(); ++iter) {
cout << iter->first << " 家的孩子有:";
for (auto name : iter->second) {
cout << name << " ";
}
cout << endl;
}
return 0;
}
```
以上代码定义了一个 `map`,关键字是家庭的姓,值是一个 `vector`,用来保存每个家庭的孩子们的名字。通过 `family["姓"]` 的方式可以直接获取到该家庭的孩子们的 `vector`,然后再通过 `push_back` 方法向其中添加新的孩子的名字。最后遍历整个 `map`,输出每个家庭的孩子们的名字。
定义一个 map,关键字是家庭的姓,值是一个 vector,保存家中孩子的名。编写代码,实现添加新的家庭以及向已有家庭中添加新的孩子。
好的,以下是代码:
```
#include <iostream>
#include <map>
#include <vector>
using namespace std;
int main()
{
map<string, vector<string>> families;
// 添加新的家庭以及向已有家庭中添加新的孩子
string familyName, childName;
char choice;
do {
cout << "输入家庭的姓:";
cin >> familyName;
cout << "输入孩子的名字:";
cin >> childName;
// 检查家庭是否已存在
if (families.find(familyName) == families.end()) {
// 如果不存在,新建一个 vector 存储孩子名,并插入到 map 中
vector<string> children = {childName};
families.insert(make_pair(familyName, children));
} else {
// 如果已存在,直接加入到 vector 中
families[familyName].push_back(childName);
}
cout << "还要添加孩子吗?(y/n):";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
// 输出家庭及其成员
cout << "\n所有家庭及其成员名字如下:" << endl;
for (const auto& family : families) {
cout << family.first << " 家的孩子有:";
for (const auto& child : family.second) {
cout << child << " ";
}
cout << endl;
}
return 0;
}
```
你可以在程序中添加新的家庭及其成员名字,并且程序会将它们添加到一个名为 `families` 的 map 中,并输出所有的家庭及其成员。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)