(一)若每个单词之间只有空格或换行符,不含其它的标点符号,则可以直接读单词
#include<iostream>
#include<fstream>
#include<assert.h>
#include<string.h>
#include<map>
using namespace std;
int main()
{
ifstream in("D:\\test.txt");
assert(in!=NULL);
map<string,int>mvc;
string word;
while(in>>word)
mvc[word]++;
map<string,int>::const_iterator beg=mvc.begin();
for(;beg!=mvc.end();beg++)
cout<<beg->first<<":"<<beg->second<<endl;
return 0;
}
(二)若每个单词之间是其它的标点符号(如,或.)则可以按照以下方法读每个单词.
#include<iostream>
#include<fstream>
#include<assert.h>
#include<string>
#include<map>
using namespace std;
int main()
{
ifstream in("D:\\111.txt");
assert(in