Note that there are some explanatory texts on larger screens.

plurals
  1. POSort a vector of structs
    primarykey
    data
    text
    <p>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.</p> <p>This is similar to other questions, but it has more to it.</p> <p>What I am trying to implement is the scan line based polygon fill algorithm.</p> <p>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.</p> <p>Here is the definition of the struct with the override operator &lt; for normal sorting:</p> <pre><code>struct Bucket { // Fields of a bucket list int ymax, x, dx, dy, sum; // Override the &lt; operator, used for sorting based on the x value bool operator &lt; (const Bucket&amp; 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) &lt; (var.dx/var.dy); else*/ return (x &lt; var.x); } }; </code></pre> <p>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)"</p> <p>I also tried casting each division to (int) but that did not work either.</p> <p>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.</p> <p>If I should make my own sort method, please provide a link or something to a simple method which can help.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload