c++統計単語数

4980 ワード

#include 
#include <string>
#include 

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