简体   繁体   English

如何访问结构中定义的变量

[英]How to access a variable defined in a struct

struct POINT3DID 
{
    unsigned int newID;
    float x, y, z;
};

typedef std::map<unsigned int, POINT3DID> ID2POINT3DID;

ID2POINT3DID m_i2pt3idVertices;

Can someone please tell me how can I access the variables x,y and z using m_i2pt3idVertices 有人可以告诉我如何使用m_i2pt3idVertices访问变量x,y and z

m_i2pt3idVertices is a container for storing POINT3DID objects. m_i2pt3idVertices是用于存储POINT3DID对象的容器。 Alone, it doesn't have member variables x , y , or z . 单独没有成员变量xyz You can put a POINT3DID inside of it though: 您可以将POINT3DID放入其中:

m_i2pt3idVertices[0] = POINT3DID(); // Put a POINT3DID into key 0

m_i2pt3idVertices[0].x = 1.0f; // Assign x for key 0
m_i2pt3idVertices[0].y = 2.0f; // Assign y for key 0
m_i2pt3idVertices[0].z = 3.0f; // Assign z for key 0

You need to use iterator. 您需要使用迭代器。 Here is a sample: 这是一个示例:

std::map<unsigned int, POINT3DID>::iterator it;
it = m_i2pt2idVertices.find(5);
it->second.x = 0;
it->second.y = 1;
it->second.z = 2;

ID2POINT3DID is map container. ID2POINT3DID是地图容器。 You can access single element by some unsigned int key: 您可以通过一些unsigned int键访问单个元素:

m_i2pt3idVertices[42].x

Or you can iterate over elements in container: 或者,您可以遍历容器中的元素:

for(ID2POINT3DID::iterator it=m_i2pt3idVertices.begin();it!=m_i2pt3idVertices.end();++it) {
        cout << it->second.x << " " << it->second.y << " " << it->second.z << endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM