Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9079775
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:54:57+00:00 2026-06-16T19:54:57+00:00

I am trying to fix this for about 5 days and no luck, every

  • 0

I am trying to fix this for about 5 days and no luck, every solution I tried failed.

I found some cause of the SIGSEGV below but nothing helped
What is SIGSEGV run time error in C++?

Ok, here is the code.
I have 2 instances, which contain some keywords-features and their scores

I want to get their eucleidian distance, which means I have to save all the keywords for each of the instances, then find the diffs for the keywords of the first one with those of the second and then find the diffs for the remaining of the second instance. What I want is while iterating the first map, to be able to delete elements from the second. The following method is called multiple times as we have two message collections, and every message from the first one is compared with every message from the second.

I have this code but it suddenly stops although I checked it is working for some seconds with multiple cout I put in some places

Note that this is for a university task so I cannot use boost and all those tricks. But I would like to know the way to bypass the problem I am into.

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);    
}

and I also tried this idea in order to not have to delete something but it suddenly stops too.

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T19:54:58+00:00Added an answer on June 16, 2026 at 7:54 pm

    The code per se seems to be OK (the error I thought I spotted earlier wasn’t one). However,
    there may be an easier approach:

    1. Instead of looking up the string from the first set in the second set, it would be possible to move through the two lists simultanously and advance the iterator to the smaller element or both iterators if they use the same string. The corresponding computation is done directly in each case.
    2. I would personally use two sorted std::vector<std::pair<std::string, unsigned int> > for this but std::map<std::string, unsigned int> works as well.

    I don’t have access to your Instance class and, thus, haven’t tested it but something like the below should work.

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So, I have been trying to fix this for about two months. It all
I really need some help with this as I have been trying to fix
I found this script on about.com which I'm trying to learn from on how
I spent about 4 hours yesterday trying to fix this issue in my code.
I've been trying to fix this bug of mines for about 3 hours now,
I've been trying to fix this error for about an hour now and I've
I've been struggling for weeks trying to fix this problem, but I haven't found
I've been trying to fix this problems for a few hours already, I cannot
I've been trying to fix this error: {exception:Persistent class \Class org.myapp.model.Account does not seem
I'm actually trying to fix this issue where my projects are always out of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.