简体   繁体   English

图形,顶点,STL列表:为什么列表出来为空?

[英]Graphs, vertices, STL lists: Why does list come out empty?

I posted this question earlier but badly worded which is why it got bad reactions. 我早些时候发布了这个问题,但措辞不好,这就是为什么它得到不好的反应。 I will try to be clear in what I am asking. 我会尝试明确我的要求。 Please comment if you do not understand my question. 如果您不明白我的问题,请发表评论。 I am using two lists, one to hold vertices, and another to hold the neighbors of each vertex. 我正在使用两个列表,一个用于保存顶点,另一个用于保存每个顶点的邻居。 When I create a vertex, I push it into the first list. 创建顶点时,将其推入第一个列表。 When I want to create edges, I find the vertex in my 'keys' list and add the neighboring vertex into its 'neighbor' list. 当我想创建边时,我在“键”列表中找到顶点,并将相邻的顶点添加到其“邻居”列表中。 I repeat for all edges. 我为所有边缘重复。 However, my neighbor list always comes out empty. 但是,我的邻居列表总是空着。 I check if it did push the vertex into the neighbor list with a print statement in the addEdge function, which shows that it does go in. The neighbor list always come up empty when I try to access them later. 我使用addEdge函数中的打印语句检查是否确实将顶点推入了邻居列表,这表明它确实进入了。当我稍后尝试访问它们时,邻居列表总是显示为空。 Why does that happen? 为什么会这样呢? Why does the second list always come empty? 为什么第二个列表总是空的? Is the memory not preserved? 是否保留了内存?

Eg: Create vertex 1, 2, 3 [stored in keys list] 例如:创建顶点1、2、3 [存储在键列表中]

Create edge: 1-1, 1-2 (2-1), 1-3 (3-1 [stored in neighbor list] 创建边:1-1、1-2(2-1),1-3(3-1 [存储在邻居列表中]

Find edges of 1: nothing is found (why?) 查找1的边:什么也没找到(为什么?)

My implementation. 我的实现。 The graph class holds the keys list, the vertex holds the neighbor list. 图类保存键列表,顶点保存邻居列表。 The functions that I use to add edges are named: addEdge in both classes. 我用来添加边的函数的名称为:两个类中的addEdge。 The main also prints that the vertices have no neighbors. 主视图还打印出顶点没有邻居。

The central issue is that you are passing around values, modifying copies of them and expecting the original to be modified as well. 中心问题是您正在传递值,修改它们的副本,并期望原始值也被修改。 In particular, the issues are 特别是这些问题是

  1. You want getV to return a non-const reference to a vertex<T> . 您希望getV返回对vertex<T>的非常量引用。 Note that the name get is usually associated with a const method that doesn't affect the object. 请注意,名称get通常与不影响该对象的const方法相关联。 So, you might want to change the name a bit. 因此,您可能需要稍微更改名称。
  2. In graph::addEdge , sKey and dKey should really be vertex<T>& . graph::addEdgesKeydKey应该确实是vertex<T>& Also, when you call addEdge on sKey and dKey , pass in the entire vertex<T> object that you want to add, instead of the object of type T . 此外,当调用addEdgesKeydKey ,通过在整个vertex<T>要添加对象,而不是类型的对象T The code, as it stands, compiles because T can be implicitly converted to vertex<T> but that isn't what you want. 就目前而言,代码可以编译,因为T可以隐式转换为vertex<T>但这不是您想要的。
  3. Lastly, why are you maintaining a list of vertex<T> s in vertex ? 最后,你为什么要保持清单vertex<T> s中vertex Change it to a list<vertex<T>*> s. 将其更改为list<vertex<T>*> Note that if you end up changing the data structure in graph from list to something else that could invalidate iterators and pointers to its elements, this approach will fail. 请注意,如果最终将graph的数据结构从list更改为其他可能会使迭代器和指向其元素的指针无效的方法,则此方法将失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM