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

c++ 文件行重复有关问题

2012-03-29 
c++ 文件行重复问题一个文件里面有很多行,其中有一些行是重复的比如文件如下111222333111444222111-------

c++ 文件行重复问题
一个文件里面有很多行,其中有一些行是重复的

比如文件如下
111
222
333
111
444
222
111

---------------------------------
最终要把出现2次以上的内容输出
也就是
4 111
6 222
7 111
==
第4行和第7行的111
第6行的222
是重复出现的



[解决办法]

C/C++ code
#include <iostream>#include <fstream>#include <string>#include <map>using namespace std;void main(void)              {    ifstream input("d:\\1.txt");    string str;    map<string,int> string_count ;    while(input>>str){                ++string_count[str];    }        map<string,int>::iterator it=string_count.begin();        cout<<"字符串"<<"  "<<"出现次数"<<endl;    while(it!=string_count.end()){            cout<<it->first<<"     "<<it->second<<endl;        it++;    }    input.close();}
[解决办法]
用stl的map吧~

热点排行
Bad Request.