【问题标题】:How to insert struct into stl::map C++如何将结构插入 stl::map C++
【发布时间】:2015-12-06 07:01:32
【问题描述】:

我正在尝试使用循环来使用插入方法填充地图。我有一张我正在尝试使用此方法填充的地图:

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 _>' { 返回 __x

这使我相信该错误与将我的 struct 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
    }
}

【问题讨论】:

  • 您需要为您的密钥定义一个operator&lt;。在你的情况下,这将是Letter
  • @simpel01 在
  • 我们需要知道Letter的样子。
  • @simpel01 我更新了帖子以包含这些信息,谢谢!

标签: c++ dictionary insert


【解决方案1】:

std::map 的元素根据key 排序。因此,您需要为您的关键对象定义一个operator&lt;,在您的示例中为Letter。或者在构建std::map 时提供一个比较器。

bool cmp(const Letter& lhs, const Letter &rhs) { ...define order... }
std::map<Letter, int, bool(*)(const Letter&, const Letter&)> myRackMap(cmp);

无论您决定使用哪种解决方案,您都需要定义一种订购信件的方式,我想xposypos 的组合定义了Letters 的唯一标识符。因此你可以这样写:

bool cmp(const Letter& lhs, const Letter &rhs) {
   return std::tie(lhs.xPos, lhs.yPos) < std::tie(rhs.xPos, rhs.yPos);
}

或者,如果您选择重载 &lt; 运算符:

bool Letter::operator<(const Letter &other) const {
   return std::tie(xPos, yPos) < std::tie(other.xPos, other.yPos);
}

【讨论】:

  • 非常感谢您的帮助
  • 有没有办法以类似的方式填充地图,但不使用 cmp 或
  • 如果您不关心消除重复和排序,您可以使用向量 std::vector&lt;std::pair&lt;Letter, int&gt;&gt; 并使用 push_back 而不是插入。在这种情况下,您不再需要 &lt; 运算符了。
  • @Biggytiny:您需要了解 std::map 和 std::set 在内部实现为平衡二叉树(红黑树)并在内部进行排序。这就是使这些数据结构中的查找对数快速的原因。因此,如果您使用基于树的标准容器,则必须定义排序顺序,例如地图键的比较操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多