- 论坛徽章:
- 0
|
//calculate the time of the given words
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
using std::cout; using std::string;
using std::cin; using std::vector;
using std::endl; using std::sort;
int main()
{
//ask for input the words
cout << "input a word: ";
vector<string> st;
string s;
while(cin >> s )
st.push_back(s);
//sort the string st
sort(st.begin(),st.end());
//define two vectors vectors<string> st1 and vector<int> v
//to save the word and the time
vector<string> st1;
vector<int> v;
//loop variant i and count
int i = -1, count = 1;
//find the word
for(vector<string>::size_type iv = 0; iv != st.size(); ++iv){
i += 1;
while (st == st[i+1]){
++count;
++i;
}
st1.push_back(st);
v.push_back(count);
}
//output the word and the appear time
cout << "the word is: " << endl;
for(vector<string>::size_type j = 0; j != st1.size(); ++j)
cout << st[j] << "\t";
cout << "the time is: " <<endl;
for(vector<int>::size_type t = 0; t != v.size(); ++t)
cout << v[t] << "\t";
system("pause");
return 0;
}
这个WHILE循环怎么退出啊?退出后还要进行统计出现的字符数.比如:aaa bbb ccc aaa bbb ccc aa bb , 出现次数:aa 1,bb 1,aaa 2,bbb 2, ccc 2
[ 本帖最后由 bayernrimnm 于 2008-6-4 20:19 编辑 ] |
|