c++ - Nested vector and reference manipulation
问题描述
final edit: I got it!
//Initialize each collection using pointers
array<float, 3> monster1 = { 10.5, 8.5, 1.0 }; //coordinates and direction of first monster
array<float, 3> monster2 = { 13.5, 1.5, 2.0 }; //coordinates and direction of second monster
array<float, 3> monster3 = { 4.5, 6.5, 3.0 }; //coordinates and direction of third monster
array<float, 3> monster4 = { 2.5, 13.5, 4.0 }; //coordinates and direction of fourth monster
vector<array<float,3>*> pinkys = { &monster1 };
vector<array<float, 3>*> blinkys = { &monster2 };
vector<array<float, 3>*> inkys = { &monster3 };
vector<array<float, 3>*> clydes = { &monster4 };
vector<vector<array<float,3>*>*> all_ghosts = { &pinkys, &blinkys, &inkys, &clydes };
...
//Function definition
void updateMonster(array<float, 3>& monster);
...
//appropriate for loop and function call
void display() {
if (!over) {
for (auto list : all_ghosts) {
for (auto ghost : *list) {
updateMonster(*ghost);
}
}
}
ORIGINAL QUESTION BELOW:
I'm trying to modify a C++ pacman project where the ghosts are defined as float arrays:
float* monster1 = new float[3]{ 10.5, 8.5, 1.0 }; //coordinates and direction of first monster
...
float* monster4 = new float[3]{ 2.5, 13.5, 4.0 }; //coordinates and direction of fourth monster
Currently they are being updated successfully one-by-one by a function :
void updateMonster(float* monster) { ... }
That's called as:
void display() {
...
if (!over) {
updateMonster(monster1);
updateMonster(monster2);
updateMonster(monster3);
updateMonster(monster4);
}
...
}
My goal is to instead add the original monsters to a vector<float*>
so that I can iterate through them in a for loop and update them:
static vector<float*> v = { monster1, monster2, monster3, monster4 };
...
void display() {
...
if (!over) {
for (auto* m : v) {
updateMonster(m);
}
}
...
}
However, it hasn't worked successfully in a for loop. Where are my references/pointers going wrong? Thanks!
Edit: I should've mentioned that I wanted to have my collection of monsters grow and decrease in size, thus needing a vector. My problem though is that when I declare them as such:
float* monster1 = new float[3]{ 10.5, 8.5, 1.0 }; //coordinates and direction of first monster
...
static vector<float*> v = { monster1, monster2, monster3, monster4 };
It doesn't work as I expect when I iterate through them as:
for (auto& m : v) {
updateMonster(m);
}
解决方案
You can use std::array
. No need to use raw pointers:
#include <array>
#include <vector>
using Monster = std::array<float, 3>;
void updateMonster(Monster& monster);
int main() {
std::vector<Monster> monsters;
monsters.push_back(Monster{1.f, 2.f, 3.f});
monsters.push_back(Monster{4.f, 5.f, 6.f});
monsters.push_back(Monster{7.f, 8.f, 9.f});
for (auto& monster : monsters) updateMonster(monster);
}
推荐阅读
- javascript - 使用函数名称中的空格运行 Javascript 函数?
- javascript - 使用 jQuery/JavaScript 在 HTML 页面中显示/打印一组嵌套的 JSON API 数据(列表)
- scala - 将空值替换为上一行的值。[火花斯卡拉]
- mongodb - Loopback 3 按嵌入式模型的属性查询
- javascript - 检查数组 B 中的元素是否大于数组 A 中的元素,数组的长度不相等
- typescript - 为什么我不能在 typescript 中向 winston 类型添加属性?
- mongodb - 如何在 WSL ubuntu 18.04 上删除 mongodb
- javascript - ExpressJS - 在服务器端生成 CSV 文件并启用从客户端下载
- android - Android 生成带密码的密钥
- c# - 写入 Excel 的 C# 控制台应用程序返回空单元格