두 개의 노드 포인터 벡터가 있습니다.포인터를 const로 만들기 C++
vector<Node*> tmp;
vector<Node*> nodes;
내가 delete nodes
는 다음 tmp
도 삭제되면 나는 tmp = nodes;
을 말한다.
nodes
을 수정할 때마다 tmp
을 수정할 수 없습니까?
나는 기본적으로 Dijkstra 's를 실행 중입니다. 그러나이 알고리즘은 최단 경로를 결정할 때 항상 노드 벡터를 삭제합니다. 즉, 출발지에서 목적지까지 최단 경로 만 한 번만 수행 할 수 있습니다. 나는 그래프 0 ---> 1 ---> 2 ---이있는 경우 nodes
포인터 아니므로
> 즉
CGraph g;
g.Dijkstras(0);
g.PrintShortestRouteTo(2);
g.Dijktras(2);
g.PrintShortestRouteTo(3); <-- source is still 0 since nodes is deleted
therefore I get the path from 0 to 2
class Node
{
public:
Node(int id)
: id(id), previous(NULL),
distanceFromStart(INT_MAX)
{
nodes.push_back(this);
}
public:
int id;
Node* previous;
int distanceFromStart;
};
vector<Node*> nodes;
void CGraph::Dijkstras(int source)
{
nodes[source]->distanceFromStart = 0;
for (int i = 0; i < (int)nodes.size(); i++)
cout << nodes[i]->distanceFromStart << " " ;
cout << "------------------------" << endl;
while (nodes.size() > 0)
{
Node* smallest = ExtractSmallest(nodes);
//cout << "Smallest: " << smallest->id << " ";
//Node* smallest = nnodes[1];
vector<Node*>* adjacentNodes =
AdjacentRemainingNodes(smallest);
const int size = adjacentNodes->size();
for (int i=0; i<size; ++i)
{
Node* adjacent = adjacentNodes->at(i);
int distance = Distance(smallest, adjacent) +
smallest->distanceFromStart;
if (distance < adjacent->distanceFromStart)
{
adjacent->distanceFromStart = distance;
adjacent->previous = smallest;
}
}
delete adjacentNodes;
}
}
'delete nodes'는 컴파일되지 않으며,'nodes'는 포인터가 아닙니다. – GManNickG
'벡터 * tmp'을 의미 했습니까? –
iammilind
예, nodes는 포인터입니다. 나는 그것을 고정했다 – CyberShot