65,211
社区成员
发帖
与我相关
我的任务
分享
main()
{
map< string, int> mm;
string str;
while(cin>>str) //可以接收行,也可以多行,不想被getlien限制,结束输入请按q回车
{
if(str=="q")
break;
if(mm.find(str)!=mm.end())
mm[str]++;
else
mm[str]=0;
}
cout<<"input the words you want to check,'q' for quit"<<endl;
str="";
while(cin>>str&&str!="q")
{
if(mm.find(str)!=mm.end())
cout<<str<<": "<<mm[str]+1<<endl;
else
cout<<"No such words"<<endl;
}
return 0;
}
#include <iostream>
#include <sstream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
inline bool compare(const pair<string, int> &lhs, const pair<string, int> &rhs)
{
return lhs.second < rhs.second;
}
int main()
{
map<string, int> count;
string input;
getline(cin, input);
istringstream line(input);
string word;
while (line >> word)
count[word]++;
map<string, int>::iterator max = max_element(count.begin(), count.end(), compare);
cout << "max: " << max->first << ", " << max->second << " time(s)" << endl;
}
#include <sstream> //前面加个头文件。
...
...
int main()
{
vector<string> vec;
string sentence,word;
getline(cin,sentence);
stringstream ss(sentence);
while (ss>>word)
{
vec.push_back(word);
}
int res=findmax(vec);
cout<<vec[res]<<" "<<res<<endl;
system("pause");
return 0;
}
#include <iostream>
#include <string>
int main()
{
/* 在这里写程序 */
std::string result, last, input;
int max = 0, current = 0;
while(std::cin >> input)
{
if(last==input)
{
++current;
}
else
{
if( current > max )
{
max = current;
result = last;
}
last = input;
current = 1;
}
}
if( current > max )
{
max = current;
result = last;
}
std::cout<<std::endl<<"Count for string \""<<result<<"\" is "<<max<<std::endl;
return 0;
}