首页 > 解决方案 > 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);
            }

(my progress)

标签: c++vectorreference

解决方案


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);
}

Godbolt


推荐阅读