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 8204647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T07:57:44+00:00 2026-06-07T07:57:44+00:00

I been sitting over this problem quiet a few hours now and can not

  • 0

I been sitting over this problem quiet a few hours now and can not find a solution. I try to get rid of some Custom Array classes and want to change them to std::vector.

I have a class member declared like this:

std::vector<BaseCluster *> baseClusters;

In the class constructor I do not initialize the vector to a certain size. I leave it as is, uninitialized. Before, I tried to initialize if to a certain size (to gain some performance and prevent many reallocations), but that gave me problems accessing the elements in a loop (eg. printing them out). I guess it is due to the initialization of the objects with the default constructor (of type BaseCluster) as NULL-Objects ??

However, in a loop I fill the vector with objects like this

baseClusters.push_back(new BaseCluster(current, score));

After the loop finishes I do some cleanup of objects that are not interesting:

for (unsigned int i = 0; i < baseClusters.size(); i++) {
    // --- testing   
    if (baseClusters.at(i) == NULL) {
       wcout << i << ". object is NULL" << endl;
    }
    if (
      ((BaseCluster *) baseClusters.at(i))->getNode()->getSuffixedDocumentsCount() < minimalGroupSize
    ) {
        baseClusters.erase(baseClusters.begin() + i);
    }

    if (i >= noMoreBaseClustersThan) {
        baseClusters.erase(baseClusters.begin() + i);
    }
}

Now I sort the vector (should be sorted by score descending) and here I have a problem:

1.) using the sort as

sort(baseClusters.begin(), baseClusters.end()); 

doesn’t sort at all, also my implemented “operators<” or “operator>” in the BaseCluster class are not touched at all. The operators are public and look like this.

bool operator< (const BaseCluster * rhs) const {
    return m_score < rhs->m_score;
}

bool operator> (const BaseCluster * rhs) const {
    return m_score > rhs->m_score;
}

2.) Using a predicate / functor

class BaseClusterComparator {
public:
    bool operator() (const BaseCluster * a, const BaseCluster * b) const {
        wcout << "BaseClusterComparator" << endl;
        wcout << a->getScore() << " <> " << b->getScore() << endl;
        if (a->getScore() > b->getScore()) {
            return -1;
        } else if (a->getScore() < b->getScore()) {
            return 1;
        } else {
            return 0;
        }
    }
};

the class member getScore looks like this

float
BaseCluster::getScore() const {
    return m_score;
}

and executing the sort with

sort(baseClusters.begin(), baseClusters.end()), BaseClusterComparator());

will result in an segmentation fault when trying to access the getScore() method after sorting about 60 – 70 objects.

Program received signal SIGSEGV, Segmentation fault.
0x00000000004773ec in BaseCluster::getScore (this=0x2ef1) at algorithm/BaseCluster.cpp:44
44      return m_score;

I have the feeling that due to reserving / resizing of the vector there are NULL-objects (constructed with the default constructor of BaseClass ?!).

And now am stuck on how to solve the problem. What am I doing wrong here? I been reading through many other related stackoverflow questions before asking here, followed them by changing here and there but no way out of the seg. faults. As far as I am aware I do not insert any Null-Objects (tested that in the loop), so there must be another flaw in my code. Thought it was an quiet easy task changing to a vector of pointers, but seems more complicated now…

Please, anybody help me on this issue. Thanks so much in advance!

  • 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-07T07:57:45+00:00Added an answer on June 7, 2026 at 7:57 am

    Your comparator is wrong:

    std::sort is not like qsort in the comparator. You just have to return the same as the less than operator. For example

    class BaseClusterComparator {
    public:
        bool operator() (const BaseCluster * a, const BaseCluster * b) const {
            return a->getScore() < b->getScore();
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been pulling my hear out over this problem for a few hours yesterday:
I've been driving myself crazy over the past few days over this one. We've
I've been sitting on this idea for quite a long time and would like
This is weird, but I've been using this method all over my code: I
I have been going crazy over a problem with my tab control. It has
I've been pulling my hair out a bit over this. I'm creating a very
I have been ripping my hair out over this issue, I want a simple
I have been struggling with Isotope for a while now. After a few different
I've been struggling with this problem just like everyone else and I'm quite sure
This question has been asked before, but not with this exact issue. I have

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.