【C】单词统计

来源:互联网 发布:dnf自动存装备源码 编辑:程序博客网 时间:2024/06/06 15:41


给出一个字符串,输出字符串中单词的重复次数,并按字母升序排序。

标准输入:wo de ming zi shi acmer wo ai acmer

标准输出:acmer 2

              ai  1

          de  1

              ming 1

              Shi  1

              wo  2

              Zi   1


答案:

#include<iostream>
#include<map>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main ()
{
map<string,int> my;
vector<string> note;
string str;
while(cin>>str)
{
my[str]++;
note.push_back(str);

}
sort(note.begin(),note.end());
vector<string>::iterator pt=note.begin();
for(;pt!=note.end();pt++)
{
if(pt==note.begin())
{
cout<<*pt<<' '<<my[*pt]<<endl;
str=*pt;
}
else
{
if(*pt==str)
continue;
else
{
cout<<*pt<<' '<<my[*pt]<<endl;
str=*pt;
}
}
}
return 0;
}       

原创粉丝点击