如何将结构插入stl::map C++中
How to insert struct into stl::map C++
我正在尝试使用一个循环来使用insert方法填充映射。我有一个地图,我正试图使用这种方法填充:
void board:: insertToMap(Letter c, int num){
this->myRackMap.insert(pair<Letter, int>(c, num));
}
我在另一个方法中调用这个循环中的辅助方法:
void board:: getRackAsMap(){
for (int i = 0; i < this->getMyRack().size(); ++i){
insertToMap(this->getMyRack().at(i), this->getMyRack().at(i).readScore());
}
}
//myRackMap is a map<Letter, int>
//myRack is a vector<Letter>
vector<Letter>& board::getMyRack(){
return this->myRack;
}
//readScore returns an Int value based on the char value of the current Letter
当我尝试运行这个时,我会收到一条长得离谱的错误消息,太长了,无法放入其中。错误消息的结束行是这样的;但是:
"'const Letter'不是从'const std::multimap<_Key,_Tp,_比较,_Alloc>'{return __x<__y;}"
这让我相信这个错误与将结构体Letter插入映射而不是原始数据类型有关。任何建议都将不胜感激,因为我对c++不太熟悉,谢谢你能提供的任何帮助!
编辑:这是Letter.h的
struct Letter{
private:
char theLetter;
int xPos;
int yPos;
public:
Letter();
Letter(char c);
Letter(int x, int y, char c);
void setPos(int x, int y);
void setLetter(char c);
int getXPos();
int getYPos();
char getTheLetter();
int readScore();
};
和letter.cpp
Letter:: Letter(){
this->xPos = -1;
this->yPos = -1;
this->theLetter = '?';
}
Letter:: Letter(char c){
this->xPos = -1;
this->yPos = -1;
this->theLetter = c;
}
Letter:: Letter(int x, int y, char c){
this->xPos = x;
this->yPos = y;
this->theLetter = c;
}
int Letter:: getXPos(){
return this->xPos;
}
int Letter:: getYPos(){
return this->yPos;
}
char Letter:: getTheLetter(){
return this->theLetter;
}
void Letter:: setPos(int x, int y){
this->xPos = x;
this->yPos = y;
}
void Letter:: setLetter(char c){
this->theLetter = c;
}
int Letter:: readScore(){
switch (this->getTheLetter()){
case 'A':
return 1;
break;
case 'B':
return 3;
break;
//etc etc, returns int based on char of Letter
}
}
std::map
的元素是根据key
排序的。因此,您需要为您的密钥对象定义一个operator<
,在您的示例中就是Letter
。或者在构建std::map
时提供比较器。
bool cmp(const Letter& lhs, const Letter &rhs) { ...define order... }
std::map<Letter, int, bool(*)(const Letter&, const Letter&)> myRackMap(cmp);
无论你决定使用什么解决方案,你都需要定义一种排序方式,我想xpos
和ypos
的组合为Letter
s定义了一个唯一的标识符。因此,你可以写这样的东西:
bool cmp(const Letter& lhs, const Letter &rhs) {
return std::tie(lhs.xPos, lhs.yPos) < std::tie(rhs.xPos, rhs.yPos);
}
或者,如果您选择过载<
操作员:
bool Letter::operator<(const Letter &other) const {
return std::tie(xPos, yPos) < std::tie(other.xPos, other.yPos);
}
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 使用一个考虑到std::map中键值的滚动或换行的键
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 允许从 std::map 的密钥窃取资源?
- 有没有办法对std::unordered_set、std::unrdered_map、std::set、std::map
- 将重物插入std::map
- 使用通用值初始化 std::map,不重复
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- C++:当所有条目都保证是唯一时,替代 std::map
- 使用模板化的键类型定义 std::map,该键类型基于作为参数接收的函数
- 如果 KEY 是 std::list 或 std::vector 而不是值,那么 std::map 的默认行为是什么?
- 使用字符数组作为 Map 中的键
- C++如何创建 std::map
- C++ equivalent to Java Map getOrDefault?
- 从其他容器中移动构造"std::map"
- 如何使用 uint64_t 键类型从 std::map<int, std::string> 返回值?
- 将 std::map::emplace 与返回 shared_ptr 的函数一起使用是否正确?
- C++中 std::map 的运行时复杂度是多少?
- 为什么在 std::map 上移动无法将元素从一个映射移动到另一个映射