c++结构中的列表
List inside struct in c++
我在c++中有一个结构体中的列表;我只是想插入元素到这个列表,像往常一样。
我的结构是:
// A structure to represent an adjacency list node
struct AdjListNode
{
int dest;
int weight;
std::list<int> adjacents;
struct AdjListNode* next;
};
// A structure to represent an adjacency list
struct AdjList
{
int pos;
struct AdjListNode *head; // pointer to head node of list
};
// A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph
{
int V;
struct AdjList* array;
};
struct Graph* createGraph(int V)
{
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;
// Create an array of adjacency lists. Size of array will be V
graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));
// Initialize each adjacency list as empty by making head as NULL
for (int i = 0; i < V; ++i) {
graph->array[i].head = NULL;
}
return graph;
}
当我试图访问:
graph->array[position].head->adjacents->push_back(number);
它只是提示我:
进程结束,退出码139(被信号11:SIGSEGV中断)
对不起,我不知道这个错误。
分段故障来自
graph->array[position].head->adjacents.push_back(number);
graph->array[position].head = NULL;
我假设你的代码中有隐式结构不变量,因为你有两个可能连接的列表:从AdjList::head
开始并迭代到AdjNode::next
的链表和列表AdjNode::adjacent
。
为了保持连接,你可以添加一个(C风格的)函数,在两个列表中添加一个元素。
void
addAdjacent(AdjList& list, int adjacent) {
// struct AdjListNode* newNode = (struct AdjListNode*) malloc(sizeof(struct AdjListNode));
struct AdjListNode* newNode = new AdjListNode;
newNode->next = list.head;
list.head = newNode;
newNode->dest = 0;
newNode->weight = 0;
newNode->adjacents = std::list<int>(); // undefined behavior with malloc
newNode->adjacents.push_back(adjacent);
}
注意,将C风格(malloc/free)与c++风格(特别是与标准模板库的容器)混合是一个坏主意。我的代码的注释部分创建了一个分段错误,因为std::list
没有用0填充其字段。
最后,下面的main
函数即使有许多内存泄漏(参见valgrind工具)也能工作
int main(int argc, char** argv) {
struct Graph* graph = createGraph(2);
addAdjacent(graph->array[0], 1);
addAdjacent(graph->array[1], 2);
free(graph);
return 0;
}
c++ -98的解决方案(没有任何内存泄漏)可以是:
// A structure to represent an adjacency list node
struct AdjListNode
{
int dest;
int weight;
std::list<int> adjacents;
struct AdjListNode* next;
AdjListNode() : dest(0), weight(0), next(NULL) {}
};
// A structure to represent an adjacency list
struct AdjList
{
int pos;
struct AdjListNode *head; // pointer to head node of list
// Initialize each adjacency list as empty by making head as NULL
AdjList() : pos(0), head(NULL) {}
~AdjList()
{ while (head) {
struct AdjListNode* temp = head;
head = head->next;
delete temp;
}
}
void addAdjacent(int adjacent)
{ struct AdjListNode* newNode = new AdjListNode;
newNode->next = head;
head = newNode;
newNode->adjacents.push_back(adjacent);
}
};
// A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph
{
int V;
struct AdjList* array;
// Create an array of adjacency lists. Size of array will be V
Graph(int v) : V(v), array(NULL)
{ if (v >= 0 && v <= 1000)
array = new struct AdjList[v];
else
throw std::bad_alloc();
}
~Graph()
{ delete [] array; }
};
int main() {
struct Graph* graph = new Graph(2);
graph->array[0].addAdjacent(1);
graph->array[1].addAdjacent(1);
delete graph;
return 0;
}
相关文章:
- 没有为自己的结构调用列表推回方法
- 访问类lintalizer列表中的结构元素
- 将大括号括起来的初始值设定项列表作为结构返回
- 为什么这个 std::queue/指向结构的指针列表直到 List.Size() == 0 才释放内存?
- C++-将具有引用的长参数列表重构为结构
- 在C++中循环访问自定义结构列表的小问题
- 访问列表向量中的结构
- c ++数据输入并读回列表中的结构结构中的数组
- 如何在结构初始值设定项列表中实例化类
- 结构数组的构造函数错误,错误消息:没有构造函数实例与参数列表匹配
- 使用初始化列表填充C++中的多维结构数组时出现问题
- 使用列表进行动态结构分配
- 如何从结构列表中筛选信息
- 当接收到不明确的规范时,表示图的邻接列表的数据结构
- 用于筛子的最佳数据结构是什么(即一些被划掉的数字列表)?
- C/C++intellisense 0.26.1不显示结构成员列表
- C++使用旧编译器GCC 4.4.0在结构列表中查找字符串
- 列表结构和数组的奇怪问题
- 释放包含STL类的链接列表结构的内存
- c++游戏的列表结构