回答編集履歴

3

コード追記

2020/04/22 17:37

投稿

SHOMI
SHOMI

スコア4079

test CHANGED
@@ -45,3 +45,45 @@
45
45
  }
46
46
 
47
47
  ```
48
+
49
+
50
+
51
+ ---
52
+
53
+
54
+
55
+ 並べ替えせずに逆順出力。
56
+
57
+ ```C++
58
+
59
+ #include <iostream>
60
+
61
+ #include <vector>
62
+
63
+ #include <algorithm>
64
+
65
+
66
+
67
+ int main() {
68
+
69
+ int n;
70
+
71
+ std::cin >> n;
72
+
73
+ std::vector<int> a(n);
74
+
75
+
76
+
77
+ for (int& i: a)
78
+
79
+ {
80
+
81
+ std::cin >> i;
82
+
83
+ };
84
+
85
+ std::for_each(a.rbegin(), a.rend(), [](int i) { std::cout << i << std::endl; });
86
+
87
+ }
88
+
89
+ ```

2

push_back()追記

2020/04/22 17:37

投稿

SHOMI
SHOMI

スコア4079

test CHANGED
@@ -27,3 +27,21 @@
27
27
  ```
28
28
 
29
29
  として確保してください。
30
+
31
+
32
+
33
+ 一旦別の変数に受けて`push_back()`でも可。
34
+
35
+ ```C++
36
+
37
+ for(int i=0;i<n;i++){
38
+
39
+ int j;
40
+
41
+ cin>>j;
42
+
43
+ a.push_back(j);
44
+
45
+ }
46
+
47
+ ```

1

コード追記

2020/04/22 08:34

投稿

SHOMI
SHOMI

スコア4079

test CHANGED
@@ -1,3 +1,29 @@
1
1
  `cin >> a[i];`
2
2
 
3
3
  `a`は要素数`0`のため範囲外アクセスとなります。
4
+
5
+ ```C++
6
+
7
+ int n;
8
+
9
+ cin >> n;
10
+
11
+ vector<int> a(n);
12
+
13
+ ```
14
+
15
+ もしくは
16
+
17
+ ```C++
18
+
19
+ vector<int>a;
20
+
21
+ int n;
22
+
23
+ cin >> n;
24
+
25
+ a.resize(n);
26
+
27
+ ```
28
+
29
+ として確保してください。