第一部分:自己编写的程序:

#include <string>
#include <iostream>
#include <map>
#include <fstream>
using namespace std;
void print(multimap<string,string>&);
void add(istream &is,multimap<string,string> &m){
string surname,name;while(is >> surname && is >> name){m.insert(make_pair(surname,name));}}int main(int argc,char **argv)
{
ifstream in(argv[1]);
multimap<string,string> m;
add(in,m);
print(m);
for(auto & i : m){cout << i.first << "  ";cout << i.second << endl;}return 0;
}void print(multimap<string,string> &m)
{
multimap<string,string>::iterator it;
for(it = m.begin(); it != m.end();++ it){  cout << it->first << " \n\t" << it->second << endl;}}

data.txt文件内容:

王 伟
苏 州
杭 州
美 利达
捷 安特
捷 豹

编译,运行,结果为:

王 伟
苏 州
杭 州
美 利达
捷 安特
捷 豹

第二部分:答案上的程序:

#include <string>
#include <iostream>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <fstream>
using namespace std;
typedef map<string,string> families;
void add_child(multimap<string,string> &families,const string & surname,const string & name)
{families.insert({surname,name});
}int main(int argc,char **argv)
{
multimap<string,string> families;
add_child(families,"张","叉叉");
add_child(families,"郭","东东");
add_child(families,"王","娃娃");
for (auto & i : families){cout << i.first <<  " family 's child is:" << i.second << endl; }
return 0;
}

执行结果是:

张 family 's child is:叉叉
王 family 's child is:娃娃
郭 family 's child is:东东

c++ primer 5th ,习题11.23自己编写答案相关推荐

  1. c++ primer 5th 习题11.14自己编写的答案

    文件 1.cc #include <string> #include <iostream> #include <fstream> #include <list ...

  2. c++ primer 5th 练习11.38自己编写答案(用无序容器重写单词计数程序)

    文件main.cc #include <string> #include <iostream> #include <unordered_map> #include ...

  3. c++ primer 5th,练习11.19,编写代码验证

    对于这个题,自己话费2个多小时编写了代码,亲自验证正确性,顺便复习类的知识点,下面采用分离式编译自己把代码放在三个文件中main.cc,function,sales_data.h,大家应该根据文件名推 ...

  4. c++ primer 5th 练习11.9自己编写的答案

    #include <string> #include <iostream> #include <list> #include <vector> #inc ...

  5. c++primer 5th习题12.25答案

    #include <memory> #include <cstdlib> using namespace std; int main() { char *p = new cha ...

  6. c++ primer 5th第13章拷贝控制知识点和自编习题答案

    首先,先给大家提个醒.在网上的随书源代码里关于hasptr类的类指针版本编写的移动构造函数.移动赋值运算符.和析构函数部分是有错误的.大家可以把hasptr累指针版本(里面带移动构造函数和移动赋值运算 ...

  7. C++ Primer - 5th Edition - 书中源代码 - 课后习题答案

    C++ Primer - 5th Edition - 书中源代码 - 课后习题答案 C++ Primer - 5th Edition - 书中源代码 - 课后习题答案 1. C++ Primer, 5 ...

  8. c++ primer 5th,习题13.3 答案是不是错了?(未解决)

    c++ primer 5th,习题13.3 答案是不是错了?如下: 这两个类都未定义拷贝构造函数,因此编译器为它们定义了合成的拷贝构造函数.合成的拷贝构造函数逐个拷贝非 const成员,对内置类型的成 ...

  9. C++ Primer 第10章 习题10.23

    //10.23.cpp //编写程序将被排除的单词存储在vector对象中,而不是存储在set对象中 //函数restricted_wc,根据形参指定文件建立单词排除集 //将被排除的单词存储在vec ...

最新文章

  1. 状态保持中的cookie
  2. NanoPi NEO Air使用十五:使用V4L2驱动USB摄像头
  3. Apple Pay与银联的联姻,是战略,不是产品
  4. 操作系统的中断和异常
  5. 计算机科学基本理论,计算机科学的基础知识.ppt
  6. CV Code|计算机视觉开源周报20200502期
  7. lvs+keepalived+nginx+tomcat高可用高性能集群部署
  8. Mac电脑如何一键打开常用的app?
  9. 74cms php在那个文件夹里面,74cms骑士人才网站系统 v5.0/v5.1后台目录地址修改教程...
  10. 跟着海盗头子创业是一种怎样的体验?
  11. 视频直播APP开发费用多少钱?
  12. win10用计算机分区,win10电脑硬盘要不要分区
  13. java邮箱exchange_Exchange服务器之java mail 使用:exchange邮箱
  14. 【JAVA】java递归测试考拉兹猜想/冰雹猜想
  15. 验证码图片显示不出文字
  16. 网站服务器日常管理,网站管理员的日常生活
  17. Ubuntu18.04 安装搜狗输入法
  18. proteus教程——cc4021串行输入
  19. xlWarKey v2.0 手记
  20. 一文了解SAAS开发、模板定制开发、全定制开发

热门文章

  1. plsql cursor 函数
  2. php 自带过滤和转义函数
  3. 乱想-HTTP请求常用对象
  4. 关于MultipleOutputFormat若干小记
  5. LOG4J 警告错误处理办法(转)
  6. WinAPI: Pie - 绘制饼图
  7. WinAPI: GetUserName - 获取当前用户名
  8. 一個textlist控件(左右兩個文本框)。
  9. 牛客网(剑指offer) 第十五题 反转链表
  10. [Python学习] 专题一.函数的基础知识