我编写了以下代码来计算给定char数组中字符的出现次数
#include <iostream>
#include <string>
using namespace std;
int main(){
char f[]={'a','a','b','c','c','d','d','f','e','e'};
int n=sizeof(f)/sizeof(char);
char max=f[0];
for (int i=1;i<n;i++)
{
if(f[i]>max)
{
max=f[i];
}
}
int m=(int)(max)+1;
int *b=new int [m];
for (int i=0;i<n;i++)
{
b[(int)(f[i])]++;
}
for (int i=0;i<m;i++)
{
if(b[i]!=0)
{
cout<<(char)(i)<<" occurs "<<b[i]<< "times "<<endl;
}
}
return 0;
}
但它显示非常糟糕的结果
occurs -842150451times
☺ occurs -842150451times
☻ occurs -842150451times
♥ occurs -842150451times
♦ occurs -842150451times
♣ occurs -842150451times
♠ occurs -842150451times
occurs -842150451times
occurs -842150451times
occurs -84215045
occurs -842150451times
♂ occurs -842150451times
♀ occurs -842150451times
occurs -842150451times
♫ occurs -842150451times
☼ occurs -842150451times
► occurs -842150451times
◄ occurs -842150451times
↕ occurs -842150451times
‼ occurs -842150451times
¶ occurs -842150451times
§ occurs -842150451times
▬ occurs -842150451times
↨ occurs -842150451times
↑ occurs -842150451times
↓ occurs -842150451times
→ occurs -842150451times
← occurs -842150451times
∟ occurs -842150451times
↔ occurs -842150451times
▲ occurs -842150451times
▼ occurs -842150451times
occurs -842150451times
! occurs -842150451times
" occurs -842150451times
# occurs -842150451times
$ occurs -842150451times
% occurs -842150451times
& occurs -842150451times
' occurs -842150451times
( occurs -842150451times
) occurs -842150451times
* occurs -842150451times
+ occurs -842150451times
, occurs -842150451times
- occurs -842150451times
. occurs -842150451times
/ occurs -842150451times
0 occurs -842150451times
1 occurs -842150451times
2 occurs -842150451times
3 occurs -842150451times
4 occurs -842150451times
5 occurs -842150451times
6 occurs -842150451times
7 occurs -842150451times
8 occurs -842150451times
9 occurs -842150451times
: occurs -842150451times
; occurs -842150451times
< occurs -842150451times
= occurs -842150451times
> occurs -842150451times
? occurs -842150451times
@ occurs -842150451times
A occurs -842150451times
B occurs -842150451times
C occurs -842150451times
D occurs -842150451times
E occurs -842150451times
F occurs -842150451times
G occurs -842150451times
H occurs -842150451times
I occurs -842150451times
J occurs -842150451times
K occurs -842150451times
L occurs -842150451times
M occurs -842150451times
N occurs -842150451times
O occurs -842150451times
P occurs -842150451times
Q occurs -842150451times
R occurs -842150451times
S occurs -842150451times
T occurs -842150451times
U occurs -842150451times
V occurs -842150451times
W occurs -842150451times
X occurs -842150451times
Y occurs -842150451times
Z occurs -842150451times
[ occurs -842150451times
\ occurs -842150451times
] occurs -842150451times
^ occurs -842150451times
_ occurs -842150451times
` occurs -842150451times
a occurs -842150449times
b occurs -842150450times
c occurs -842150449times
d occurs -842150449times
e occurs -842150449times
f occurs -842150450times
请帮我解决问题?
答案 0 :(得分:4)
你必须将“malloc”ed(new
)区域初始化为0,它包含垃圾。
答案 1 :(得分:2)
正如其他人所说的那样,我会给你另一种做法。由于有256个ASCII字符,因此只分配了该大小的字符数组。
char mapCount[256];
std::fill(mapCount, mapCount+256, 0); // std::fill is from <algorithm> header
现在,正如您之前所做的那样,只需遍历输入序列并增加相应的mapCount值。即,
++mapCount[(int)inputSequence[i]];
最后输出mapCount
索引,其值大于0.将索引转换为char将为您提供实际字符。正如@Kerrek SB建议的那样,有一种与价值关键关联的概念。您可以使用关联容器std :: map来执行此任务。
答案 2 :(得分:1)
好吧,你的b数组没有初始化......
我的2美分答案 3 :(得分:1)
分配的缓冲区应初始化为零
int *b=new int [m];
memset(b, 0, sizeof(int) * m);
答案 4 :(得分:1)
不确定我是否想要深入研究这段代码,但是既然你把它标记为C ++,我就推荐C ++这样做:
#include <map>
#include <iostream>
std::map<char, unsigned int> histogram;
// Counting
for (char * it = f; it != f + sizeof(f); ++it)
++histogram[*it];
// Reporting
for (std::map<char, unsigned int>::const_iterator it = histogram.begin(); it != histogram.end(); ++it)
std::cout << "Character '" << it->first << "' appears " << it->second << " times.\n";
没有手动内存分配,不会忘记清理,而且它具有相当自我描述性。 (如果数组f
作为没有大小信息的指针,则必须单独传递大小信息并使用它代替sizeof(f)
。)
答案 5 :(得分:1)
实际的“错误”是您没有初始化已分配给零的内存,而新[]不会为您执行此操作。
除了使用new []之外,你实际上并没有编写任何C ++,它都是C.为什么不使用vector?这也会将值初始化为0。
//在顶部
#include <vector>
然后
std::vector<int> b[m];
最后为标识符而不是单个字母使用有意义的名称。
答案 6 :(得分:1)
这里有很多问题:
你想在这做什么?
char f[]={'a','a','b','c','c','d','d','f','e','e'};
int n=sizeof(f)/sizeof(char);
char max=f[0];
for (int i=1;i<n;i++)
{
if(f[i]>max)
{
max=f[i];
}
}
完成后,此代码将为max
分配数组中最高字符的值,在本例中为e
。
在这里?
int m=(int)(max)+1;
//cout<<m;
int *b=new int [m];
此代码将分配一个大小为m
的新整数数组,该数组将为'e' + 1
。几乎肯定不是你想要的。您希望这是字符串的长度(n
)。
正如其他人已经说过的那样,b
将包含未初始化的值,这也会导致一些不必要的行为。