C++ Dijkstra算法中的节点未被保存

C++ Dijkstra算法中的节点未被保存,c++,dijkstra,C++,Dijkstra,Dijkstra的算法有一个问题,我可以访问我创建的列表中的下一个节点,但是我不能将信息保存到下一个节点。我使用的结构是一个向量,它在每个索引处都保存了节点。我使用向量索引的地址来访问该索引处的节点 我使用本例中的节点制作了向量: 您正在存储指向向量中其他节点的指针。创建其副本时,新节点仍指向原始向量中的节点。特别是下一个节点列表和上一个节点中的指针 如果您通过引用传递sub,它可能会开始工作。但是,将指针存储到动态容器中通常是不安全的。当元素从向量中添加和删除时,它们可以从内存中的原始位置移动

Dijkstra的算法有一个问题,我可以访问我创建的列表中的下一个节点,但是我不能将信息保存到下一个节点。我使用的结构是一个向量,它在每个索引处都保存了节点。我使用向量索引的地址来访问该索引处的节点

我使用本例中的节点制作了向量:


您正在存储指向向量中其他节点的指针。创建其副本时,新节点仍指向原始向量中的节点。特别是下一个节点列表和上一个节点中的指针

如果您通过引用传递sub,它可能会开始工作。但是,将指针存储到动态容器中通常是不安全的。当元素从向量中添加和删除时,它们可以从内存中的原始位置移动。如果你从前面移除,那么所有剩余的元素都会向前移动。使用push_back时,可能没有足够的空间,因此可能会进行新的分配,并将元素复制/移动到一个全新的内存块中


您可以尝试使用std::vector,而不是在下一个节点列表和上一个节点中使用原始节点指针,而是使它们成为std::弱节点ptr。这将允许您在保持连接完整性的同时随意移动向量。

我正在使用的伪代码是。你给我的建议对于向量的第一个索引是可以的,但是在那之后,对于大于4的图,我得到了错误的答案。
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <limits>
#include <algorithm>
#include <queue>
#include <vector>
#define INFINITY 999

using namespace std;

struct node
{

int id;
int edge_weight;
int distance_to_source;
//used for reverse iteration to find the path
int previous;

//pointing to children of parent node and the addresses are stored
//in an array
struct node* next_node_list[3];
struct node* previous_node[3];

};



int Dijkstra(vector<node> sub,int source, int target)
{
//intializing for main while loops
int best_path_weight =0;
int end_of_loops= (sub.size());
int child = 0;

//intializing intial distances to INF to show that 
for(int i=0; i < end_of_loops; i++)
{

sub[i].distance_to_source = INFINITY;

}
//source is set to be 0
sub[source].distance_to_source=0;
//copy of sub for later access 
vector<node> copy = sub;

while(!sub.empty())
{
    cout << sub.size() << endl;

    if(sub[0].distance_to_source == INFINITY)
    {
    cout << "new node distance2source is INF" << endl;
    break;

    }
    for(int j = 0; (j < 1); j++)
    {

        if((sub[source].next_node_list[j] == NULL))
        {
        cout << "null child " <<endl;
        break;
        }

        //nextnode = (sub[0]).next_node_list[j];

        child = (sub[0].distance_to_source) + ((sub[0].next_node_list[j]->edge_weight));


        if((child) < ((sub[0].next_node_list[j])->distance_to_source))
        {

            //this is where my problem lies I beleive
            ((sub[0].next_node_list[j])->distance_to_source) = child;

            //used for a reference to have access to final paths
            copy[((sub[0].next_node_list[j])->id)].distance_to_source = child;

            ((sub[0].next_node_list[j])->previous) = sub[0].id;       
        }


    best_path_weight = copy[target].distance_to_source;

    }

    sub.erase(sub.begin());


}


return best_path_weight;
}



int main() {
vector<node> sub;

// changing size of graph
int number_of_vertices = 3;
int source = 0;
int target = 2;
if(target > number_of_vertices)
cout << "ERROR! target cannot be greater than the number of vertices!" << endl; 

for(int i=0; i < number_of_vertices; i++)
{
    //Push new node onto a vector 
    sub.push_back(node());
    //assigning information to nodes
    sub[i].id = i;
    sub[i].edge_weight = 2*i+1;

    for(int j = 0; j < 3; j++)
    {
        sub[i].next_node_list[j]=NULL;
        sub[i].previous_node[j]=NULL;
    }

}
//node linking declaration
//node 0
sub[0].next_node_list[0]=(&sub[1]);
sub[0].next_node_list[1]=(&sub[2]);

//node 1
sub[1].next_node_list[0]=(&sub[2]);
//sub[1].next_node_list[1]=(&sub[4]);
sub[1].previous_node[0]=(&sub[0]);

//node3
sub[2].previous_node[0]=(&sub[0]);
sub[2].previous_node[1]=(&sub[1]);

cout << "distance "<< Dijkstra(sub, source, target) << endl;

}