我正在使用 C++ priority_queue 数据结构来存储大量数据。但是,我总是遇到分段错误,可能是由于堆栈溢出错误。我的 priority_queue 定义是:
typedef struct cell {
int x;
int y;
float value;
}*cell_p;
struct cell_comparator {
bool operator()(cell_p arg1, cell_p arg2) {
return arg1->value > arg2->value;
}
};
priority_queue<cell_p, vector<cell_p>, cell_comparator>* open;
在主要功能中:
open = new priority_queue<cell_p, vector<cell_p>, cell_comparator>();
int x, y;
for (x = 0; x < nXSize; x++) {
for (y = 0; x < nYSize; y++) {
int index = (y * nXSize) + x;
cell_p c = (cell_p) malloc(sizeof(cell));
c->x = x;
c->y = y;
c->value = input_buffer[index];
open->push(c);
}
}
作为一个容器类,我使用了向量,但似乎它将数据存储在堆栈上,而不是堆上。如何告诉 priority_queue 将数据存储在堆上?