《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中进行条件插入数据时,可能会用到以下语句,现小结一下.我们先建一个简单的表来作为测试: ...
随机推荐
- Apache FileUpload详细介绍
Apache FileUpload组件 在最初的 http 协议中,没有上传文件方面的功能.RFC1867("Form-based File Upload in HTML".)为 ...
- 荷兰国旗问题 划分成3部分 leecode
public class Solution { public void sortColors(int[] A) { int len=A.length; int beg=0; int end=len-1 ...
- nodejs 基本问题答疑
传统服务器架构1.服务器接到请求2.创建一个新的响应程序的实例 node服务器架构1.服务器接到请求2.检查处理程序是否存在,如果存在直接处理,不存在则创建. node的做法节省了内存 ubuntu下 ...
- SSL 通信及 java keystore 工具介绍
http://www.javacodegeeks.com/2014/07/java-keystore-tutorial.html Table Of Contents 1. Introduction 2 ...
- 关于升级linux下apache
1:Check whether Apache is already installed. # rpm -qa | grep -i http httpd-tools-2.2.8-3.i386 httpd ...
- typeahead使用配置参数。
示例代码: var suggestion_source = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace( ...
- 非常棒的jqChart图表插件
由于项目需要,做一些类似于OA系统的应用时,表格用到的是最多的,那么图表展示就先显得尤为的重要,其中柱柱状图.折线图.饼状图又居多. 之前一直使用的是Chrome下的googleAPI中提供的char ...
- PAT---1005. Spell It Right (20)
#include<iostream> #include<stack> #include<string.h> ]= {"zero", " ...
- Show Global Status 整理
原文来源:MySQL 5.5 Reference Manual 部分翻译取自:<MySQL_5.1中文参考手册> 转载请注明原文链接http://www.cnblogs.com/lenag ...
- 装有Win7系统的电脑在局域网不能共享的解决方案
Win7系统的网络功能比XP有了进一步的增强,使用起来也相对清晰.但是由于做了很多表面优化的工作,使得底层的网络设置对于习惯了XP系统的人来说变得很不适应,其中局域网组建就是一个很大的问题.默认安装系 ...