遇到个C++ MAP问题,有谁会的么?

typedef struct pos
{
    int x;
    int y;
    bool operator < (const pos &o) const
    {

        if((x==o.x)&&(y==o.y))
            return false;
        return true;
    }
}pos;

pair<map<pos,int>::iterator, bool> ret;
static map<pos,int>posMap;
void fss(vector<vector<int>>& image,int x,int y,int num)
{
    if(x<0)
        return;
    if(x>=image.size())
        return;
    if(y<0)
        return;
    if(y>=image[0].size())
        return;

    pos p;
    p.x=x;
    p.y=y;
    ret= posMap.insert(pair<pos,int>(p,1));
    if(!ret.second)
    {
        return;
    }



    if(image[x][y]==num)
	{

        fss(image,x-1,y,num);//左递归
        fss(image,x+1,y,num);//右递归
        fss(image,x,y-1,num);//上递归
        fss(image,x,y+1,num);//左递归

    }
}
int main(int argc, char *argv[])
{
    vector<int>vec;
    int arr[]={0,0,0};
    for(auto i=0;i<sizeof(arr)/sizeof(int);i++)
        vec.push_back(arr[i]);

    vector<int>vec2;
    int arr2[]={0,0,0};
    for(auto i=0;i<sizeof(arr2)/sizeof(int);i++)
        vec2.push_back(arr2[i]);

    vector<int>vec3;
    int arr3[]={1,0,1};
    for(auto i=0;i<sizeof(arr)/sizeof(int);i++)
        vec3.push_back(arr3[i]);



    vector<vector<int>> coordinates;
    coordinates.push_back(vec);
    coordinates.push_back(vec2);



    fss(coordinates,0,0,0);




    cout<<posMap.size()<<endl;
    map<pos,int>::iterator it;
    for(it=posMap.begin();it!=posMap.end();++it)
    {
        cout<<it->first.x<<" "<<it->first.y<<endl;
    }
    return 0;
}

结果是:

12
1 0
1 2
0 0
1 0
1 1
1 2
0 2
0 0
0 1
1 1
1 0
0 0

不是说map键具有唯一性么?谁能解释下

posted @ 2022-05-28 15:34  萧海~  阅读(16)  评论(0编辑  收藏  举报