遇到个C++ MAP问题,有谁会的么?
Published on 2022-05-28 15:34 in 暂未分类 with 萧海~

遇到个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 @   萧海~  阅读(19)  评论(0编辑  收藏  举报
    相关博文:
    阅读排行:
    · winform 绘制太阳,地球,月球 运作规律
    · AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
    · 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
    · 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
    · 上周热点回顾(3.3-3.9)
    点击右上角即可分享
    微信分享提示
    电磁波切换