如何通过先前存储的ID从结构对象中检索值?C

How do I retrieve a value from struct object by previously stored ID? C++

本文关键字:对象 检索 结构 何通过 存储 ID      更新时间:2023-10-16

i具有一个c 脚本,带有结构对象'粒子',x,y,z位置存储。我创建了具有唯一ID的几个粒子对象。如何通过ID获得粒子的位置?诸如:eNterial.getPosbyId(int id);

我的代码如下:

struct Particle {
Vector position     { 0.0f, 0.0f, 0.0f };
Vector velocity     { 0.0f, 0.0f, 0.0f };
Vector acceleration { 0.0f, 0.0f, 0.0f};
Vector color        { 1.0f, 0.0f, 0.0f }; // RED
int ID = 0;}

我在结构中尝试了:

    float getPosX(int ID)
{
    return this->position.x;
}

with: float ix = particles->getPosX(5);

但没有运气。有任何想法吗?我需要一堂和getters的课吗?如果是这样,我该怎么做?到目前为止,结构一直很好且易于使用...

谢谢!

编辑

向量是一个结构:

struct Vector
{
float x, y, z;}

粒子是使用数组存储的:

const int MaxParticles = 5;
Particle particles[MaxParticles];

使用阵列

存储粒子

如果粒子ID对应于数组索引,则通过ID访问该数组:

float ix = particles[id].position.x;

如果没有,最好将它们存储在某些关联结构中,例如map

std::map<int, Particle> particles;

然后,您可以从结构中删除ID,并使用与上述相同的代码。

由于您将粒子存储在数组中,因此您必须一个一个一个一个仔细检查

float getPosX(int ID)
{
    for (auto const & p : particles) { // assume particles are a global variable
        if (p.ID == ID) 
            return p.position.x;
    }
    return 0; // or some value to indicate error or not found
}

如果您使用数组来存储粒子,并且数组索引与粒子ID不符,则只需搜索数组即可。

#include <iostream>
#include <algorithm>
using namespace std;
struct Vector { 
    float x, y, z;
};
struct Particle {
    int ID;
    Vector position     { 0.0f, 0.0f, 0.0f };
    Vector velocity     { 0.0f, 0.0f, 0.0f };
    Vector acceleration { 0.0f, 0.0f, 0.0f};
    Vector color        { 1.0f, 0.0f, 0.0f }; // RED
};
int main() {
    const int MaxParticles = 5;
    Particle particles[MaxParticles] = {{0},{3},{2},{4},{5}};
    // find particle with loop
    int findid = 3;
    for(size_t i=0;i< MaxParticles;++i)
    {
        if (particles[i].ID == findid)
        {
            std::cout << "found partcile with id " << findid <<  " at index " << i << std::endl;
            break;
        }
    }
    // find particle with std::find_if
    auto iter = std::find_if(particles,particles+MaxParticles, [&](const Particle& p){
        if (p.ID == findid)
            return true;
        return false;
    });
    std::cout << "Found particle " << iter->ID << std::endl;
    return 0;
}

演示

您最好只使用其他答案之一中建议的关联容器,例如STD :: MAP。

这是类定义,可以帮助您实现这一目标。在这里,您可以使用std ::映射而不是数组"粒子"来更好地搜索性能。

struct Particle 
{
    /// This is your existing particle struct
}
class Particles
{
    const int MaxParticles = 5;
    Particle particles[MaxParticles];
    Particles()
    {
    }
    void AddParticle(Particle p)
    {
        ///Insert particle 'p' into array 'particles'
    }
    void RemoveParticle(Particle p)
    {
        ///Remove particle 'p' from array 'particles'
    }
    Particle GetParticle(int ID)
    {
        Particle p;
        ///Add loop over 'particles' to search particle with specified ID and then return that particle
        return p;
    }
    Vector GetParticlePosition(int ID)
    {
        Vector v;
        ///Add loop over 'particles' to search particle with specified ID and then return it postion
        return v;
    }
}