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

想与习题10-1相结合,也就是先输入几组 map<string, int>类型,存入vector中。

再输入单词word,如果已经存在则在key对应的value+1

如果不存在,则插入并使得其value为1.

之前的问题是-》输入了一次之后,再要输入单词word,读不进。(呵呵 果然小白)

看到11章之后,知道要用语句cin.clear;使得输入流重新有效。

再然后 重新熟悉了iterator对map的操作。

#include <iostream>
#include <utility>
#include<vector>
#include <string>
#include <map> using namespace std; int main()
{
pair<string,int > sipr;
string str;
int ival;
vector< pair<string,int> > pvec;
map<string,int> word_count;
cout<<"Enter a string and an integer ( Ctrl + Z to end) : "
<<endl;
while (cin >>str>>ival )
{
sipr = make_pair(str, ival);
pvec.push_back(sipr);
word_count.insert(map<string, int>::value_type(str, ival) );
} string num;
cin.clear();
while ( cin>>num )
{
pair<map<string,int>::iterator , bool> ret=
word_count.insert(make_pair(num, ));
if (!ret.second)
{
++ret.first->second;
cout<<ret.first->first<<" 的值变为:"<<ret.first->second<<endl;
}
}
map<string,int>::iterator it = word_count.begin();
while (it !=word_count.end())
{
cout<<"key:" <<it->first<<"value:"<< it->second <<endl;
it++;
} return ;
}

《C++ Primer》P314中使用insert重写单词统计程序的扩展的更多相关文章

  1. Eclipse上运行第一个Hadoop实例 - WordCount(单词统计程序)

    需求 计算出文件中每个单词的频数.要求输出结果按照单词的字母顺序进行排序.每个单词和其频数占一行,单词和频数之间有间隔. 比如,输入两个文件,其一内容如下: hello world hello had ...

  2. 第六篇:Eclipse上运行第一个Hadoop实例 - WordCount(单词统计程序)

    需求 计算出文件中每个单词的频数.要求输出结果按照单词的字母顺序进行排序.每个单词和其频数占一行,单词和频数之间有间隔. 比如,输入两个文件,其一内容如下: hello world hello had ...

  3. [Swift]LeetCode720. 词典中最长的单词 | Longest Word in Dictionary

    Given a list of strings words representing an English Dictionary, find the longest word in words tha ...

  4. PL/SQL客户端中执行insert语句,插入中文乱码

    问题描述:在PL/SQL客户端中执行insert语句,插入中文乱码 解决方案: 1.执行脚本 select userenv('language') from dual;    结果为AMERICAN_ ...

  5. Oracle一个事务中的Insert和Update执行顺序

    今天碰到了一个奇怪的问题,是关于Oracle一个事务中的Insert和Update语句的执行顺序的问题. 首先详细说明下整个过程: 有三张表:A,B,C,Java代码中有一段代码是先在表A中插入一条数 ...

  6. 转载MSDN 在ASP.NET 中执行 URL 重写

    转载文章原网址 http://msdn.microsoft.com/zh-cn/library/ms972974.aspx 摘要:介绍如何使用 Microsoft ASP.NET 执行动态 URL 重 ...

  7. 2、 Spark Streaming方式从socket中获取数据进行简单单词统计

    Spark 1.5.2 Spark Streaming 学习笔记和编程练习 Overview 概述 Spark Streaming is an extension of the core Spark ...

  8. C++中重载、重写(覆盖)和隐藏的区别实例分析

    这篇文章主要介绍了C++中重载.重写(覆盖)和隐藏的区别,是C++面向对象程序设计非常重要的概念,需要的朋友可以参考下 本文实例讲述了C++中重载.重写(覆盖)和隐藏的区别,对于C++面向对象程序设计 ...

  9. MySQL中的insert ignore into, replace into等的一些用法小结(转)

    MySQL中的insert ignore into, replace into等的一些用法总结(转) 在MySQL中进行条件插入数据时,可能会用到以下语句,现小结一下.我们先建一个简单的表来作为测试: ...

随机推荐

  1. 关于MySQL的分区(partion)

    1 CREATE TABLE part_tab ( c1 int default NULL, c2 ) default NULL, c3 date default NULL ) engine=myis ...

  2. C++域宽设置

    域宽设置,域宽填充; 设置域宽, cout<<set[w-width](int n)<<被设置的输出内容 设置填充字符, cout<<setfill(char n) ...

  3. table 的thead th 固定 tbody滚动例子

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  4. Android——C语言、JNI与低层调用

    JNI java native interface c的基本数据类型 int:32位,能表示的数字是2的32次方个 最高位用来表示符号位,那么还剩下31位可以表示数值,所以能表示的数字就是2的31次方 ...

  5. Js- 在一个JS文件中引用另一个JS文件

    在调用文件的顶部加入下例代码: document.write(”<script language=javascript src=’/js/import.js’></script> ...

  6. SEO为什么要求网页设计师用DIV+CSS布局网页?

    问:SEO为什么要求网页设计师用DIV+CSS布局网页? 答:通常情况下,SEOer非常喜欢把一个网站做到最细节,在网页设计方面,有时与设计师沟通时,通常会问到:SEO为什么要求网页设计师用DIV+C ...

  7. MobilePhone正则表达式

    电话号码正则表达式(支持手机号码,3-4位区号,7-8位直播号码,1-4位分机号) ((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3} ...

  8. 【转】cocos2d-x中锚点设置及定位方式

    http://blog.csdn.net/wayne5ning/article/details/8160506 说在前面:以下是基于cocos2d-2.0-x-2.0.3作的总结 问题 在cocos2 ...

  9. Director Scene Layer and Sprite

    Scenes Director Layers Multiple Layers Example: Sprites References Scenes A scene (implemented with ...

  10. java的一些程序

    1.文件读取并打印 import java.io.*;public class readandprint{//*********Found********public static void main ...