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

时间:2013-03-27 05:08:46

标签: c++ struct stdmap

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

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

ID2POINT3DID m_i2pt3idVertices;

有人可以告诉我如何使用x,y and z

访问变量m_i2pt3idVertices

3 个答案:

答案 0 :(得分:2)

m_i2pt3idVertices是用于存储POINT3DID个对象的容器。单独,它没有成员变量xyz。你可以在其中加入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

答案 1 :(得分:1)

您需要使用迭代器。这是一个示例:

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

答案 2 :(得分:1)

ID2POINT3DID是地图容器。您可以通过某个unsigned int键访问单个元素:

m_i2pt3idVertices[42].x

或者你可以迭代容器中的元素:

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