Pagini: [1]   În jos
  Imprimă  
Ajutor Subiect: Overloading operator < on (int, int)  (Citit de 1644 ori)
0 Utilizatori şi 1 Vizitator pe acest subiect.
skipy
Strain
*

Karma: 8
Deconectat Deconectat

Mesaje: 46



Vezi Profilul
« : Martie 02, 2006, 20:13:30 »

Cod:
struct Point 
{  
    int x, y;  
    bool operator < (const Point& o) const
    {    
         if (x < o.x || (x == o.x && y < o.y)) return true;
         else return false;
    }
}
;
 
Cu acea bucata de cod putem sorta un vector de Points. Intrebarea este, cum se poate redefini operatorul < pe int, int ca sa pot sorta un vector de int cum vreau eu (spre exemplu, numerele pare < cele impare).
Memorat

Cheap VR WoW could destroy modern society...
greco
Nu mai tace
*****

Karma: 144
Deconectat Deconectat

Mesaje: 434



Vezi Profilul
« Răspunde #1 : Martie 02, 2006, 21:01:37 »

Ca sa le poti sorta tu nu e nevoie sa definesti operatorul (de altfel ar fi cam ciudat sa redefinesti int, nu crezi ?).

Poti sa faci

Cod:

bool cmp(const int &a, const int &b) {
    return (a%2) < (b%2);
}


si

Cod:

sort(v.begin(), v.end(), cmp);
Memorat

Jump in the cockpit and start up the engines
Remove all the wheelblocks there's no time to waste
Gathering speed as we head down the runway
Gotta get airborne before it's too late.
skipy
Strain
*

Karma: 8
Deconectat Deconectat

Mesaje: 46



Vezi Profilul
« Răspunde #2 : Martie 02, 2006, 22:04:00 »

Mersi, asta cautam.
Memorat

Cheap VR WoW could destroy modern society...
Pagini: [1]   În sus
  Imprimă  
 
Schimbă forumul:  

Powered by SMF 1.1.19 | SMF © 2006-2013, Simple Machines