回答編集履歴

1

質問のタグがC\+\+だったのでCからC\+\+にコードを変更

2017/07/21 17:24

投稿

退会済みユーザー
test CHANGED
@@ -2,9 +2,13 @@
2
2
 
3
3
 
4
4
 
5
- ```c
5
+ ```c++
6
6
 
7
+ #include <iostream>
8
+
7
- #include <stdio.h>
9
+ #include <string>
10
+
11
+ #include <vector>
8
12
 
9
13
 
10
14
 
@@ -14,23 +18,23 @@
14
18
 
15
19
  // Hello, world!
16
20
 
17
- const char ascii[] = {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20,
21
+ std::vector<char> ascii = {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20,
18
22
 
19
- 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00};
23
+ 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00};
20
24
 
21
25
 
22
26
 
23
- for (int i = 0; i < sizeof(ascii); i++)
27
+ for (const auto& chr : ascii) {
24
28
 
29
+ std::cout << chr;
30
+
31
+ }
32
+
25
- printf("%c", ascii[i]);
33
+ std::cout << std::endl;
26
34
 
27
35
 
28
36
 
29
- printf("\n");
30
-
31
-
32
-
33
- printf("%s\n", ascii);
37
+ std::cout << ascii.data() << std::endl;
34
38
 
35
39
 
36
40
 
@@ -38,4 +42,6 @@
38
42
 
39
43
  }
40
44
 
45
+
46
+
41
47
  ```