简体   繁体   中英

vector insert segmentation fault second iteration

I'm actually trying to implement the floyd's warshall algorithm but I met a hard-point. I got a segmentation fault while i'm trying to find the shortest-way in my matrix of sequence. I follow this tutorial: floyd's warshall algorithm

Everything work well except at the line 124:

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

where i get the segfault (You can skip the first part except if you want to know how I implement the algorithm butt the problem is in the second part):

#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];
}
}

Thanks for your attention and for taking the time to help me!

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

This invalidates the iterator it , but you keep reusing it in the iterations that follow. If you want to continue inserting at the same position, capture the return value.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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