M
mail.zhf
if have two char use equality, out first.
example:
char str[] = "gbdfssdffss";
out is: f.
example:
char str[] = "gbdfssdffss";
out is: f.
mail.zhf said:if have two char use equality, out first.
example:
char str[] = "gbdfssdffss";
out is: f.
osmium said::
if have two char use equality, out first.
example:
char str[] = "gbdfssdffss";
out is: f.
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.
#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;
}
}
mail.zhf said:if have two char use equality, out first.
example:
char str[] = "gbdfssdffss";
out is: f.
mail.zhf said:if have two char use equality, out first.
akarl said:
Zara said:osmium said::
if have two char use equality, out first.
example:
char str[] = "gbdfssdffss";
out is: f.
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.
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;
}
}
Not much thought dedicated to it, I supose it shold be easy to obfuscate
it a lot more.
BTW, out is: s
Then, why do you ask on comp.lang.c++? You should ask on comp.lang.cmail.zhf said:the method is best but i don't use stl, i want use c
mail.zhf said:if have two char use equality, out first.
example:
char str[] = "gbdfssdffss";
char MostUsed(const char* pC)
{
std::map<char, int> counts;
while(*pC!='\0')
{
counts[*pC] ++;
}
return *counts.front().first; // or back??
}
mail.zhf said:if have two char use equality, out first.
example:
char str[] = "gbdfssdffss";
out is: f.
<snip>char MostUsed(const char* pC)
{
std::map<char, int> counts;
while(*pC!='\0')
{
counts[*pC] ++;
}
return *counts.front().first; // or back??
}
Marc said:Gernot Frisch wrote:
<snip>char MostUsed(const char* pC)
{
std::map<char, int> counts;
while(*pC!='\0')
{
counts[*pC] ++;
}
return *counts.front().first; // or back??
}
Yeah, right
Try
return counts.empty()
? '\0'
: min_element(counts.begin(),counts.end())->first;
instead.
mail.zhf said:if have two char use equality, out first.
example:
char str[] = "gbdfssdffss";
out is: f.
akarl said:#define SIZE 256 /* number of values a char can assume */
#define OFFSET (SIZE / 2)
static int counts[SIZE]; /* counts[c + OFFSET] is the frequency of
char c */
/* most_common(s) returns the first character in the string s with
the highest frequency.
Precondition: s[0] != '\0'
Example: most_common("Alibaba") returns 'b'.
*/
char most_common(char s[])
{
int k;
char result;
assert(s[0] != '\0');
for (k = 0; k < SIZE; k++) { counts[k] = 0; }
k = 0;
while (s[k] != '\0') {
counts[(int) s[k] + OFFSET]++;
k++;
}
result = s[0];
k = 0;
while (s[k] != '\0') {
if (counts[(int) s[k] + OFFSET] > counts[(int) result + OFFSET]) {
result = (char) s[k];
akarl said:I'm feeling generous today, so here's a solution in C:
Maxim said:akarl wrote:
I'm feeling generous today, so here's a solution in C:
There is a number of problems in the solution:
1) The function is not reentrant.
2) It uses 2 loops instead of 1.
3) It operates on char[] rather than char const[] and does not handle
empty strings.
akarl said:Maxim said:akarl wrote:
I'm feeling generous today, so here's a solution in C:
There is a number of problems in the solution:
1) The function is not reentrant.
2) It uses 2 loops instead of 1.
3) It operates on char[] rather than char const[] and does not handle
empty strings.
Thanks for your comments. Code reviewing is indispensable. Whether the
function should be thread-safe or not depends on the requirements (the
thread safe version is slightly slower).
Can you think of any sensible return value for an empty string?
Maxim said:akarl said:Maxim said:akarl wrote:
I'm feeling generous today, so here's a solution in C:
There is a number of problems in the solution:
1) The function is not reentrant.
2) It uses 2 loops instead of 1.
3) It operates on char[] rather than char const[] and does not handle
empty strings.
Thanks for your comments. Code reviewing is indispensable. Whether the
function should be thread-safe or not depends on the requirements (the
thread safe version is slightly slower).
Mine is not.
An empty c-string contains a single character '\0', which is the most
used character in this case.
akarl said:Maxim Yegorushkin wrote:
[]
The allocation of the array on the stack requires some extra instructions.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.