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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:46:03+00:00 2026-06-15T17:46:03+00:00

I have a vector vec that I need to sort every time I put

  • 0

I have a vector vec that I need to sort every time I put an element inside it

so when I put the first Upgrade* inside the vector I have no problems

but when I put the second Upgrade* inside it and the sort routine is called I have a runtime error

this is how I put elements and call sort every time I insert

std::vector<Upgrade*> stack = getStack();

stack.push_back(element);

std::sort(stack.begin(), stack.end(), CostBenefitUpgradeOrder());

and this is my comparator

struct CostBenefitUpgradeOrder {
    bool operator ()(const Upgrade * u1, const Upgrade * u2) const {

        const UpgradeType upgradeType1 = u1->getUpgradeType();
        const UpgradeType upgradeType2 = u2->getUpgradeType();

        int price1 = PriceUtil::getPrice(upgradeType1);
        int price2 = PriceUtil::getPrice(upgradeType2);

        if (price2 < price1)
            return true;
        else
            return false;
    }
}

and this is the error

runtime error

I have noticed that it only happens when I execute the program in Debug mode!!

  • 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-15T17:46:04+00:00Added an answer on June 15, 2026 at 5:46 pm

    Your comparison function is broken. You cannot have a predicate that returns true for both u1 < u2 and u2 < u1.

    Replace the return statement with return u1 < u2; if you just need something for a quick test.

    Also, are you sure you need to use a vector? Unless you need the pointers to be stored in contiguous memory, you’d be better off using an std::set instead with an appropriate comparator. The set will keep the elements ordered after every insertion / deletion.

    Also, since you’re using raw pointers, if you’re allocating the objects using new make sure you delete before removing elements from the container. Better yet, use an std::set<std::unique_ptr<Upgrade>, CostBenefitUpgradeOrder> instead and not have to worry about deleting the allocated memory.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a vector of objects (objects are term nodes that amongst other fields
I have a vector where I keep an incrementing data. Normally each element of
I have a vector t_vec that stores references to instances of class Too .
I have a vector: std::vector<std::unique_ptr<myClass>> vec; //filling the vector vec.push_back(std::unique_ptr<myClass>(new myClass())); vec.push_back(std::unique_ptr<myClass>(new myClass())); vec.push_back(std::unique_ptr<myClass>(new
I have a class Vector that represents a point in 3-dimensional space. This vector
I have a vector that I am trying to perform a contains function on.
This is related to this question I have a vector of points that will,
I'm using a vector. For my purposes, I generally need it to have (n)
I have a pointer to a vector of object pointers vector<object*>* vec; Which happens
I have a vector of count data that is strongly over dispersed and zero

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.