std::map 은 기본적으로 1개의 key값을 넣어서, 그에 따른 value를 얻게 해줍니다.
그런데 저는 2개의 key값을 사용할 일이 생겼습니다.
이런땐 class를 map의 인자로 사용하면됩니다.
map에서 < operatoro를 사용하기 때문에
class를 사용하기로 하였다면, < operator overloading을 추가로 선언해주어야 합니다.
아래의 source를 보면 2개의 값을 key로 사용해서 value를 얻고 있습니다.
typedef unsigned long long new_addr_type;
class CKey
{
public:
CKey()
: x_(0), y_(0) {}
CKey(new_addr_type a, int b)
: x_(a), y_(b) {}
bool operator < (const CKey& other) const
{
if (this->x_ < other.x_)
return true;
else if (this->x_ == other.x_ && this->y_ < other.y_)
return true;
else
return false;
}
private:
new_addr_type x_;
int y_;
};
int main()
{
map<CKey, char> mapTest;
mapTest.insert(make_pair(CKey(1, 32), 'A'));
mapTest.insert(make_pair(CKey(1, 64), 'B'));
cout << mapTest[CKey(1, 32)] << endl;
cout << mapTest[CKey(1, 64)] << endl;
map<CKey,char>::iterator iter;
iter = mapTest.find(CKey(1,33));
if(iter != mapTest.end())
cout<<iter->second<<endl;
for(iter = mapTest.begin() ; iter !=mapTest.end() ; iter++)
cout <<(*iter).second<<endl;
return 1;
}