c++ - 无法使用结构作为键在 C++ 映射中找到插入的值

标签 c++ struct c++14

我正在尝试使用 map 以坐标 x,y 为键来存储 map 信息。我无法使用自动迭代器或 map.find() 通过此 map 正确迭代。它只是不会返回正确的 map 值。我正在使用 C++14,这是我的代码块:

#include <iostream>
#include <string>
#include <map>
#include <iterator>
#include <vector>

using namespace std;

struct mapPoint {
  int X;
  int Y;

  bool operator < (const mapPoint &coord) const {
    if (X == coord.X && Y == coord.Y) {
      return true;
    } else {
      return false;
    }
  }
};

struct explorerNotes {
  bool foundCoin;
  int timesVisitedThisBlock;
  vector<bool> unexploredEntrances; // 0 0 0 0 -> N S E W
  explorerNotes():timesVisitedThisBlock(0),unexploredEntrances(4,false){}
};

int main() {
  map<mapPoint, explorerNotes> explorerNotebook;

  explorerNotes testNote1, testNote2, testNote3;
  mapPoint testCoord1, testCoord2, testCoord3;

  testNote1.foundCoin = true;
  testNote1.timesVisitedThisBlock = 42;
  testNote1.unexploredEntrances = {true, true, false, false};
  testCoord1.X = 25;
  testCoord1.Y = 3;

  testNote2.foundCoin = false;
  testNote2.timesVisitedThisBlock = 314;
  testNote2.unexploredEntrances = {false, true, false, false};
  testCoord2.X = 11;
  testCoord2.Y = 2;

  testNote3.foundCoin = true;
  testNote3.timesVisitedThisBlock = 420;
  testNote3.unexploredEntrances = {false, true, false, false};
  testCoord3.X = 1;
  testCoord3.Y = 1;

  explorerNotebook.insert(pair<mapPoint, explorerNotes>(testCoord1, testNote1));
  explorerNotebook.insert(pair<mapPoint, explorerNotes>(testCoord2, testNote2));
  explorerNotebook.insert(pair<mapPoint, explorerNotes>(testCoord3, testNote3));

  map<mapPoint, explorerNotes>::iterator p;
  p = explorerNotebook.find(testCoord1);
  cout << " testing 1:"
       << "\nfoundCoin: " << p->second.foundCoin
       << "\ntimesVisitedThisBlock: " << p->second.timesVisitedThisBlock
       << "\nunexploredEntrances: "<< "(" << p->second.unexploredEntrances[0] << "," << p->second.unexploredEntrances[1] << "," << p->second.unexploredEntrances[2] << "," <<p->second.unexploredEntrances[3] << ")" << endl;

  map<mapPoint, explorerNotes>::iterator q;
  q = explorerNotebook.find(testCoord2);
  cout << " testing 2:"
       << "\nfoundCoin: " << q->second.foundCoin
       << "\ntimesVisitedThisBlock: " << q->second.timesVisitedThisBlock
       << "\nunexploredEntrances: "<< "(" << q->second.unexploredEntrances[0] << "," << q->second.unexploredEntrances[1] << "," << q->second.unexploredEntrances[2] << "," <<q->second.unexploredEntrances[3] << ")" << endl;

  map<mapPoint, explorerNotes>::iterator r;
  r = explorerNotebook.find(testCoord3);
  cout << " testing 3:"
       << "\nfoundCoin: " << r->second.foundCoin
       << "\ntimesVisitedThisBlock: " << r->second.timesVisitedThisBlock
       << "\nunexploredEntrances: "<< "(" << r->second.unexploredEntrances[0] << "," << r->second.unexploredEntrances[1] << "," << r->second.unexploredEntrances[2] << "," <<r->second.unexploredEntrances[3] << ")" << endl;;

  return 0;
}

当我编译这段代码时,它给了我输出:

 testing 1:
foundCoin: 1
timesVisitedThisBlock: 42
unexploredEntrances: (1,1,0,0)
 testing 2:
foundCoin: 1
timesVisitedThisBlock: 42
unexploredEntrances: (1,1,0,0)
 testing 3:
foundCoin: 1
timesVisitedThisBlock: 42
unexploredEntrances: (1,1,0,0)

它只是重复第一个附加值...我整天都被困住了。有任何想法吗?提前致谢。

最佳答案

你的 operator <完全搞砸了。

你是说如果 X 坐标和 Y 坐标相等,则 A 小于 B。所以 (1,1) < (2,2) 是假的,但是 (1,1) < (1,1) 是真的。难怪 map 无法找到正确的条目。

特别是:

  • 因为 (1,1) < (2,2) 是假的,并且 (1,1) > (2,2) 是假的(> 是 < 参数相反),这意味着 (1,1) 和(2,2) 必须相等!
  • 因为 (1,1) < (1,1) 为真,(1,1) > (1,1) 不可能为真,因为一件事不能小于或大于另一件事。然而它是。

您需要找出点的实际顺序,并在 operator < 中执行该顺序.例如,如果 a

bool operator < (const mapPoint &coord) const {
    if (X < coord.X || (X == coord.X && Y < coord.Y)) {
        return true;
    } else {
        return false;
    }
}

关于c++ - 无法使用结构作为键在 C++ 映射中找到插入的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39693900/

相关文章:

c - fread() : Reading from a file (without alignment) results in skipping of bytes

c - C中结构数组的静态初始化

c++ - 将 shared_ptr 移动到在 shared_ptr 指向的对象上调用的方法

c++ - 有没有办法使全局函数/静态成员函数可调用一次?

c++ - 尝试访问指向节点的指针时出现段错误

c++ - 如何用单个信号处理程序解决这个多个Linux计时器

c++ - HealthCare Provider.exe 中发生类型为 'System, Access Violation Exception' 的未处理异常

c++ - 自定义外观 QTabWidget

c++ - 为什么从 write() 调用 __kernel_vsyscall() 永远不会返回?

Swift - 字符串类型有固定大小吗?