问题描述
如果有两个char使用相等,首先输出。
例如:
char str [] =" gbdfssdffss" ;;
out是:f。
if have two char use equality, out first.
example:
char str[] = "gbdfssdffss";
out is: f.
推荐答案
填充此数组:
char cnt [26];
然后查看数组中哪个条目具有最大值。可能还有一些令人费解的方式用STL做这个令人费解的方式。
Fill this array:
char cnt[26];
Then see which entry in the array has the largest value. There may be some
obnoxious, convoluted way to do it with the STL too.
也有一些令人讨厌的方式来处理STL。
是的。看看这段美妙的代码:
#include< iostream>
#include< map>
#include< algorithm>
using namespace std;
typedef string :: iterator s_iter;
//用于计算字符
typedef map< char,size_t> charSet;
typedef charSet :: iterator cs_iter;
//用于排序结果
typedef multimap< size_t,char,greater< size_t> > countSet;
typedef countSet :: iterator cnt_iter;
//计算字符的算符
class countChar
{
私人:
charSet& cs;
public:
countChar(charSet& chs):cs(chs){}
void operator()(char c)
{
size_t count = 1;
cs_iter csi = cs.find(c);
if(csi!= cs.end())
{
count = csi-> second +1;
cs.erase(csi);
}
cs.insert(make_pair(c,count));
}
};
int main()
{
string str;
cout<<"输入你的字符串:" ;;
cin>> str;
if(str.empty())
{
cout<<"这个字符串是空的!!" ;;
返回-1;
}
charSet cs;
for_each(str.begin(),str.end(),countChar(cs));
countSet cnts;
for(cs_iter csi = cs.begin(); csi!= cs.end(); ++ csi)
{
cnts.insert(make_pair(csi-> second,csi-> first) ));
}
cout<<"最常用的字符是:" ;;
cnt_iter ci = cnts.begin ();
size_t重复ions = ci-> first;
for(;(ci!= cnts.end())&&(ci-> first == repetitions); ++ ci)
{
cout<<''''<< ci-> second;
}
}
some obnoxious, convoluted way to do it with the STL too.
Yes. Look at this beutiful piece of code:
#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
typedef string::iterator s_iter;
// for counting chars
typedef map<char,size_t> charSet;
typedef charSet::iterator cs_iter;
// for ordering the results
typedef multimap<size_t,char,greater<size_t> > countSet;
typedef countSet::iterator cnt_iter;
// functor that counts chars
class countChar
{
private:
charSet& cs;
public:
countChar(charSet& chs):cs(chs) {}
void operator()(char c)
{
size_t count=1;
cs_iter csi=cs.find(c);
if (csi!=cs.end())
{
count=csi->second+1;
cs.erase(csi);
}
cs.insert(make_pair(c,count));
}
};
int main()
{
string str;
cout<<"Input your string: ";
cin>>str;
if (str.empty())
{
cout<<"This string is empty!!";
return -1;
}
charSet cs;
for_each (str.begin(),str.end(),countChar(cs));
countSet cnts;
for (cs_iter csi=cs.begin();csi!=cs.end();++csi)
{
cnts.insert(make_pair(csi->second,csi->first));
}
cout<<"The character(s) most used is/are:";
cnt_iter ci=cnts.begin();
size_t repetitions=ci->first;
for (;(ci!=cnts.end())&&(ci->first==repetitions);++ci)
{
cout<<'' ''<<ci->second;
}
}
我没有太多考虑过它,我觉得很容易混淆
它更多。
BTW,out是:s
Not much thought dedicated to it, I supose it shold be easy to obfuscate
it a lot more.
BTW, out is: s
你的问题有点含糊不清。
如果你要求字符串中最常用的字符,
答案应该是s。
这个字符串的输出应该是什么:
" abc"
和这一个
" cba"
和这一个
" ..."
和这一个
""
和这一个
Aabbc
?
Your question is a little ambiguous.
If you are asking for the most frequently used character in a string,
the answer should be ''s''.
What should be the output for this string:
"abc"
and this one
"cba"
and this one
"..."
and this one
""
and this one
"Aabbc"
?
这篇关于怎么找到一个最常用的char?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!