template<class typ_danych>
struct wiekszy
{
bool operator()(const typ_danych &dane1, const typ_danych &dane2)
{
return dane1 > dane2;
}
};
template<class typ_danych>
struct zamien
{
private:
typ_danych daneTmp;
public:
void operator()(typ_danych &dane1, typ_danych &dane2)
{
daneTmp = dane1;
dane1 = dane2;
dane2 = daneTmp;
}
};
template<class typ_danych, class komparator = wiekszy<typ_danych>, class zamieniacz = zamien<typ_danych> >
class sortownik
{
public:
static void sortuj(int ile, typ_danych * czego)
{
komparator kom;
zamieniacz zam;
for(int i=1; i < ile ;i++)
for(int j=ile-1 ; j >= i ; j--)
if(kom(czego[j-1], czego[j])) zam(czego[j-1], czego[j]);
}
};
const int ile = 5;
const char * napisy[ile] = { "ccc", "aaa", "eee", "bbb", "ddd" };
int liczby[ile] = { 2, 5, 3, 1, 4 };
char napis[ile+1] = "caebd";
struct wiekszyCStr
{
bool operator()(const char * const &dane1, const char * const &dane2)
{
return strcmp(dane1, dane2) > 0;
}
};
int main(int argc, char* argv[])
{
sortownik<const char*, wiekszyCStr>::sortuj(ile, napisy);
for(int i = 0; i < ile; i++) cout << napisy[i] << endl;
cout << endl;
sortownik<int>::sortuj(ile, liczby);
for(int i = 0; i < ile; i++) cout << liczby[i];
cout << endl;
sortownik<char>::sortuj(ile, napis);
cout << napis << endl;
cin.get();
return 0;
}