这是我帮别人改的单词统计。。。你可以看看。。
#include <string>
#include <map>
#include <iostream>
using namespace std;
int main()
{
char str[500];
char *strToken;
char strDelimit[] = " ,.?!";
int wordCount = 0;
map<string, int> words;
map<string, int>::iterator iter;
cout << "Please input a passage:" << endl;
cin.getline( str, sizeof(str) );
strToken = strtok( str, strDelimit );
while ( strToken != NULL )
{
iter = words.find(strToken );
if ( iter == words.end() )
{
words.insert( pair<string, int>(strToken, 1 ) );
}
else
{
iter->second++;
}
++wordCount;
strToken = strtok( NULL, strDelimit );
}
for ( iter=words.begin(); iter!=words.end(); ++iter )
{
cout << "Words begin with " << iter->first << ": " << iter->second << endl;
}
cout << "Total words: " << wordCount << endl;
}