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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T01:27:59+00:00 2026-06-12T01:27:59+00:00

I have a vector of structs and I need help with how to sort

  • 0

I have a vector of structs and I need help with how to sort them according to one of the values, and if those 2 values are the same, then sort it according to another parameter.

This is similar to other questions, but it has more to it.

What I am trying to implement is the scan line based polygon fill algorithm.

I build the active edge list, but then I need to sort it based on the x value in each struct object. If the x values are the same, then they need to be sorted based on the inverse of the slopes for each struct object.

Here is the definition of the struct with the override operator < for normal sorting:

struct Bucket
{
    // Fields of a bucket list
    int ymax, x, dx, dy, sum;

    // Override the < operator, used for sorting based on the x value
    bool operator < (const Bucket& var) const
    {
        // Check if the x values are the same, if so
        // sort based on the ivnerse of the slope (dx/dy)
        /*if(x == var.x)
            return (dx/dy) < (var.dx/var.dy);
        else*/
            return (x < var.x);
    }
};

I commented out the if then else statement because it does compile, but causes a floating point error and the program crashes.
The exact error is: “Floating point exception (core dumped)”

I also tried casting each division to (int) but that did not work either.

My question: Is there a way to do the sort similar to the way I have it, or should I write my own sort method.

If I should make my own sort method, please provide a link or something to a simple method which can help.

Thanks

  • 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-12T01:28:00+00:00Added an answer on June 12, 2026 at 1:28 am

    You should implement double division, because with integers, when you have for example 5/6 it results in 0, and division by 0 is not possible as we know. That’s why the program crashes.
    SO change the members of the structure to doubles.And then you should take care of some precision issues but at least the program won’t crash assuming that you are not allowing 0 value for dy.

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

Sidebar

Related Questions

What I have: Vectors of different custom structs(one custom struct per vector) I used
Sitation: overview: I have something like this: std::vector<SomeType> values; std::vector<int> indexes; struct Range{ int
I have an application that stores a vector of structs. These structs hold information
I have an array of structs that I need to retrive data from. The
I have never used variadic templates myself, but think I could need them now.
I need to access a vector pointer elements, I have the following code for
I have a vector of ordered container classes where I need to know the
I have the following code: #include <iostream> #include <vector> using namespace std; struct A{};
I have vector< pair<int, int>> myVec (N); I want to have all pairs initialized
in clojure i have vector [myfn1 myfn2 myfn3] how can i call functions named

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.