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吧~