如何通过先前存储的ID从结构对象中检索值?C
How do I retrieve a value from struct object by previously stored ID? C++
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;
}
}
相关文章:
- 如何检索由带通配符的字符串索引的对象
- 用于从可能不存在的容器中检索对象的 API 设计
- 在 c++ 中使用指向对象的指针检索数据
- 通过 std::bind 从对象成员检索值
- 将 JSON 对象作为参数在 JNI 中传递给 CPP,并在 CPP 中检索 JSON 对象的数据
- C++ 从树中检索对象
- 检索不同类型的对象指针
- 如何在Boost Serialization中检索多个对象值
- 使用Boost序列化保存和检索多个对象
- C++ 无法检索自定义矢量中的对象
- 如何通过先前存储的ID从结构对象中检索值?C
- 在另一个中检索JSON对象
- c++使用字符串句柄对对象进行通用存储和检索
- 从C 添加和检索类的对象
- 如何从wow64进程中检索特定内核对象的64位地址
- 检索在函数内部使用运算符new分配的类指针对象成员的值时出现的问题
- 从数据库中以 blob 数据类型形式存储的原始图像数据中检索 CImage 对象
- QML:如何检索默认字体对象
- 从继承的对象检索信息
- 从 win32api C++中的 LDAPMessage 对象检索'pre windows 2000 logon'名称