如何在c ++中解决这个EXC_BAD_ACCESS(code = EXC_i386_GPFLT)

时间:2017-11-02 17:51:33

标签: c++ segmentation-fault

以下是我的计划:

使用STL set

找到Dijkstra的最短路径是程序

它可能不是做同样工作的最快版本,但我已尽力完成我需要做的事情......只是不知道为什么遇到分段错误...而且我不知道#39;知道如何确定错误的来源......感谢您的帮助!

我发现它在运行时出现分段错误(EXC_BAD_ACCESS(code = EXC_i386_GPFLT)),我该如何解决? 谢谢你回答我的问题!!

main.cpp

// Program to find Dijkstra's shortest path using STL set
#include<iostream>
#include"graph.h"
#include<string>
#include<stdlib.h>
#include<list>
#include<set>
#include<sstream>
#include<vector>
using namespace std;
# define INF 0x3f3f3f3f

void Graph::addEdge(int u, int v, int w)
{
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}

// Prints shortest paths from src to all other vertices
vector<int> Graph::shortestPath(int src)
{
// Create a set to store vertices that are being
// prerocessed
set< pair<int, int> > setds;

// Create a vector for distances and initialize all
// distances as infinite (INF)
vector<int> dist(V, INF);

// Insert source itself in Set and initialize its
// distance as 0.
setds.insert(make_pair(0, src));
dist[src] = 0;

/* Looping till all shortest distance are finalized
 then setds will become empty */
while (!setds.empty())
{
    // The first vertex in Set is the minimum distance
    // vertex, extract it from set.
    pair<int, int> tmp = *(setds.begin());
    setds.erase(setds.begin());

    // vertex label is stored in second of pair (it
    // has to be done this way to keep the vertices
    // sorted distance (distance must be first item
    // in pair)
    int u = tmp.second;

    // 'i' is used to get all adjacent vertices of a vertex
    list< pair<int, int> >::iterator i;
    for (i = adj[u].begin(); i != adj[u].end(); ++i)
    {
        // Get vertex label and weight of current adjacent of u.
        int v = (*i).first;
        int weight = (*i).second;

        //  If there is shorter path to v through u.
        if (dist[v] > dist[u] + weight)
        {
            /*  If distance of v is not INF then it must be in
             our set, so removing it and inserting again
             with updated less distance.
             Note : We extract only those vertices from Set
             for which distance is finalized. So for them,
             we would never reach here.  */
            if (dist[v] != INF)
                setds.erase(setds.find(make_pair(dist[v], v)));

            // Updating distance of v
            dist[v] = dist[u] + weight;
            setds.insert(make_pair(dist[v], v));
        }
    }
}

// Print shortest distances stored in dist[]
//printf("Vertex   Distance from Source\n");
//for (int i = 0; i < V; ++i)
  //  printf("%d \t\t %d\n", i, dist[i]);

return dist;
}

// Driver program to test methods of graph class
int main(int argc, char *argv[])
{
// create the graph given in above fugure

string Vm;
cin >> Vm;
istringstream ss(Vm);
string tokenVm;
int V, m;

std::getline(ss, tokenVm, ',');
V = stoi(tokenVm);
std::getline(ss, tokenVm, ',');
m = stoi(tokenVm);

int k;
cin >> k;

Graph g(V);

string id;
cin >> id;
istringstream sss(id);
string tokenid;
int list[k];
int i=0;

while(std::getline(sss, tokenid, ',')){
    list[i] = stoi(tokenid) << '\n';
    i++;
}

for(int j = 0; j < m; j++){
    string input;
    cin >> input;
    istringstream ssss(input);
    string tokeninput;
    int a,b,w;

    std::getline(ssss, tokeninput, ',');
    a = stoi(tokeninput);
    std::getline(ssss, tokeninput, ',');
    b = stoi(tokeninput);
    std::getline(ssss, tokeninput, ',');
    w = stoi(tokeninput);

    // read edge and insert it to graph
    g.addEdge(a, b, w);
}

string section;
cin >> section;
if(section == "NearestGasStation"){
    int shortest;
    cin >> shortest;

    int temp = g.shortestPath(shortest)[list[0]];
    for(int i=1 ; i<k; i++){
        if ( g.shortestPath(shortest)[list[i]] < temp ){
            temp = g.shortestPath(shortest)[list[i]];
        }
    }
    cout << temp << endl;

} else if (section == "ReverseNeighbors"){
    int u;
    cin >> u;

} else {
    cout << "wrong input file!" << endl;
    return 0;
}

return 0;
}

和graph.h文件。

#ifndef graph_h
#define graph_h
#include<iostream>
#include<string>
#include<stdlib.h>
#include<list>
#include<set>
#include<sstream>
#include<vector>

class Graph
{
int V;

std::list< std::pair<int, int> > *adj;

public:
Graph(int V);  // Constructor

void addEdge(int u, int v, int w);
std::vector<int> shortestPath(int s);
};

Graph::Graph(int V)
{
this->V = V;
adj = new std::list< std::pair<int, int> >[V];
}



#endif /* graph_h */

0 个答案:

没有答案