在C ++ Map中使用结构作为键

时间:2019-06-17 17:02:12

标签: c++ struct maps

我试图在地图中使用Vertex结构作为键,但是当我尝试获取该值时,每次都会打印出“ 0”。

这是我的主要功能:

int main () {
    Vertex x;
    x.Position = glm::vec3 (1,0,0);
    Vertex y;
    y.Position = glm::vec3 (2,0,0);
    Vertex z;
    z.Position = glm::vec3 (-1,0,0);
    map <Vertex, int> test;
    test.insert( std::pair<Vertex,int>(x,1) );
    test.insert( std::pair<Vertex,int>(y,2) );
    test.insert( std::pair<Vertex,int>(z,3) );
    std::cout << "x: " << test[x];
    std::cout << "y: " << test[y];
    std::cout << "z: " << test[z];
}

我尝试在我的Vertex结构中执行此操作:

struct Vertex {
    // position
    glm::vec3 Position;
    // normal
    glm::vec3 Normal;
    // texCoords
    glm::vec2 TexCoords;
    // tangent
    glm::vec3 Tangent;
    // bitangent
    glm::vec3 Bitangent;
    bool operator<(const Vertex& t) const
    { 
        return true; 
    }
    bool operator==(const Vertex& t) const {
        return t.Position == this->Position;
    } 
};

我认为我只需要知道它们是否相等即可。顶点的顺序无关紧要。

我在网上和其他帖子上看了一下,但似乎无法理解什么地方出了问题。任何帮助将不胜感激!

0 个答案:

没有答案