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

想与习题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. 兼容的placeholder属性

    作为一个.net后台开发的程序猿,博客里既然大多都是前端相关的博文.是不是该考虑换方向了,转前端开发得了 ... 小小吐槽一下,近期受该不该跳槽所困惑,我有选择困难症! 继续前端,这次说一下输入框 p ...

  2. YII 小模块功能

    //1,使用updateCounters()来更新计数器字段. Book::model()->updateCounters(array('download_count'=>1),':id= ...

  3. MongoDB:The Definitive Guide CHAPTER 1 Introduction

    MongoDB is a powerful, flexible, and scalable data store. It combines the ability to scale out with ...

  4. hdu 4712 (随机算法)

    第一次听说随机算法,在给的n组数据间随机取两个组比较,当随机次数达到一定量时,答案就出来了. #include<stdio.h> #include<stdlib.h> #inc ...

  5. 关键字instanceof和final

    Instanceof关键字(类似oc的isKindOfClass 和 isMemberOfClass) instanceof(实例类型) 关键字作用: 1.判断某一个对象是否属于某一个类 2.inst ...

  6. IT工作中工资最高和增长最快的是什么

    Staff在博客IT Jobs with the Highest Pay and Fastest Growth中画了个图说明IT工作中工资最高和增长最快的是什么.  

  7. 分析IIS日志文件

    "D:\Program Files (x86)\Log Parser 2.2\logparser.exe" "SELECT * FROM 'D:\u_ex160405.l ...

  8. Excel文件上传

    *&---------------------------------------------------------------------* *& FORM FRM_UPDATA_ ...

  9. gwt中java与js的相互调用

    1. java通过jsni调用内部js Button button = new Button("java调用内部jsni的js方法"); button.addClickHandle ...

  10. Java程序猿学习C++之字符串

    #include <iostream> #include <string.h> using namespace std; int my_len(const char *str) ...