Note that there are some explanatory texts on larger screens.

plurals
  1. POstd::sort going overboard
    primarykey
    data
    text
    <p>I'm trying to sort a vector of objects using a predicate function and I'm getting some segfaults...</p> <p>I have a class <code>Item</code> and a list of Items in a <code>vector&lt; Item &gt; _items</code>. I needed to sort it according to a display order (numerical member of the class) and I just called a simple sort with a predicate function.</p> <pre><code>sort(_items.begin(), _items.end(), sort_item_by_display_order); </code></pre> <p>where the predicate function is </p> <pre><code>bool sort_item_by_display_order (Item i, Item j) { return i.GetDisplayOrder()&gt;j.GetDisplayOrder(); } </code></pre> <p>and GetDisplayOrder is</p> <pre><code>int Item::GetDisplayOrder() { return display_order; } </code></pre> <p>but... I got some segfaults while doing this. I then added a counter to the predicate function to check how many times it was called and I discovered that when this crashed the counter was bigger then the size of the vector.</p> <p>After some reading I changed the code to use iterators instead of using the .begin() and .end() (Shouldn't this be the same?!)</p> <p>So what I have now is</p> <pre><code>vector&lt;Item&gt;::iterator it_start, it_end; it_start = _items.begin(); it_end = _items.end(); sort(it_start, it_end, sort_item_by_display_order); </code></pre> <p>with the same predicate function.</p> <p>And now it doesn't crash, but... for most of the sorting I do I get more iterations then the size of the vector I am sorting (which is probably normal)</p> <p>So... What is the difference between calling sort with <code>_items.begin()</code> or <code>_it_start</code>. From what I can tell they are the same right?!</p> <p>One more note. <code>Item</code> is a simple base class declared as</p> <pre><code>class Item { private: (...) public: (...) } </code></pre> <p>As reference I used <a href="http://www.cplusplus.com/reference/algorithm/sort/" rel="nofollow">http://www.cplusplus.com/reference/algorithm/sort/</a> and <a href="http://www.codeguru.com/forum/showthread.php?t=366064" rel="nofollow">http://www.codeguru.com/forum/showthread.php?t=366064</a>.</p> <p>In the second link they add a const and &amp; to the predicate function arguments which would make my function something like this</p> <pre><code>bool sort_item_by_display_order (const Item&amp; i, const Item&amp; j) { return i.GetDisplayOrder()&gt;j.GetDisplayOrder(); } </code></pre> <p>but I get a compiler error:</p> <pre><code>Item.cpp|1485|error: passing `const Item' as `this' argument of `int Item::GetDisplayOrder()' discards qualifiers| </code></pre> <p>arghhh... The question is... What am I doing wrong?</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.
 

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