c++ - Kruskal 算法代码因未知原因崩溃

标签 c++ cout

来这里之前我已经努力尝试并做了我的研究。下面的代码崩溃了,我怀疑滥用了 cout。 (这不应该是最好的实现,但暂时不是 pb)

有经验的人能看出问题出在哪里吗?

提前致谢

#include <cmath>
#include <iostream>
#include <cstdlib>
#include <fstream> //file io
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;

template <typename T>
  string NumberToString ( T Number )
  {
     ostringstream ss;
     ss << Number;
     return ss.str();
  }

class Point { //an instance of this class represents the integer triples: (i, j, cost).

    int Edge1, Edge2, Cost;

    public:
    Point(int x, int y, int z) : Edge1(x), Edge2(y), Cost(z) {}
    Point() {Edge1=Edge2=Cost=0;}

    bool operator<(Point const &other) {
        return (Cost < other.Cost);
    }


    void setEdge1(int x){this->Edge1=x;}
    void setEdge2(int x){this->Edge2=x;}
    void setCost(int x){this->Cost=x;}
    int getEdge1(){return this->Edge1;}
    int getEdge2(){return this->Edge2;}
    int getCost(){return this->Cost;}

};

class GRAPH
{
    private:
    vector<Point> GraphMatrix;
    int GRAPH_vertex=0;
    int initialised=0;

public:

    GRAPH(string Path)
    {

    this->initialised=1;
     // import input data from file
    vector<int> reader(10);
    ifstream ifp(Path, ios::in);
    int ii = 0;
    while(!ifp.eof() )
    {
    ifp >> reader[ii++];
    if (ii%9 ==0)
    reader.resize(reader.size() +10);
    }
    reader.resize(ii-1);

    //End of data import


    this->GRAPH_vertex=reader[0];// Number of vertices set

    for(int i=0;i<(ii-2)/3;i++)
    {
        Point punto(reader[1+3*i],reader[2+3*i],reader[3+3*i]);

        GraphMatrix.insert(GraphMatrix.end(),punto);
    }

    }


    //Copy constructor omited

    ~GRAPH() //destructor
    {
        if (this != NULL)
        delete this;
    }


    int Get_GRAPH_vertex(){return GRAPH_vertex;}
    vector<Point> Get_GraphMatrix() {return GraphMatrix;}

    void Kruskal();
    friend bool compareTwoPoint(Point,Point);
};

bool compareTwoPoint(Point rowA, Point rowB){
     return ( rowA.getCost()<rowB.getCost() );
 }

void GRAPH::Kruskal()
{
    int n_vertices=this->GRAPH_vertex;
    std::sort(GraphMatrix.begin(),GraphMatrix.end(),&compareTwoPoint);
    vector <int> temp1(n_vertices*n_vertices,0);
    int minimumcost=0;
    int Iteration=0;
    vector<string> Tree;
    for (std::vector<Point>::iterator it=GraphMatrix.begin(); it!=GraphMatrix.end(); ++it)
    {
        int ii=it->getEdge1();
        int jj=it->getEdge2();
        if((temp1[ii+n_vertices*jj] !=1)&& Iteration<n_vertices)
        {
            temp1[ii+n_vertices*jj]=1;
            temp1[jj+n_vertices*ii]=1;
            minimumcost+=it->getCost();
            Iteration+=1;
            Tree.push_back(NumberToString(ii)+"->"+ NumberToString(jj));
        }
    }
            cout<<Iteration<<'\n';
            cout<<"minimum cost is"+ NumberToString(minimumcost)<<'\n';


            for (vector<string>::iterator p = Tree.begin();
                p != Tree.end(); ++p)
                {
                cout << *p << '\n';
                cout << endl;
                }
}


int main()
{
    GRAPH grafe("C:/Users/Algoris/Desktop/simplon.txt");

    grafe.Kruskal();
}

//txt文件输入示例

20
0 1 17 
0 2 2 
0 3 9 
0 4 24 
0 5 28 
0 6 29 
0 7 14 
0 8 28 
0 9 13 
0 10 23
0 11 10
0 12 15
0 13 23
0 14 15
0 15 18
0 16 11
0 17 4
0 18 27
0 19 5 

最佳答案

您能否在调试器下运行程序直到它崩溃,然后发布堆栈跟踪?这将告诉您问题出在哪里。

跳出来的一个问题是:

~GRAPH() //destructor
{
    if (this != NULL)
    delete this;
}

当对象被删除(从堆中)或超出范围(在堆栈中)时调用析构函数。所以到目前为止,它已经被删除了。 this 指针在实例方法中将是有效的且非 NULL,因此它试图对您的 GRAPH 对象执行双重 delete

一般来说,您应该调用delete this。 (这唯一有效的情况是您正在实现自己的内存管理,例如引用计数方案或智能指针。)

析构函数应该释放对象拥有的内存,而不是对象本身。

关于c++ - Kruskal 算法代码因未知原因崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19966750/

相关文章:

c++ - 链接器错误未解析的外部符号与我的 cnt 变量

c++ - 嵌套 std::array 时结构初始值设定项中的多余元素

c++ - 如何查找进程是否挂起?

c++ - 为什么这个 volatile 变量的地址总是为 1?

c++ - C/C++ 中的搜索字符串解析器

c++ - Windows——在子进程中继承控制台文件句柄

c++ - Debug模式自发关闭?

c++ - 错误:'std::cout 中的 'operator<<"不匹配

c++ - 如何在 C++ 中打印 Unicode 字符?

c++ - C++ 中的模板类和函数重载