C ++错误:map.insert(make_pair(struct,vector <struct>));

时间:2016-03-28 20:44:05

标签: c++ c++11

下面的代码是国际象棋游戏的一部分我在哪里,关键是作品的位置,价值是该作品的可能移动

#include<iostream>
#include<map>
#include<vector>

using namespace std;

struct Coordinate{
    int x, y;
};

int main(){
    map<Coordinate, vector<Coordinate>> moves;//map that have an struct as key and a vector of structs as value.
    //There is the error
    moves.insert(make_pair(Coordinate{0,0},//the struct
                               vector<Coordinate>{Coordinate{1,1},//the vector
                                                  Coordinate{2,2},
                                                  Coordinate{3,3}}));
    return 0;
};

Thos代码将我带到文件&#39; stl_function.h&#39;

中的第235行

1 个答案:

答案 0 :(得分:4)

您需要为结构提供自定义比较器:

struct Coordinate{
    int x, y;

    constexpr bool operator<(const Coordinate & rhs) const
    {
        return x < rhs.x && y < rhs.y;   
    }
};
相关问题