- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
来这里之前我已经努力尝试并做了我的研究。下面的代码崩溃了,我怀疑滥用了 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/
我对cassandra并使用1.2.10非常陌生。我有一个时间戳数据类型的主键列。现在,我正在尝试检索日期范围的数据。由于我们知道不能在cassandra中使用,因此我使用的是大于()来获取日期范围。
我正在尝试进行有条件的转场。但我得到: Terminating app due to uncaught exception 'NSInvalidArgumentException', reas
我有一个游戏项目,在调试和发布模式下在设备上运行得非常好。我有两个版本。旧版本和新版本具有更多(后来我添加了)功能,并且两者的 bundle ID、版本相同。当我构建旧版本时,之前没有安装“myGam
这个问题已经有答案了: 奥 git _a (2 个回答) 已关闭 5 年前。 我正在获取 ClassCastException 。这两个类来自不同的 jar,但是JettyContinuationPr
以下代码行抛出异常: HttpResponse response = client.execute(request); // actual HTTP request 我能够捕获它并打印: Log
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
public class TwoThreads { private static Object resource = new Object(); private static void
当我输入 6 (int) 作为值时,运行此命令会出现段错误 (gcc filename.c -lm)。请帮助我解决这个问题。预期的功能尚未实现,但我需要知道为什么我已经陷入段错误。 谢谢! #incl
所以,过去一周半我一直在研究这个 .OBJ/.MTL 网格解析器。在这段时间里,我一直在追踪/修复很多错误、清理代码、记录代码等等。 问题是,每修复一个错误,仍然会出现这个问题,而且一张图片胜过一千个
我正在运行一个代码,它基本上围绕 3 个维度旋转一个大数据数组(5000 万行)。但是,我遇到了一个奇怪的问题,我已将其缩小到如何评估旋转矩阵。基本上,对于除绕 x 轴以外的任何旋转,python 代
就在你说这是重复之前,我已经看到了其他问题,但我仍然想发布这个。 所以我正在阅读 Thinking in Java -Bruce Eckel 这篇文章是关于小写命名约定的: In Java 1.0 a
我想在我的应用程序中使用 REST API。它为我从这个应用程序发出的所有请求抛出 SocketTimeoutException。 Logcat 输出:(您也可以在此处看到带有漂亮格式的输出:http
我知道 raise ... from None 并已阅读 How can I more easily suppress previous exceptions when I raise my own
在未能找到各种Unix工具(例如xargs和whatnot)的最新独立二进制文件(this version很好,但需要外部DLL)后,我承担了自己进行编译的挑战。 ...这是痛苦的。 最终,尽管如此,
我有一个用PHP编写的流套接字服务器。 为了查看一次可以处理多少个连接,我用C语言编写了一个模拟器来创建1000个不同的客户端以连接到服务器。 stream_socket_accept几次返回fals
我的Android Studio昨天运行良好,但是今天当我启动Android Studio并想在移动设备上运行应用程序时,发生了以下错误, 我在互联网和stackoverflow上进行了搜索,但没有解
默认情况下,grails似乎为Java域对象的toString()返回:。那当然不是我想要的,所以我尝试@Override toString()返回我想要的。当我尝试grails generate-a
尝试通过LDAP通过LDAP对用户进行身份验证时,出现以下错误。 Reason: Cannot pass null or empty values to constructor. 谁能告诉我做错了什么
我正在尝试使用应用程序附带的 Houdini Python 模块,该模块是 Houdini 安装文件夹的一部分,位于标准 Python 路径之外。按照安装说明操作后,运行 Houdini Termin
简单地说,我正在为基本数据库编写单链表的原始实现。当用户请求打印索引下列出的元素高于数据库中当前记录数量时,我不断出现段错误,但仅当差值为 1 时。对于更高的数字,它只会触发我在那里编写的错误系统。
我是一名优秀的程序员,十分优秀!