【发布时间】:2012-12-17 06:34:27
【问题描述】:
我尝试解决这个问题大约 5 天,但没有运气,我尝试的每个解决方案都失败了。
我在下面找到了 SIGSEGV 的一些原因,但没有任何帮助 What is SIGSEGV run time error in C++?
好的,这里是代码。 我有 2 个实例,其中包含一些关键字特征及其分数
我想得到他们的欧几里得距离,这意味着我必须保存每个实例的所有关键字,然后找到第一个关键字与第二个关键字的差异,然后找到其余的差异二审的。我想要的是在迭代第一个地图时,能够从第二个地图中删除元素。由于我们有两个消息集合,因此会多次调用以下方法,并将第一个消息集合中的每条消息与第二个消息集合中的每条消息进行比较。
我有这段代码,但它突然停止了,尽管我检查了它在某些地方放置了多个 cout 的情况下工作了几秒钟
请注意,这是针对大学任务的,因此我不能使用 boost 和所有这些技巧。但我想知道绕过我遇到的问题的方法。
float KNNClassifier::distance(const Instance& inst1, const Instance& inst2) {
map<string,unsigned> feat1;
map<string,unsigned> feat2;
for (unsigned i=0; i<inst1.getNumberOfFeatures(); i++) {
feat1[inst1.getFeature(i)]=i;
}
for (unsigned i=0; i<inst2.getNumberOfFeatures(); i++) {
feat2[inst2.getFeature(i)]=i;
}
float dist=0;
map<string,unsigned>::iterator it;
for (it=feat1.begin(); it!=feat1.end(); it++) {
if (feat2.find(it->first)!=feat2.end()) {//if and only if it exists in inst2
dist+=pow( (double) inst1.getScore(it->second) - inst2.getScore(feat2[it->first]) , 2.0);
feat2.erase(it->first);
}
else {
dist+=pow( (double) inst1.getScore(it->second) , 2.0);
}
}
for (it=feat2.begin(); it!=feat2.end(); it++) {//for the remaining words
dist+=pow( (double) inst2.getScore(it->second) , 2.0);
}
feat1.clear(); feat2.clear(); //ka8arizoume ta map gia thn epomenh xrhsh
return sqrt(dist);
}
我也尝试了这个想法,以便不必删除某些内容,但它也突然停止了。
float KNNClassifier::distance(const Instance& inst1, const Instance& inst2) {
map<string,unsigned> feat1;
map<string,unsigned> feat2;
map<string,bool> exists;
for (unsigned i=0; i<inst1.getNumberOfFeatures(); i++) {
feat1[inst1.getFeature(i)]=i;
}
for (unsigned i=0; i<inst2.getNumberOfFeatures(); i++) {
feat2[inst2.getFeature(i)]=i;
exists[inst2.getFeature(i)]=false;
if (feat1.find(inst2.getFeature(i))!=feat1.end()) {
exists[inst2.getFeature(i)]=true;
}
}
float dist=0;
map<string,unsigned>::iterator it;
for (it=feat1.begin(); it!=feat1.end(); it++) {
if (feat2.find(it->first)!=feat2.end()) {
dist+=pow( (double) inst1.getScore(it->second) - inst2.getScore(feat2[it->first]) , 2.0);
}
else {
dist+=pow( (double) inst1.getScore(it->second) , 2.0);
}
}
for (it=feat2.begin(); it!=feat2.end(); it++) {
if(it->second==false){//if it is true, it means the diff was done in the previous iteration
dist+=pow( (double) inst2.getScore(it->second) , 2.0);
}
}
feat1.clear(); feat2.clear(); exists.clear();
return sqrt(dist);
}
【问题讨论】:
-
您是否运行调试器来找到它崩溃的行?
-
Basilis,您显示的代码似乎格式正确。如果您不想浪费更多的时间,在调试器中运行它将是至关重要的。不要运行
basilis_prog,而是运行gdb basilis_prog。r将运行它直到它崩溃。bt会告诉你崩溃发生的确切位置。 -
我使用 netbeans。我究竟如何使用 gdb 运行程序?调试器有什么技巧吗?
-
你可以在netbeans中使用调试器。右键单击项目节点并选择“调试”。您现在可以看到它在哪里崩溃,甚至可以在您慢慢接近崩溃时检查正在发生的事情。如果您有特定的调试问题,请在此处发布。我们已经开始离开这个问题的初衷。您必须相信我,对许多此类问题的一般答案是“学习如何使用调试器”。
-
好的,我有具体的问题。我运行调试器等待看到熟悉的东西,但我得到的只是这 2 [main] spam_filter 3520 exception::handle: Exception: STATUS_ACCESS_VIOLATION 686 [main] spam_filter 3520 open_stackdumpfile: Dumping stack trace to spam_filter.exe.stackdump
标签: c++ map iterator segmentation-fault