C++ C++;错误:map.insert(生成_对(struct,vector<;struct>;);
下面的代码是我正在进行的国际象棋游戏的一部分,其中键是棋子的位置,值是棋子可能的移动C++ C++;错误:map.insert(生成_对(struct,vector<;struct>;);,c++,c++11,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 tha
#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;
};
#包括
#包括
#包括
使用名称空间std;
结构坐标{
int x,y;
};
int main(){
map moves;//以结构作为键,以结构向量作为值的映射。
//这是一个错误
moves.insert(make_pair)(坐标{0,0},//结构
向量{坐标{1,1},//向量
坐标{2,2},
坐标{3,3}});
返回0;
};
这些代码将我带到文件“stl_function.h”中的第235行。您需要为您的结构提供一个自定义比较器:
struct Coordinate{
int x, y;
constexpr bool operator<(const Coordinate & rhs) const
{
return x < rhs.x && y < rhs.y;
}
};
结构坐标{
int x,y;
constexpr bool操作符您需要为结构提供自定义比较器:
struct Coordinate{
int x, y;
constexpr bool operator<(const Coordinate & rhs) const
{
return x < rhs.x && y < rhs.y;
}
};
结构坐标{
int x,y;
constexpr bool操作符
在哪里?您还需要一个自定义比较器或operatorYea向量包含您得到的错误。如果您使用[]
和移动[{0,0}]={1,1},{2,2},{3,3},它可能是可读的
哪里是
?您还需要一个自定义比较器或运算符a矢量包含您得到的错误。如果您使用[]
和移动[{0,0}]={{1,1},{2,2},{3,3},它可能是可读的我的初学者是C++的,你能告诉我什么是代码吗?RHS?@ RoBotojimes它代表“右手边”,因为它是用在“代码”>坦克的右边,解决了我的问题。我是C++的初学者,你能告诉我什么是<代码> RS< <代码>吗?@ RoBotojimes它代表“右手边”,Cu。z它用于