向量的向量,错误的分配

发布于 2024-08-02 05:04:17 字数 753 浏览 2 评论 0原文

我有一个名为 Contact 的类,我想构建一个指向这些对象的指针的数据结构,例如 127 行 20 列的矩阵。 然后,我尝试以这种方式使用 std::vector 类

std::vector < std::vector<Contact* > > matrix (127, std::vector < Contact* > (20));

,在标头中声明了以下内容后

std::vector<std::vector<Contact* > > Buckets;

,我将之前初始化和声明的矩阵分配给它(这一步是因为基本上我不知道如何在更清晰和简短的方法):

Buckets = matrix;

但是使用push_back函数,比如

Buckets[pot].push_back(cont_temp);

一段时间后会产生错误(“在抛出‘std::bad_alloc’实例后终止调用”),我不知道如何修复它。

还有其他更好的方法来实例化和初始化矩阵吗?您会建议其他解决方案而不是使用向量向量(boost::multiarray..?)吗?

谢谢 (抱歉这个愚蠢的问题,我是一个差生:)

编辑:我发现了错误(只是作业超出范围)。如果您对这种数据结构有一般性建议,我仍然在这里......

I've a class named Contact and I want to build a data structure of pointers to those objects like a matrix of 127 rows and 20 columns.
I've tried to use the std::vector class in this way

std::vector < std::vector<Contact* > > matrix (127, std::vector < Contact* > (20));

then, having declarated the following in the header

std::vector<std::vector<Contact* > > Buckets;

I assign the matrix initialized and declared before to it (this step because basically I don't know how to do it in a more clear and short way):

Buckets = matrix;

but using the push_back function like

Buckets[pot].push_back(cont_temp);

after a while produces an error ("terminate called after throwing an instance of 'std::bad_alloc'") and I don't know how to fix it.

Is there any other better way to instantiate and initialize the matrix? Would you suggest other solutions instead of using a vector of vectors (a boost::multiarray..?) ?

thanks
(sorry for the stupid question, I'm a poor student:)

Edit: I've found the error (just an assignment out of bounds). If you have general suggestions for this kind of data structure I'm still here...

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

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

发布评论

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

评论(3

怂人 2024-08-09 05:04:17

当你说“过了一会儿”时,这是什么意思? std::bad_alloc 意味着你内存不足。你有一个消耗内存的循环吗?

When you say "after a while", what does that mean? std::bad_alloc means you ran out of memory. Do you have a loop that gobbles up memory?

戏蝶舞 2024-08-09 05:04:17

矩阵中的联系人是动态分配的吗?如果是这样,当你说:

Buckets = matrix;

你最终会得到两个指向同一个动态分配对象的指针,这只会导致麻烦。您应该使用智能指针向量,例如 Boost 的共享指针。

Are the Contacts in the matrix dynamically allocated? If so, when you say:

Buckets = matrix;

you end up with two pointers pointing to the same dynamically allocated object, which can only lead to trouble down the line. You should use vectors of smart pointers, such as Boost's shared pointers, instead.

忘你却要生生世世 2024-08-09 05:04:17

如果您只想使用已知大小的二维矩阵,则可以使用简单的数组:

(Contact*) matrix[127][20];

当然,如果大小可以在编译后确定或更改,则此方法不起作用。
在这种情况下,我建议您转向 boost 库,特别是 multi_array 。

有关简短示例,请参阅此处或者那里获取完整的文档。

typedef boost::multi_array<Contact*, 2> ContactContainer;
ContactContainer matrix(boost::extents[127][20]);

If you only mean to use a 2d matrix of known sizes, you may use a simple array :

(Contact*) matrix[127][20];

Of course, this approach does not work if sizes can be determined or change after compilation.
In this case, i suggest you turn to boost libraries, multi_array in particular.

See here for a short example or there for the complete documentation.

typedef boost::multi_array<Contact*, 2> ContactContainer;
ContactContainer matrix(boost::extents[127][20]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文