[cpp]
#include <map>
using namespace std;
struct grid
{
unsigned short x;
unsigned short y;
bool operator<(const grid& g)const
{
if ( y < g.y)
{
return true;
}
else if(y==g.y && x < g.x)
{
return true;
}
return false;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
map<grid, int> lev;
grid g;
g.x = 0;
g.y = 0;
lev[g] = 1;
g.x = 0;
g.y = 1;
lev[g] = 2;
g.x = 1;
g.y = 0;
lev[g] = 3;
g.x = 1;
g.y = 1;
lev[g] = 4;
grid gg;
gg.x = 1;
gg.y = 0;
int nn = lev[gg];
return 0;
}
#include <map>
using namespace std;
struct grid
{
unsigned short x;
unsigned short y;
bool operator<(const grid& g)const
{
if ( y < g.y)
{
return true;
}
else if(y==g.y && x < g.x)
{
return true;
}
return false;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
map<grid, int> lev;
grid g;
g.x = 0;
g.y = 0;
lev[g] = 1;
g.x = 0;
g.y = 1;
lev[g] = 2;
g.x = 1;
g.y = 0;
lev[g] = 3;
g.x = 1;
g.y = 1;
lev[g] = 4;
grid gg;
gg.x = 1;
gg.y = 0;
int nn = lev[gg];
return 0;
}
STL中map,當使用自定義對象做key時,需要重載<操作符,示例