C++Primer第五版 练习11.14(解答)
2015-09-29 14:27
387 查看
练习11.14:扩展你在11.2.1节练习(第378页)中编写的孩子姓到名的map,添加一个pair的vector,保存孩子的名和生日。
参考练习11.7
C++Primer第五版 练习11.7(解答)
参考练习11.7
C++Primer第五版 练习11.7(解答)
/* *练习11.14.cpp *2015/9/29 *问题描述: 练习11.14:扩展你在11.2.1节练习(第378页)中编写的孩子姓到名的map,添加一个pair的vector,保存孩子的名和生日。 练习11.7:定义一个map,关键字是家庭的姓,值是一个vector,保存家中孩子(们)的名。编写代码,实现添加新的家庭以及向已有家庭中添加新的孩子。 *说明:弄清楚pair的类型,map的类型 *作者:Nick Feng *邮箱:nickgreen23@163.com */ #include <iostream> #include <string> #include <map> #include <vector> #include <utility> using namespace std; //定义一个Date类,表示生日,需要说明的是,该Date类很简单,没有做类型检查 struct Date{ int year; int month; int day; Date() = default; //空构造函数不写,后果很严重,不妨注释掉看看 Date(int y,int m,int d) : year(y), month(m), day(d) { } void print(){ cout << year << "-" << month << "-" << day << endl; } }; int main() { string fname; string name; int year,month,day; pair<string,Date> p; //pair保存孩子的名和生日 //该类型稍复杂,关键字string类型,用于保存家族姓氏 //第二个元素是vector中存放pair保存孩子的名和生日向量 map<string,vector<pair<string,Date>>> family; //依次输入姓氏,名字,年,月,日 while(cin >> fname >> name >> year >> month >> day) { Date d(year,month,day); p = {name,d}; family[fname].push_back(p); } for(auto &member : family) { cout << "Member is:" << " " << endl; for(auto it = member.second.begin(); it != member.second.end();++it) cout << (*it).first << "." << member.first << " " << (*it).second.year << "-" << (*it).second.month << "-" << (*it).second.day << endl; cout << endl; } return 0; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中ipair和pair的区别
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性