wu
作者:互联网
#include<string>
#include<map>
#include<vector>
#include<iostream>
#include<utility>
#include<algorithm>
//字符串分割函数
std::vector<std::string> split(std::string str, std::string pattern)
{
std::string::size_type pos;
std::vector<std::string> result;
str += pattern;//扩展字符串以方便操作
int size = str.size();
for (int i = 0; i < size; i++)
{
pos = str.find(pattern, i);
if (pos < size)
{
std::string s = str.substr(i, pos - i);
result.push_back(s);
i = pos + pattern.size() - 1;
}
}
return result;
}
int cmp(const std::pair<std::string,int> &pairIt1,const std::pair<std::string,int> &pairIt2)
{
return pairIt1.second > pairIt2.second;
}
int main(void)
{
std::string str_input;
std::map<std::string,int> map_result;
std::vector<std::string> split_result;
std::vector< std::pair<std::string,int> > vector_sort;
std::string tmp(",");
getline(std::cin,str_input);
std::cout << str_input << std::endl;
split_result = split(str_input,tmp);
int size = split_result.size();
for(int i=0;i < size;i++)
{
map_result[split_result[i]]++;
std::cout << split_result[i] << std::endl;
}
std::map<std::string,int>::iterator obj = map_result.begin();
std::map<std::string,int>::iterator end = map_result.end();
for( ; obj!=end; obj++)
{
vector_sort.push_back(*obj);
std::cout << obj->second << std::endl;
}
sort(vector_sort.begin(),vector_sort.end(),cmp);
size = vector_sort.size();
for(int i = 0; i < size; i++)
{
std::cout <<vector_sort[i].first << " " << vector_sort[i].second << std::endl;
}
return 0;
}
标签:std,int,wu,vector,result,str,size 来源: https://www.cnblogs.com/kwdeblog/p/16148553.html