在这里,我试图使用地图技术解决我的问题。 [英] Here I am trying to solve my problem using maps technique.

查看:74
本文介绍了在这里,我试图使用地图技术解决我的问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用maps.t这是我试图实现的代码。当我接受输入string.it继续输入但不输出code.how我可以修改此代码,以便我可以得到我的要求结果。



我的尝试:



i just started with maps.this is the code i tried to implement.when i take input for string.it keeps taking input but doesn't output the code.how can i modify this code so that i can get my required result.

What I have tried:

#include<iostream>
#include<conio.h>
#include
#include<string>
#include<iterator>

using namespace std;

int main()
{
        map<string, int> stringCounts;
	string str;

	while (getline(cin, str)) 
          stringCounts[str]++;

	map<string, int>::iterator iter;
	for (iter = stringCounts.begin(); iter != stringCounts.end(); iter++)
	{
	cout << "word: " << iter->first << ", count: " << iter->second << endl;
	}
	_getch();
}

推荐答案

实际上你的代码(或它的编译版本)有效。

Actually your code (or a compiling version of it) works.
#include <iostream>
#include <map>
#include <string>
#include <iterator>

using namespace std;

int main()
{
  map<string, int> stringCounts;
  string str;

  while (getline(cin, str))
    stringCounts[str]++;

  map<string, int>::iterator iter;
  for (iter = stringCounts.begin(); iter != stringCounts.end(); iter++)
  {
    cout << "word: " << iter->first << ", count: " << iter->second << endl;
  }
}



样本输入:


sample input:

as
asas
asas
aa



输出


output

word: aa, count: 1
word: as, count: 1
word: asas, count: 2





[update]

使用现代 C ++编写的相同程序



[update]
The same program written using modern C++:

#include <iostream>
#include <map>

using namespace std;

int main()
{
  map<string, int> stringCounts;
  string str;

  while (getline(cin, str))
    stringCounts[str]++;

  for (auto p : stringCounts)
    cout << "word: " << p.first << ", count: " << p.second << endl;
}



[/ update]


这篇关于在这里,我试图使用地图技术解决我的问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆