2. (10 scores)#
点击查看考点
类型转换
According to the following output results, fill in the blanks in the following programs.
翻译
根据以下输出结果, 填写下面程序中的空白.
1Output:
21 3
32 2
43 1
5A 1
6B 2
7C 3
8a 3
9b 2
10c 1
1#include <cstdlib>
2#include <iostream>
3
4using namespace std;
5
6void hist(/* (1) */) {
7 int n;
8 int hist[256] = /* (2) */;
9 while (*src != /* (3) */) {
10 hist[/* (4) */]++;
11 }
12 for (int i = 0; i < 256; i++) {
13 if (hist[i] != 0) {
14 cout << /* (5) */ << endl;
15 }
16 }
17}
18
19auto main() -> int {
20 char const* src = "aaabbc111223ABBCCC";
21 hist(src);
22 return 0;
23}
点击查看解答参考
char const* src
, 传递字符串.{}
, 将数组全部初始化为0
.'\0'
, C风格字符串的终止条件是达到了终止字符'\0'
.*src++
, 两个功能: 返回当前指针指向的字符, 让对应的计数加一;++
从而向后继续遍历.static_cast<char>(i) << ' ' << hist[i]
, 首先要输出对应的字符,因而要类型转换,然后输出字符的计数,中间有空格.