teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

編集終了

2018/05/12 15:52

投稿

asm
asm

スコア15149

answer CHANGED
@@ -1,1 +1,71 @@
1
+ 自殺するオブジェクトが自分が住んでるビルをハードコーディングして部屋ごと爆破するのは
2
+ なんというか迷惑な気がする。
3
+
4
+ 案1. 死体掃除業者を呼ぶ
5
+
6
+ ```c++
7
+ #include<iostream>
8
+ #include<list>
9
+
10
+ class cEnemy {
11
+ bool alive = true;
12
+ public:
13
+ bool isAlive() const;
14
+ void F();
15
+ };
16
+
17
+ std::list<cEnemy> TestList;
18
+
1
- 途中送信してしまいました
19
+ int main() {
20
+ TestList.push_back(cEnemy());
21
+ TestList.push_back(cEnemy());
22
+ TestList.push_back(cEnemy());
23
+
24
+ for (auto IT = TestList.begin(); IT != TestList.end(); IT++) {
25
+ IT->F(); // 殺す
26
+ }
27
+ std::cout << TestList.size() << std::endl;
28
+ // お片付け
29
+ TestList.remove_if([](const cEnemy& e){return !e.isAlive();});
30
+ std::cout << TestList.size() << std::endl;
31
+ return 0;
32
+ }
33
+
34
+ void cEnemy::F() {
35
+ //画面外に出たら、殺す
36
+ if (true) {
37
+ alive = false; // 死にました
38
+ }
39
+ }
40
+ bool cEnemy::isAlive() const{return alive;}
41
+ ```
42
+
43
+ 案2. ビル(list)に管理人を雇ってそいつに命令する。
44
+ ```c++
45
+ class cEnemy {
46
+ bool alive = true;
47
+ public:
48
+ bool isAlive() const;
49
+ bool F();
50
+ };
51
+
52
+ class cEnemyManager {
53
+ public:
54
+ std::list<cEnemy> TestList;
55
+ void remove_if_F();
56
+ }
57
+
58
+ void cEnemyManager::remove_if_F(){
59
+ TestList.remove_if(
60
+ [](cEnemy& e){return e.F();}
61
+ );
62
+ }
63
+ bool cEnemy::F() {
64
+ //画面外に出たら、殺す
65
+ if (true) {
66
+ alive = false; // 死にました
67
+ return true;
68
+ }
69
+ return false;
70
+ }
71
+ ```

1

途中

2018/05/12 15:52

投稿

asm
asm

スコア15149

answer CHANGED
@@ -1,31 +1,1 @@
1
- ```c++
2
- #include<iostream>
3
- #include<list>
4
-
5
- class cEnemy {
6
- bool isAlive = true;
7
- public:
8
- void F();
9
- };
10
-
11
- std::list<cEnemy> TestList;
12
-
13
- int main() {
1
+ 途中送信してしまいました
14
- TestList.push_back(cEnemy());
15
- TestList.push_back(cEnemy());
16
- TestList.push_back(cEnemy());
17
-
18
- for (auto IT = TestList.begin(); IT != TestList.end(); IT++) {
19
- IT->F();
20
- }
21
- TestList.remove_if([](const auto& e){return !e.isAlive;});
22
- return 0;
23
- }
24
-
25
- void cEnemy::F() {
26
- //画面外に出たら、殺す
27
- if (true) {
28
- isAlive = false;
29
- }
30
- }
31
- ```