向量插入分段错误第二次迭代

vector insert segmentation fault second iteration

我实际上正在尝试实现弗洛伊德的 warshall 算法,但我遇到了一个难点。当我试图在我的序列矩阵中找到最短路径时,我遇到了分段错误。我遵循本教程: floyd's warshall algorithm

除第 124 行外,一切正常:

chemin.insert(it,sequenceTest[v1-1][temp-1]);

我在哪里遇到段错误(你可以跳过第一部分,除非你想知道我是如何实现算法的,但问题出在第二部分):

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
int i,j,k;
int sequenceTest [4][4] = {{1,2,3,4},{1,2,3,4},{1,2,3,4},{1,2,3,4}};
int tempSequenceTest [4][4];
int distanceTest [4][4] = {{0,2,4,100000},{2,0,1,5},{4,1,0,3},{100000,5,3,0}};
int tempDistanceTest[4][4];

for(i=0;i<4;i++)
{
    for(j=0;j<4;j++)
    {
        cout<<sequenceTest[i][j]<<" ";
    }
    cout<< endl;
}

cout<< endl;

for(i=0;i<4;i++)
{
    for(j=0;j<4;j++)
    {
        cout << distanceTest[i][j] << " ";
    }
    cout<< endl;
}

cout<< endl;

for(k=0;k<3;k++)
{
    cout <<endl;
    for(i=0;i<4;i++)
    {
        for(j=0;j<4;j++)
        {
            cout << "i: " << i << " j: " << j << endl;
            if(i == k)
            {
                tempSequenceTest[i][j] = sequenceTest[i][j];
                tempDistanceTest[i][j] = distanceTest[i][j];
                cout << " D " << tempDistanceTest[i][j] << " S " << tempSequenceTest[i][j];
            }
            if(j == k)
            {
                tempSequenceTest[i][j] = sequenceTest[i][j];
                tempDistanceTest[i][j] = distanceTest[i][j];
                cout << " D " << tempDistanceTest[i][j] << " S " << tempSequenceTest[i][j];
            }
            cout<< endl;
        }
    }

    for(i=0;i<4;i++)
        {
            for(j=0;j<4;j++)
            {
                //cout<< "i: " << i << " j: " << j << " k:" << k << endl;
                if(i!=k && j!=k)
                {
                    if(distanceTest[i][j]> distanceTest[i][k] + distanceTest[k][j])
                    {
                        tempDistanceTest[i][j] = distanceTest[i][k] + distanceTest[k][j];
                        cout << distanceTest[i][j] << " > " << distanceTest[i][k] << " + " << distanceTest[k][j] << " = " <<  tempDistanceTest[i][j] << " " << i << j << endl;
                        tempSequenceTest[i][j] = k+1;
                    }
                    else
                    {
                        tempDistanceTest[i][j] = distanceTest[i][j];
                        tempSequenceTest[i][j] = sequenceTest[i][j];
                    }
                }
            }
        }
        cout<< endl;
        for(i=0;i<4;i++)
        {
            for(j=0;j<4;j++)
            {
                distanceTest[i][j] = tempDistanceTest[i][j];
                sequenceTest[i][j] = tempSequenceTest[i][j];
            }
        }
        for(i=0;i<4;i++)
        {
            for(j=0;j<4;j++)
            {
                cout<<tempSequenceTest[i][j]<<" ";
            }
            cout<< endl;
        }

        cout<< endl;

        for(i=0;i<4;i++)
        {
            for(j=0;j<4;j++)
            {
                cout << tempDistanceTest[i][j] << " ";
            }
            cout<< endl;
        }
}

int v1, v2;
v1 = 1;
v2 = 4;
vector <int> chemin;
vector <int>::iterator it;
chemin.push_back(v1);
chemin.push_back(v2);
int temp = v2;
cout << sequenceTest[0][2];
while(sequenceTest[v1-1][temp-1] != v2)
{
    it = chemin.begin() + 1;
    cout << "V1: " << v1 << " V2: " << v2 << " Temp: " << temp << endl;
    chemin.insert(it,sequenceTest[v1-1][temp-1]);
    for(i=0;i<chemin.size();i++)
    {
        cout << chemin[i];
    }
    cout << endl;
    temp = sequenceTest[v1-1][temp-1];
}
}

感谢您的关注和花时间帮助我!

chemin.insert(it,sequenceTest[v1-1][temp-1]);

这会使迭代器 it 无效,但您在随后的迭代中继续重复使用它。如果要在同一个位置继续插入,捕获return值。

it = chemin.insert(it,sequenceTest[v1-1][temp-1]);