如果查看输出,可以看到listOrder的内容是正确的内容。但是,一旦我将其传递到图形构造函数中并输出相同的列表,我就会变得毫无用处。我怀疑这与我的图表Vertex *内容的adjacencyList有关,但是我不理解指针如何混乱。请帮助我将正确的节点放入图形向量中!

for (vector<char>::iterator it=listOrder.begin(); it!=listOrder.end(); ++it)
{
    cout << "ListNode: " << *it << endl;
}
Graph graph(listOrder, adjacencyList);
for (vector<char>::iterator it=graph.getOrder().begin(); it!=graph.getOrder().end(); ++it)
{
    cout << "Node: " << *it << endl;
}


建设者

Graph::Graph(vector<char> newVertices, map<char, Vertex*> newAL)
{
vertices = newVertices;
adjacencyList = newAL;
}


GetOrder函数

vector<char> Graph::getOrder()
{
return vertices;
}


输出量

ListNode: A
ListNode: B
ListNode: C
ListNode: D
ListNode: E
ListNode: F
ListNode: G
Node: ░
Node: ↨
Node: >
Node:
Node: ─
Node:
Node: >

最佳答案

每次调用getOrder时,您都将返回向量的副本,因此迭代器不是来自同一容器。

通过引用从getOrder返回向量,或者仅调用getOrder一次以获得向量的副本,然后对其进行迭代。

就像是:

vector<char> v(graph.getOrder());
for (vector<char>::iterator it=v.begin(); it!=v.end(); ++it)
{
    cout << "Node: " << *it << endl;
}

09-06 12:48