C++ map에서
직접 만든 class 를 key 로 쓰려면
map 이 std::less<KeyType> 을 사용하기 때문에
key 로 쓰는 class 에서 operator< 만 overloading 하면 된다고
google 이 그렇다고하네요
그래서 다음과 같이 해보았습니다
class A{
public:
int a;
bool operator<(const A& other) {
return this.a < other.a;
}
};
int main() {
map<A, int> m;
A x;
A y;
x.a=1;
y.a=2;
m[x] = 5;
m[y] = 6;
cout << m[x] << endl;
return 0;
}
google 말 대로라면 잘 작동해야 하지만 다음과 같은 에러가 뜨네요.;;
염치없지만 도와주세요 ㅠㅠ
대략 const A 를 less에 넘겨주는게 문제라는 뜻 같은데;;