单词统计
Des
给一个比较长的字符串,统计一共有多少个单词,并输出最短和最长的单词
Code
#include <iostream>
#include <vector>
#include <map>
#include <sstream>
using namespace std;
int main() {
string str1="Airmail, or standard mail?";
string str2="Do you have anything cheaper?";
string str=str1+" "+str2;
map<string,int> m_arr;
istringstream isstrm(str);
string tmp;
int cnt=0;
int Max=-1;
while(isstrm>>tmp){
cnt++;
m_arr[tmp]=tmp.size();
if(Max<m_arr[tmp]){
Max=m_arr[tmp];
}
}
cout<<"共有单词:"<<cnt<<"最长的单词:"<<Max;
return 0;
}