回答編集履歴

1

追記

2021/04/13 15:08

投稿

episteme
episteme

スコア16612

test CHANGED
@@ -65,3 +65,69 @@
65
65
  }
66
66
 
67
67
  ```
68
+
69
+ [別解]
70
+
71
+ ```C++
72
+
73
+ #include <iostream>
74
+
75
+ #include <vector>
76
+
77
+ #include <memory>
78
+
79
+
80
+
81
+ class foo {
82
+
83
+ int n_;
84
+
85
+ public:
86
+
87
+ foo(int n) : n_(n) { std::cout << " foo(" << n_ << ")\n"; }
88
+
89
+ ~foo() { std::cout << "~foo(" << n_ << ")\n"; }
90
+
91
+ int get() const { return n_; }
92
+
93
+ };
94
+
95
+
96
+
97
+ int main(){
98
+
99
+ std::vector<std::unique_ptr<foo>> vec;
100
+
101
+ for ( int i = 0; i < 10; ++i ) {
102
+
103
+ vec.push_back(std::make_unique<foo>(i));
104
+
105
+ }
106
+
107
+ std::cout << "奇数を削除\n";
108
+
109
+ for ( auto iter = vec.begin(); iter != vec.end(); ) {
110
+
111
+ if ( (*iter)->get() % 2 ) {
112
+
113
+ iter = vec.erase(iter);
114
+
115
+ } else {
116
+
117
+ ++iter;
118
+
119
+ }
120
+
121
+ }
122
+
123
+ std::cout << "残ったのを削除\n";
124
+
125
+ while ( !vec.empty() ) {
126
+
127
+ vec.pop_back();
128
+
129
+ }
130
+
131
+ }
132
+
133
+ ```