首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

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

2012-03-09 
编写程序统计并输出所读入的单词出现的次数#include iostream#includestring#includeutility#includ

编写程序统计并输出所读入的单词出现的次数
#include <iostream>
#include   <string>
#include   <utility>
#include   <map>
#include <stdlib.h>
using   namespace   std;


int   main()
{
    map <string,int> word_count;
    map <string,int> ::iterator   map_it=word_count.begin();

    string   word;
    while(cin> > word)
    ++word_count[word];
    cout < <map_it-> first;
    cout < <map_it-> second;
    system( "pause ");
  return   0;
}
我这个不对啊     该怎么编阿


[解决办法]
#include <iostream>
#include <string>
#include <map>
#include <cstdlib>
using namespace std;


int main()
{
map <string,int> word_count;
map <string,int> ::iterator map_it; //word_count.begin() is invalid because word_count is empty now

string word;
while(cin> > word)
++word_count[word];
for (map_it=word_count.begin(); map_it!=word_count.end(); ++map_it)
cout < <map_it-> first < < "\t " < <map_it-> second < <endl;
system( "pause ");
return 0;
}

热点排行