嵌套向量<float>和参考操作 [英] Nested vector<float> and reference manipulation
问题描述
最终我明白了!
//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);
}
}
}
下面的原始问题:
我正在尝试修改 C++ pacman 项目,其中鬼被定义为浮点数组:
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) { ... }
这被称为:
void display() {
...
if (!over) {
updateMonster(monster1);
updateMonster(monster2);
updateMonster(monster3);
updateMonster(monster4);
}
...
}
我的目标是将原始怪物添加到 vector
以便我可以在 for 循环中遍历它们并更新它们:
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);
}
}
...
}
然而,它在 for 循环中没有成功.我的引用/指针哪里出错了?谢谢!
However, it hasn't worked successfully in a for loop. Where are my references/pointers going wrong? Thanks!
我应该提到我想让我的怪物收藏在大小上增加和减少,因此需要一个向量.我的问题是,当我这样声明它们时:
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);
}
(我的进度)>
推荐答案
您可以使用 std::array
.无需使用原始指针:
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);
}
这篇关于嵌套向量<float>和参考操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!