我记得这个问题我以前发过
哎~~
楼主还是自己想想
写写对自己有好处
#include<iostream>
#include<string>
#include<vector>
using namespace std;
inline bool compare(const string& s1, const string& s2)
{ return s1 == s2; }
int main()
{
vector<string> words;
vector<int> times;
cout << "Please enter some words"
" ctrl + z.";
cout << endl;
string s;
// words 包含了用户输入的所有的单词( 重复的仅有一次 )
// times 包含了该单词出现的次数
while(cin >> s)
{
vector<string>::size_type i = 0;
while(i != words.size())
{
if(compare(s, words[i]))
++times[i];
else
{
words.push_back(s);
times[i] = 1;
}
++i;
}
if(words.size() == 0)
{
words.push_back(s);
times[i] = 1;
}
}
vector<string>::size_type iter = 0;
//目前为止已输出了 iter 个单词 当然还有它出现的次数
while(iter != words.size())
{
cout << "Word " << words[iter] << ' '
<< times[iter] << " time(s). ";
cout << endl;
++iter;
}
cout << endl;
return 0;
}
[此贴子已经被作者于2006-3-31 17:14:28编辑过]