《C++ Primer》P314中使用insert重写单词统计程序的扩展
编写程序统计并输出所读入的单词出现的次数
想与习题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重写单词统计程序的扩展的更多相关文章
- Eclipse上运行第一个Hadoop实例 - WordCount(单词统计程序)
需求 计算出文件中每个单词的频数.要求输出结果按照单词的字母顺序进行排序.每个单词和其频数占一行,单词和频数之间有间隔. 比如,输入两个文件,其一内容如下: hello world hello had ...
- 第六篇:Eclipse上运行第一个Hadoop实例 - WordCount(单词统计程序)
需求 计算出文件中每个单词的频数.要求输出结果按照单词的字母顺序进行排序.每个单词和其频数占一行,单词和频数之间有间隔. 比如,输入两个文件,其一内容如下: hello world hello had ...
- [Swift]LeetCode720. 词典中最长的单词 | Longest Word in Dictionary
Given a list of strings words representing an English Dictionary, find the longest word in words tha ...
- PL/SQL客户端中执行insert语句,插入中文乱码
问题描述:在PL/SQL客户端中执行insert语句,插入中文乱码 解决方案: 1.执行脚本 select userenv('language') from dual; 结果为AMERICAN_ ...
- Oracle一个事务中的Insert和Update执行顺序
今天碰到了一个奇怪的问题,是关于Oracle一个事务中的Insert和Update语句的执行顺序的问题. 首先详细说明下整个过程: 有三张表:A,B,C,Java代码中有一段代码是先在表A中插入一条数 ...
- 转载MSDN 在ASP.NET 中执行 URL 重写
转载文章原网址 http://msdn.microsoft.com/zh-cn/library/ms972974.aspx 摘要:介绍如何使用 Microsoft ASP.NET 执行动态 URL 重 ...
- 2、 Spark Streaming方式从socket中获取数据进行简单单词统计
Spark 1.5.2 Spark Streaming 学习笔记和编程练习 Overview 概述 Spark Streaming is an extension of the core Spark ...
- C++中重载、重写(覆盖)和隐藏的区别实例分析
这篇文章主要介绍了C++中重载.重写(覆盖)和隐藏的区别,是C++面向对象程序设计非常重要的概念,需要的朋友可以参考下 本文实例讲述了C++中重载.重写(覆盖)和隐藏的区别,对于C++面向对象程序设计 ...
- MySQL中的insert ignore into, replace into等的一些用法小结(转)
MySQL中的insert ignore into, replace into等的一些用法总结(转) 在MySQL中进行条件插入数据时,可能会用到以下语句,现小结一下.我们先建一个简单的表来作为测试: ...
随机推荐
- HDOJ 1004题 Let the Balloon Rise strcmp()函数
Problem Description Contest time again! How excited it is to see balloons floating around. But to te ...
- linux 多线程基础2
6. 名称:: pthread_detach 功能: 使线程进入分离状态. 头文件: #include <pthread.h> 函数原形: int pthread_detach(pthre ...
- 有两个数组a,b,大小都为n,;通过交换a,b中的元素,使sum(a)-sum(b)最小。
有两个数组a,b,大小都为n,数组元素的值任意整形数,无序: 要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小. 当前数组a和数组b的和之差为 A = sum(a) - ...
- Application使用示例
MainActivity如下: package cn.testapplication; import android.os.Bundle; import android.view.View; impo ...
- Code Forces 711D Directed Roads
D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- 【学习】ABAP OLE 对EXCEL的处理
原文:http://blog.sina.com.cn/s/blog_7229b9c00100opx2.html -------------------------------------------- ...
- Lamda和Linq语法对比详细
本人转载:http://www.cnblogs.com/knowledgesea/p/3897665.html 闲言碎语 近期比较忙,但还是想写点什么,就分享一些基础的知识给大家看吧,希望能帮助一些l ...
- 安卓开发中Theme.AppCompat.Light的解决方法
styles.xml中<style name="AppBaseTheme" parent="Theme.AppCompat.Light">提示如下错 ...
- BA的广度和深度
http://insights.thoughtworkers.org/ba-capability-and-development-path-in-thoughtworks/ 作者:ThoughtWor ...
- 即时通讯(IM)
即时通讯(IM)功能是APP的重要功能之一,而开发好移动IM却绝非易事.通常来说,IM技术选型至少要解决以下问题:1. 协议选型 2. IM服务器选型 3. 对协议和服务器做相应修改,通常来说直接拿个 ...