C++ primer 第十章参考答案 10.9

news/2024/7/2 21:27:10
 

习题10.9   编写程序统计并输出所读入的单词出现的次数

方法一:

#include<iostream>
#include<vector>
#include<map>
#include<string>
using namespace std;
int main()
{
   map<string,int> word_count;
 string word;
 while(cin>>word)
 {
 
  ++word_count[word];
 }
 for(map<string,int>::iterator map_it = word_count.begin();map_it!=word_count.end();++map_it)
  cout<<map_it->first<<" "<<map_it->second<<endl;
 

 cout<<endl;
 return 0;
}

方法二:

#include<iostream>
#include<vector>
#include<map>
#include<string>
using namespace std;
int main()

 map<string,int> word_count;
 string word;
 while(cin>>word)
 {
  pair<map<string,int>::iterator,bool> p = word_count.insert(make_pair(word,1));

  /*

 

p为pair类型变量,第一个元素为map<string,int>容器的迭代器,第二个元素为bool类型。insert操作在word_count容器中加入一个键为string word,值为int 1的对象;insert操作返回pair赋给p,则p的第一个元素迭代器指向的键为string word,且当word在word_count中不存在时p的第二个元素为true,存在时为false。后面的语句if(p.second == false)即判断当word在word_count中存在时,执行if语句内操作。

 

*/
  if(p.second==false)
  {
  
   ++p.first->second;

/*

可理解为:

++(*(p.first).second);

p.first为指向word_count容器内键为string word对象的迭代器,对其解引用得到word_count容器内键为string word的对象,该对象类型为map<string,int>::valut_type。value_type为pair类型,对该对象执行.second得到第二个元素类型为int,在该程序内即为word出现的数量。最后对该int类型的第二元素执行自增操作。

 

*/
  }
 
 }
 for(map<string,int>::iterator map_it = word_count.begin();map_it!=word_count.end();++map_it)
  cout<<map_it->first<<" "<<map_it->second<<endl;
 

 cout<<endl;
 return 0;
}


http://www.niftyadmin.cn/n/2278888.html

相关文章

文件的基本操作 C语言版

文件的写操作&#xff1a; void CMYCFileView::OnFileWrite() { // TODO: Add your command handler code here //打开文件 FILE *pFile fopen("111.txt","w"); //写入操作 fwrite("http://www.baidu.com",1,strlen("http://www.ba…

区分文本文件和二进制文件

区分文本文件和二进制文件 当按文本方式向文件中写入数据时&#xff0c;一旦遇到“换行”字符&#xff08;ASCII码为10&#xff09;,则会转换为“回车-换行”&#xff08;ASCII码为13、10&#xff09;。在读取文件时&#xff0c;一旦遇到“回车-换行”的组合&#xff08;连续的…

二进制文件和文本文件的例子

给定一个整数&#xff0c;如 97865将这个整数保存在文件中&#xff0c;要求以记事本程序打开该文件时&#xff0c;显示 97865 FILE *pFile fopen("3.txt","w"); int i 98743; char ch[5]; ch[0] 948; ch[1] 848; ch[2] 748; ch[3] 448; ch[4]…

make, gmake, Makefile简明教程

0 Makefile概述 什么是makefile&#xff1f;makefile关系到了整个工程的编译规则。一个工程中的源文件不计数&#xff0c;其按类型、功能、模块分别放在若干个目录中&#xff0c;makefile定义了一系列的规则来指定&#xff0c;哪些文件需要先编译&#xff0c;哪些文件需要后编…

GDAL的安装和配置---出现的问题

一、linux下的安装和使用 1.安装 下载源程序包 &#xff0c;解压&#xff0c;运行以下三条命令 Java代码 ./configure make make install ./configuremakemake install 在/usr/local/lib目录下会出现编译好了的相关gdal库&#xff0c;我用的redhat linux下的一个问题…

主题:Linux平台gcc和动态共享库的基础知识

对大多数不从事Linux平台C语言开发的人来说&#xff0c;GNU gcc的一套工具和Linux平台的共享库的使用还是十分陌生的&#xff0c;其实我也不太熟悉&#xff0c;姑且写点基础知识&#xff0c;权当做备忘吧。 一、GNU gcc的编译工具用法 我们先来写一个简单的C程序&#xff1a;he…

配置mapserv出现了问题。。。

Q: I installed the PROJ.4, GDAL, or one of the support libraries on my system, it is recognized by MapServer’s “configure” as a system lib but at runtime I get an error: “libproj.so.0: No such file or directory”. A: You are probably running a RedHat L…

Ubuntu 桌面图标不见,鼠标右键的问题

主要步骤&#xff1a;1 终端运行&#xff1a;sudo apt-get install nautilus 2 终端运行&#xff1a;sudo nautilus 虽然自己的文件系统是可以打得了&#xff0c;但是自己的系统选线的图标还是没有出来&#xff0c;又找了很多的帖子问题还是没解决&#xff0c;突然自己想是不是…