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

想与习题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. Web---HTTP请求、重定向、转发和数据压缩

    HTTP常用的请求方式包括: GET-最为常见,但发送的数据量很小,发送的数据直接包含到url的后面. POST-可以包含大量数据,数据在请求正文中通过表单进行提交. HEAD,PUT,DELETE. ...

  2. IO流的应用————小型资源管理器

    小型资源管理器 private void LoadTreeView() { DirectoryInfo dir = new DirectoryInfo(@"E:\"); Direc ...

  3. 开发小技巧:C#逐个输出字符

    静态自定义方法: static int counter = 0; static string displayString = "This string will appear one let ...

  4. JAVA--线程wait()、lnotify()和notifyAll()方法

    join()方法是Thread类的一个方法,而wait().notify().notifyAll()是java.lang.Object类的方法,这意味着,任何一个Java对象(包括线程对象)都有wai ...

  5. BigInteger Uva

    import java.io.*; import java.math.BigInteger; import java.util.*; public class Main { public static ...

  6. coco2dx 精灵类

    CCSize size = CCDirector::sharedDirector()->getWinSize(); CCSprite *bg = CCSprite::create("H ...

  7. Hibernate学习之表一对多,多对一关系

    代码: person类: public class Person { private long id; private String name; private int age; private Da ...

  8. swift小结02-基础篇

    闭包      类似于 OC 中的 Block,是一段预先定义好的代码,在需要时执行   闭包表达式格式:  { (形参名称1: 形参类型1, 形参名称2: 形参类型2, ...) -> 返回值 ...

  9. eclipse中java项目的build path详解(转载)

    BuildPath中只支持加入jar文件,具体方法如下:在eclips里在工程名上右键->build path->contigure bud path->java build pat ...

  10. HDU 2196 树形DP Computer

    题目链接:  HDU 2196 Computer 分析:   先从任意一点开始, 求出它到其它点的最大距离, 然后以该点为中心更新它的邻点, 再用被更新的点去更新邻点......依此递推 ! 代码: ...