课堂内外,为什么相同的数据具有不同的值?

发布于 2025-02-06 02:12:46 字数 1210 浏览 0 评论 0原文

作为主题,我在两个晚上混淆了这个问题。

我尝试使用函数build_bdds修改地图数据结构的值,但是在函数内部打印的值与函数外部的值不同。

我将struct map_data的bool元素更改为int类型,并遇到了相同的问题。 我怀疑我使用的是错误的指针,但无法弄清楚怎么了。

这是我的代码。

#include <bits/stdc++.h>
using namespace std;

struct map_Data{
    bool hi;
    bool lo;
};
map<pair<int, int>, map_Data*> outs;

class MWVC{
    public:

    map_Data build_BDDs(map<pair<int, int>, map_Data*> & ins){
        map_Data txt;
        txt.hi = 1;
        txt.lo = 0;
        ins[make_pair(2,4)] = &txt;
        cout << ins[make_pair(2,4)]->hi << ";" << ins[make_pair(2,4)]->lo << endl;
 
        return * ins[make_pair(2,4)];
    }
};


int main(){
    MWVC ojb;
    map_Data a;
    a.hi = 1;
    a.lo = 0;
    outs[make_pair(1,2)] = &a;
    if(outs[make_pair(1,4)]){
        cout << outs[make_pair(1,4)]->hi << endl;
    }

    map_Data b = ojb.build_BDDs(outs);
   
    cout << outs[make_pair(2,4)]->hi << ";" << outs[make_pair(2,4)]->lo << ";" << b.hi << ";" << b.lo << endl;
    return 0;
}

结果是

1;0
0;0;1;0

感谢进步。

As the topic, I was confusing this question for two night.

I tried to use function build_BDDs to modify the value of map data structure outs, but the value printed inside the function is not the same as outside the function.

I changed the bool elements of struct map_Data to int type and had the same problem.
I suspect I'm using the wrong pointer, but can't figure out what's wrong.

here is my codes.

#include <bits/stdc++.h>
using namespace std;

struct map_Data{
    bool hi;
    bool lo;
};
map<pair<int, int>, map_Data*> outs;

class MWVC{
    public:

    map_Data build_BDDs(map<pair<int, int>, map_Data*> & ins){
        map_Data txt;
        txt.hi = 1;
        txt.lo = 0;
        ins[make_pair(2,4)] = &txt;
        cout << ins[make_pair(2,4)]->hi << ";" << ins[make_pair(2,4)]->lo << endl;
 
        return * ins[make_pair(2,4)];
    }
};


int main(){
    MWVC ojb;
    map_Data a;
    a.hi = 1;
    a.lo = 0;
    outs[make_pair(1,2)] = &a;
    if(outs[make_pair(1,4)]){
        cout << outs[make_pair(1,4)]->hi << endl;
    }

    map_Data b = ojb.build_BDDs(outs);
   
    cout << outs[make_pair(2,4)]->hi << ";" << outs[make_pair(2,4)]->lo << ";" << b.hi << ";" << b.lo << endl;
    return 0;
}

and the result is

1;0
0;0;1;0

Thanks advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世晨晓 2025-02-13 02:12:46

build_bdds函数存储地图中的本地变量的地址。当功能返回时,它指向的内存可以随时覆盖。

对于此情况,只需放下指针,让STD :: MAP担心内存管理:

map<pair<int, int>, map_Data> outs;

并将build_bdds更改为:

map_Data build_BDDs(map<pair<int, int>, map_Data> & ins){
        ins[make_pair(2,4)] = map_Data{1, 0};
        cout << ins[make_pair(2,4)].hi << ";" << ins[make_pair(2,4)].lo << endl;
 
        return ins[make_pair(2,4)];
    }

You build_BDDs function stores the address of a local variable in the map. When the function returns, the memory it points to can be overwritten at any time.

For this instance, just drop the pointers and let the std::map worry about memory management:

map<pair<int, int>, map_Data> outs;

and change build_BDDs to:

map_Data build_BDDs(map<pair<int, int>, map_Data> & ins){
        ins[make_pair(2,4)] = map_Data{1, 0};
        cout << ins[make_pair(2,4)].hi << ";" << ins[make_pair(2,4)].lo << endl;
 
        return ins[make_pair(2,4)];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文