c++ - 在 C++ 中迭代 map 时发出 SIGSEGV 信号

标签 c++ dictionary iterator segmentation-fault

我尝试修复此问题大约 5 天,但没有成功,我尝试的每个解决方案都失败了。

我在下面找到了 SIGSEGV 的一些原因,但没有任何帮助 What is SIGSEGV run time error in C++?

好的,这是代码。 我有 2 个实例,其中包含一些关键字特征及其分数

我想得到它们的欧几里德距离,这意味着我必须保存每个实例的所有关键字,然后找到第一个关键字与第二个关键字的差异,然后找到剩余的差异的二审。我想要的是在迭代第一个 map 时,能够从第二个 map 中删除元素。由于我们有两个消息集合,因此会多次调用以下方法,并将第一个消息集合中的每条消息与第二个消息集合中的每条消息进行比较。

我有这段代码,但它突然停止了,尽管我检查了它在某些地方放置的多个 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);
}

最佳答案

代码本身似乎没问题(我认为我之前发现的错误不是一个)。然而, 可能有更简单的方法:

  1. 与其在第二组中查找第一组中的字符串,还可以同时在两个列表中移动并将迭代器推进到较小的元素或两个迭代器(如果它们使用相同的字符串)。每种情况下都直接进行相应的计算。
  2. 我个人会使用两个排序的 std::vector<std::pair<std::string, unsigned int> >为此std::map<std::string, unsigned int>效果也很好。

我无权访问您的 Instance类,因此尚未对其进行测试,但类似下面的内容应该可以工作。

struct compare1st {
    bool operator()(std::pair<std::string, unsigned int> const& p1,
                    std::pair<std::string, unsigned int> const& p2) const {
        return p1.first < p2.first;
    }
};

std::vector<std::pair<std::string, unsigned int> > fill(Instance const& inst) {
    std::vector<std::pair<std::string, unsigned int> > rc;
    for (unsigned int i(0), end(inst.getNumberOfFeatures()); i != end; ++i) {
        rc.push_back(std::make_pair(inst.getFeature(i), i));
    }
    std::sort(rc.begin(), rc.end(), compare1st());
    return rc;
}
double square(double d) { // pow(d, 2.0) is fairly expensive
    return d * d;
}

float KNNClassifier::distance(const Instance& inst1, const Instance& inst2) {   
    typedef std::pair<std::string, unsigned int> Pair;
    std::vector<Pair> feat1 = fill(inst1);
    std::vector<Pair> feat2 = fill(inst2);

    std::vector<Pair>::const_iterator it1(feat1.begin()), end1(feat1.end());
    std::vector<Pair>::const_iterator it2(feat2.begin()), end2(feat2.end());
    double result(0.0);
    while (it1 != end1 && it2 != end2) {
        if (it1 != end1 && (it2 == end2 || it1->first < it2->first)) {
            result += square(inst1.getScore((it1++)->second);
        }
        else if (it2 != end2 && (it1 == end1 || it2->first < it1->first))
            result += square(inst2.getScore((it2++)->second);
        }
        else {
            result += square(inst1.getScore((it1++)->second)
                             -  inst2.getScore((it2++)->second);
        }
    }
    return sqrt(result);
}

关于c++ - 在 C++ 中迭代 map 时发出 SIGSEGV 信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14129737/

相关文章:

c++ - 从类中删除指向 this 的指针?

c++ - 多个 vector 的数据表示

python - 让 max(dict_name.values()) 忽略字符串

c++ - 迭代器边界检查超出 vector 大小

C++ 双端队列 : when iterators are invalidated

java - 迭代器和内存使用

c++ - 在宏中使用 `else`

c++ - 不使用外部库的稀疏 vector 实现建议

python - 将包含列表项的字典展开为字典对列表

java - Hazelcast ConcurrentMap/MultiMap 键