Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Runtime error from std::sort

DonotaloDonotalo Posts: 715Member
[purple]here is the code fragment:[code]
vector< LogInfo > info;
...
sort(info.begin(), info.end(), sort_by_difference);
...
[/code]
sort_by_difference() is defined as follows:[code]
bool sort_by_difference(const LogInfo& a, const LogInfo& b) {
return [red]![/red](a.difference < b.difference);
}
[/code]
since i need to sort descendingly, so there is a NOT.

i'm getting runtime error from the sort(). if i commented out sort(), then everything is ok. what is wrong?
[/purple]
[hr][purple]~Donotalo()[/purple]

Comments

  • HK_MP5KPDWHK_MP5KPDW Posts: 770Member ✭✭✭
    : [purple]here is the code fragment:[code]
    : vector< LogInfo > info;
    : ...
    : sort(info.begin(), info.end(), sort_by_difference);
    : ...
    : [/code]
    : sort_by_difference() is defined as follows:[code]
    : bool sort_by_difference(const LogInfo& a, const LogInfo& b) {
    : return [red]![/red](a.difference < b.difference);
    : }
    : [/code]
    : since i need to sort descendingly, so there is a NOT.
    :
    : i'm getting runtime error from the sort(). if i commented out sort(), then everything is ok. what is wrong?
    : [/purple]
    : [hr][purple]~Donotalo()[/purple]
    :

    [blue]What does [italic]LogInfo[/italic] look like? Is it the type of [italic]class[/italic] that might need a custom copy constructor or assignment operator to assist in moving data around within the [italic]vector[/italic] whilst sorting?[/blue]
  • DonotaloDonotalo Posts: 715Member
    : : [purple]here is the code fragment:[code]
    : : vector< LogInfo > info;
    : : ...
    : : sort(info.begin(), info.end(), sort_by_difference);
    : : ...
    : : [/code]
    : : sort_by_difference() is defined as follows:[code]
    : : bool sort_by_difference(const LogInfo& a, const LogInfo& b) {
    : : return [red]![/red](a.difference < b.difference);
    : : }
    : : [/code]
    : : since i need to sort descendingly, so there is a NOT.
    : :
    : : i'm getting runtime error from the sort(). if i commented out sort(), then everything is ok. what is wrong?
    : : [/purple]
    : : [hr][purple]~Donotalo()[/purple]
    : :
    :
    : [blue]What does [italic]LogInfo[/italic] look like? Is it the type of [italic]class[/italic] that might need a custom copy constructor or assignment operator to assist in moving data around within the [italic]vector[/italic] whilst sorting?[/blue]
    :
    [purple]i dont think a custom copy constructor is required for LogInfo:[code]
    struct LogInfo {
    bool rank_changed;
    string league, name, since_before;
    int difference, played, points_before, points, rank, last_rank;
    };
    [/code]
    another thing, if i remove the NOT sign from [blue]sort_by_difference()[/blue], then no run time error occurs!
    [/purple]
    [hr][purple]~Donotalo()[/purple]

  • PruyquePruyque Posts: 149Member
    : : : [purple]here is the code fragment:[code]
    : : : vector< LogInfo > info;
    : : : ...
    : : : sort(info.begin(), info.end(), sort_by_difference);
    : : : ...
    : : : [/code]
    : : : sort_by_difference() is defined as follows:[code]
    : : : bool sort_by_difference(const LogInfo& a, const LogInfo& b) {
    : : : return [red]![/red](a.difference < b.difference);
    : : : }
    : : : [/code]
    : : : since i need to sort descendingly, so there is a NOT.
    : : :
    : : : i'm getting runtime error from the sort(). if i commented out sort(), then everything is ok. what is wrong?
    : : : [/purple]
    : : : [hr][purple]~Donotalo()[/purple]
    : : :
    : :
    : : [blue]What does [italic]LogInfo[/italic] look like? Is it the type of [italic]class[/italic] that might need a custom copy constructor or assignment operator to assist in moving data around within the [italic]vector[/italic] whilst sorting?[/blue]
    : :
    : [purple]i dont think a custom copy constructor is required for LogInfo:[code]
    : struct LogInfo {
    : bool rank_changed;
    : string league, name, since_before;
    : int difference, played, points_before, points, rank, last_rank;
    : };
    : [/code]
    : another thing, if i remove the NOT sign from [blue]sort_by_difference()[/blue], then no run time error occurs!
    : [/purple]
    : [hr][purple]~Donotalo()[/purple]
    :
    :


    MSDN (http://msdn2.microsoft.com/en-us/library/wh15hsex.aspx) states that the ordering must be strict and weak.

    The 'strict' part means that if you compare an object to itself it must return false. For your compare-function this will return true, that might be the problem (if you remove the NOT sign you will have a strict ordering, so it makes sense). Why not just replace the '<' with a '>'?
  • DonotaloDonotalo Posts: 715Member
    : : : : [purple]here is the code fragment:[code]
    : : : : vector< LogInfo > info;
    : : : : ...
    : : : : sort(info.begin(), info.end(), sort_by_difference);
    : : : : ...
    : : : : [/code]
    : : : : sort_by_difference() is defined as follows:[code]
    : : : : bool sort_by_difference(const LogInfo& a, const LogInfo& b) {
    : : : : return [red]![/red](a.difference < b.difference);
    : : : : }
    : : : : [/code]
    : : : : since i need to sort descendingly, so there is a NOT.
    : : : :
    : : : : i'm getting runtime error from the sort(). if i commented out sort(), then everything is ok. what is wrong?
    : : : : [/purple]
    : : : : [hr][purple]~Donotalo()[/purple]
    : : : :
    : : :
    : : : [blue]What does [italic]LogInfo[/italic] look like? Is it the type of [italic]class[/italic] that might need a custom copy constructor or assignment operator to assist in moving data around within the [italic]vector[/italic] whilst sorting?[/blue]
    : : :
    : : [purple]i dont think a custom copy constructor is required for LogInfo:[code]
    : : struct LogInfo {
    : : bool rank_changed;
    : : string league, name, since_before;
    : : int difference, played, points_before, points, rank, last_rank;
    : : };
    : : [/code]
    : : another thing, if i remove the NOT sign from [blue]sort_by_difference()[/blue], then no run time error occurs!
    : : [/purple]
    : : [hr][purple]~Donotalo()[/purple]
    : :
    : :
    :
    :
    : MSDN (http://msdn2.microsoft.com/en-us/library/wh15hsex.aspx) states that the ordering must be strict and weak.
    :
    : The 'strict' part means that if you compare an object to itself it must return false. For your compare-function this will return true, that might be the problem (if you remove the NOT sign you will have a strict ordering, so it makes sense). Why not just replace the '<' with a '>'?
    :
    [purple]
    yeah, that solves the problem! thanks!

    i use the NOT instead of opposite sign just because of curiosity.
    [/purple]
    [hr][purple]~Donotalo()[/purple]

Sign In or Register to comment.