std::sort Comparator falsch?



  • Hallo,

    ich habe ein struct und einen Comparator der in std::sort aufgerufen wird:

    struct RHO_IDX
    {
    	int		idx;
    	double	rho;
    };
    
    struct RHO_Comparator
    {
    	bool operator()(const RHO_IDX& a, const RHO_IDX& b)
    	{	
    		if( a.rho <= b.rho ) return true;
    		return false;
    	}
    };
    

    im Programm habe ich sowas:

    RHO_IDX* a = new RHO_IDX[len];
    //fülle a mit RHO_IDX werten.
    
    std::sort(a, a + len; RHO_Comparator());
    

    bei mir verursacht das so den folgenden error:

    *** glibc detected *** ./sp: double free or corruption (!prev): 0x08073120 ***
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6[0xb6c797cd]
    /lib/tls/i686/cmov/libc.so.6(cfree+0x90)[0xb6c7ce30]
    /usr/lib/libstdc++.so.6(_ZdlPv+0x21)[0xb6e3ad11]
    /usr/lib/libstdc++.so.6(_ZdaPv+0x1d)[0xb6e3ad6d]
    ./sp[0x804cd8e]
    ./sp[0x804d14b]
    ./sp[0x8049923]
    /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xdc)[0xb6c27ebc]
    ./sp(__gxx_personality_v0+0x95)[0x80494f1]
    //Dann folgt die memory map
    

    kann mir jemand helfen? Ist der Comparator richtig? Ich wollte alle array werte nach aufsteigendem rho sortieren....


  • Mod

    testo schrieb:

    Ist der Comparator richtig?

    nein. <= ist keine Ordnungsrelation.

    struct RHO_Comparator
    {
        bool operator()(const RHO_IDX& a, const RHO_IDX& b)
        {   
            return a.rho < b.rho;
        }
    };
    


  • camper schrieb:

    testo schrieb:

    Ist der Comparator richtig?

    nein. <= ist keine Ordnungsrelation.

    na aber hallo ist das ne ordnungsrelation. Sie ist nur nicht strikt.


  • Mod

    Jester schrieb:

    na aber hallo ist das ne ordnungsrelation. Sie ist nur nicht strikt.

    wollte nur wissen, wie lange es dauert 😉


Anmelden zum Antworten