gpt4 book ai didi

c++ - 创建集合 vector

转载 作者:行者123 更新时间:2023-11-28 08:17:45 25 4
gpt4 key购买 nike

在我的代码中我定义了一个 vector :

vector<vector<vector<vector<shared_ptr<foo> > > > > fooBoxes;

我正在使用以下方法初始化 vector :

int BOX_NUM = 12 //this is actually defined elsewhere

fooBoxes.resize(BOX_NUM);
for (int i = 0; i<BOX_NUM; i++){
fooBoxes[i].resize(BOX_NUM);
for (int j = 0; j < BOX_NUM; j++){
fooBoxes[i][j].resize(BOX_NUM);
for (int k = 0; k < BOX_NUM; k++){
fooBoxes[i][j][k].resize(0);
}
}
}

我怀疑 vector 的使用导致了段错误,我想将 fooBoxes 替换为:

vector<vector<vector<set<shared_ptr<foo> > > > > fooBoxes

我在 for 循环中做什么?简单地删除 resize(0) 部分?

编辑:
这是崩溃时 valgrind 的输出:

==2258== Invalid read of size 8
==2258== at 0x439237: trans(int) (stl_iterator.h:704)
==2258== by 0x413B0E: membrane::MCstep(int) (membrane.cpp:490)
==2258== by 0x402767: main (main.cpp:14)
==2258== Address 0x7932420 is 8 bytes after a block of size 24 free'd
==2258== at 0x4A05743: operator delete(void*) (vg_replace_malloc.c:346)
==2258== by 0x405636: vec::~vec() (valarray_array.h:71)
==2258== by 0x437D66: trans(int) (transFile.cpp:64)
==2258== by 0x413B0E: membrane::MCstep(int) (membrane.cpp:490)
==2258== by 0x402767: main (main.cpp:14)
==2258==
==2258== Invalid read of size 8
==2258== at 0x439240: trans(int) (stl_vector.h:604)
==2258== by 0x413B0E: membrane::MCstep(int) (membrane.cpp:490)
==2258== by 0x402767: main (main.cpp:14)
==2258== Address 0x111 is not stack'd, malloc'd or (recently) free'd
==2258==
==2258==
==2258== Process terminating with default action of signal 11 (SIGSEGV)
==2258== Access not within mapped region at address 0x111
==2258== at 0x439240: trans(int) (stl_vector.h:604)
==2258== by 0x413B0E: membrane::MCstep(int) (membrane.cpp:490)
==2258== by 0x402767: main (main.cpp:14)
==2258== If you believe this happened as a result of a stack
==2258== overflow in your program's main thread (unlikely but
==2258== possible), you can try to increase the size of the
==2258== main thread stack using the --main-stacksize= flag.
==2258== The main thread stack size used in this run was 10485760.

我认为问题是我在尝试删除珠子/将珠子放入 vector 时不够小心,这就是我想移动到集合的原因。

最佳答案

这取决于您想要实现的目标。如果您删除 fooBoxes[i][j][k].resize(0);,您的代码肯定会编译,因为 std::set 不支持此功能.

关于c++ - 创建集合 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7105845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com