c++ 统计单词数量

 

#include <iostream>
#include <string>
#include <map>

using namespace std;

 

 

int main()
{

map<string,int> myMap;

string str = "Statistics suggest that the population of this country will be double in ten yearsLondoners are under starter's orders as the city gets ready for the Olympic Games, which will begin one year today.To mark the start of the 366-day countdown (2012 is a leap year), special events are planned for today.The design of the Olympic medals will be unveiled tonight in a live ceremony from Trafalgar Square.Over at the brand new Aquatics Centre, Britain's star diver Tom Daley is going to perform an official launch dive into the Olympic pool.With this buildthe organisers have attempted to give London a landmark to rival Beijing's Water Cube from 2008.It was designed by the prestigious architect Zaha Hadid and has a wave-like roof that is 160 metres long.Today's special events are designed to arouse interest in the Olympics around the world and to encourage British fans too.Many failed to get Olympic tickets in the recent sales process.According to a new survey for the BBC, 53% of Londoners think the process was not fair.But the same survey found support is growing for London 2012. Of the 1,000 people surveyed, 73% said they backed the Games - up from 69% in 2006.";

cout << str << endl;
int start = 0;
int end = 0;

map<string, int>::iterator iter;

while(1)
{
start = end ;
if(start >= str.length() )
{
break;
}

end = str.find(" ",start);
if(end <= 0 )
{
end = str.length();    

}


string keyStr = str.substr(start,end - start);

iter = myMap.find( keyStr );
if(iter == myMap.end() )
{
myMap.insert(pair<string, int>(keyStr, 1));
cout << "Insert " << keyStr << endl;

}
else
{
iter->second = iter->second + 1;


}
end++;


}

cout << "Start" << endl;

for( iter = myMap.begin(); iter != myMap.end(); iter++ )
{
cout << iter->first << "->" << iter->second << endl;


}

cout << "End" << endl;

return 0;
}

 

转载于:https://www.cnblogs.com/zswang2018/p/10484670.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值