http://blog.csdn.net/ly21st http://ly21st.blog.chinaunix.net
分类: C/C++
2011-11-22 10:20:55
#include
#include
#include
#include
#include
using namespace std;
版本1
/*
void count_word(ifstream &in,ofstream &out)
{
string word;
int err=0;
map
while(in>>word,!in.eof()) {
if(in.bad())
throw runtime_error("io data bad\n");
if(in.fail()) {
cout<<"io data fail"<
err++;
in.clear();
in.sync();
continue;
}
dict[word]++;
}
cout<<"发生读写错误:"<
for(map
{
out<<(*iter).first<<" "<<(*iter).second<
}
}
*/
版本2
void count_word(ifstream &in,ofstream &out)
{
string word;
int err=0;
map
while(in>>word,!in.eof()) {
if(in.bad())
throw runtime_error("io data bad\n");
if(in.fail()) {
cout<<"io data fail"<
err++;
in.clear();
in.sync();
continue;
}
pair< map
iter=dict.insert(make_pair(word,1));
if ( !iter.second )
(*iter.first).second++;
}
cout<<"发生读写错误:"<
for(map
{
out<<(*iter).first<<" "<<(*iter).second<
}
}
int main()
{
ifstream in("aa1.txt");
ofstream out("bb.txt");
count_word(in,out);
getchar();
return 0;
}