指针做MAP的KEY的TEST

用struct做map的key会需要"operator <"等等,还会出现奇怪的问题可能。

试了下用指针做key,看看效果:

#include <iostream>
#include <map>
#include <vector>
using namespace std;

//////////////////////////////////////////////////////////////////////////

struct s
{
    int x;
    int y;
};

void main()
{
    map<s*, int> m;
    s* a = new s;
    a->x = 1;
    a->y = 2;

    s* b = new s;
    b->x = 1;
    b->y = 2;

    m.insert(pair<s*, int>(a, 11));

    if (m.find(a) != m.end())
    {
        cout<<"y for a"<<endl;
    }
    else
    {
        cout<<"x for a"<<endl;
    }

    if (m.find(b) != m.end())
    {
        cout<<"y for b"<<endl;
    }
    else
    {
        cout<<"x for b"<<endl;
    }

    system("pause");
}

//output:
//y for a
//x for b

 

posted on 2013-10-10 03:30  shizuka  阅读(278)  评论(0编辑  收藏  举报

导航