stl::list 对象的最大数量 [英] Maximum number of stl::list objects

查看:54
本文介绍了stl::list 对象的最大数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是在数据集中找到周期性图形模式.所以我有 1000 个时间步长,每个时间步长有一个图(编码为整数).因此,有 999 个可能的时间段可以出现该图.我还定义了一个相位偏移,定义为(timestep mod period).对于在周期为 2 的第 5 个时间步中首次看到的图,相位偏移为 1.

The problem is to find periodic graph patterns in a dataset. So I have 1000 timesteps with a graph(encoded as integers) in each timestep. So, there are 999 possible periods in which the graph can occur. Also I define a phase offset defined as (timestep mod period). For a graph which was first seen in the 5th timestep with period 2, the phase offset is 1.

我正在尝试用 C++ 创建一个二维列表数组.每个单元格包含一个列表,其中包含具有指定周期和相位偏移的图形.我不断在相应的列表中插入图表.

I am trying to create a bidimensional array of lists in C++. Each cell contains a list containing graphs having a specified period and phase offset. I keep inserting graphs in the corresponding lists.

list<ListNode> A[timesteps][phase offsets]

ListNode 是一个有 4 个整数变量的类.

ListNode is a class with 4 integer variables.

这给了我分段错误.使用 500 作为大小运行良好.这是因为内存不足还是其他问题?

This gives me Segmentation fault. Using 500 for the size runs fine. Is this due to lack of memory or some other issue?

谢谢.

推荐答案

听起来您的堆栈空间不足.尝试在堆上分配它,例如通过 std::vector,并包裹在 try ... catch 中以查看内存不足错误而不是崩溃.

Sounds like you're running out of stack space. Try allocating it on the heap, e.g. through std::vector, and wrap in try ... catch to see out of memory errors instead of crashing.

(不要使用 std::array 因为它也在堆栈上分配.)

( Don't use std::array since it also allocates on the stack.)

try {
   std::vector<std::list<ListNode> > a(1000000);   // create 1000*1000 lists
   // index a by e.g. [index1 * 1000 + index2]
   a[42 * 1000 + 18].size(); // size of that list

   // or if you really want double subscripting without a wrapper function:
   std::vector<std::vector<std::list<ListNode> > > a(1000);
   for (size_t i = 0; i < 1000; ++i) {   // do 1000 times:
       a[i].resize(1000);                // default-create and create 1000 in each
   }
   a[42][18].size(); // size of that list

} catch (std::exception const& e) {
    std::cerr << "Caught " << typeid(e).name() << ": " << e.what() << std::endl;
}

这篇关于stl::list 对象的最大数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆