지도를 사용하여 x, y 좌표를 사용하여지도 정보를 저장하려고합니다. 필자는 auto iterator 또는 map.find()를 사용하여이 맵을 올바르게 반복 할 수 없습니다. 그것은 나에게 정확한지도 값을 돌려주지 않을 것이다. 이 코드를 컴파일 할 때구조체를 키로 사용하여 C++ 맵에 삽입 된 값을 찾습니다.
#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;
}
이 나에게 출력을 주었다 : 여기의 코드 내 체크입니다, C++ 14을 사용하고 그냥 처음 부가가치를 반복
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)
.. 하루 종일 붙어있어. 어떤 아이디어? 미리 감사드립니다.
'연산자 <'가 잘못되었습니다. [strict weak ordering]을 구현해야합니다 (https://en.wikipedia.org/wiki/Weak_ordering#Strict_weak_orderings) –
'std :: pair' 또는'std :: tuple'을 사용하여이 문제를 피할 수 있습니다. 연산자 <'. –