infoarena

infoarena - concursuri, probleme, evaluator, articole => Informatica => Subiect creat de: Giurgea Mihnea din Martie 02, 2006, 20:13:30



Titlul: Overloading operator < on (int, int)
Scris de: Giurgea Mihnea din 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).


Titlul: Overloading operator < on (int, int)
Scris de: Tiberiu-Lucian Florea din 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);


Titlul: Overloading operator < on (int, int)
Scris de: Giurgea Mihnea din Martie 02, 2006, 22:04:00
Mersi, asta cautam.