Runtime error from std::sort

[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

  • : [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]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]

  • : : : [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]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.

Howdy, Stranger!

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

Categories