V0lt
Platinum Member | Редактировать | Профиль | Сообщение | Цитировать | Сообщить модератору
Код: // chapter.h #pragma once #include <cstdint> class Chapter { static int counter; int order; public: int64_t rt; Chapter(int64_t rt); Chapter& operator = (const Chapter& c); static int Compare(const void* a, const void* b); static bool Less(const Chapter& a, const Chapter& b); }; |
Код: // chapter.cpp #include "chapter.h" int Chapter::counter = 0; Chapter::Chapter(int64_t rt) { order = counter++; this->rt = rt; } Chapter& Chapter::operator = (const Chapter& c) { if ( this != &c ) { order = c.counter; rt = c.rt; } return *this; } int Chapter::Compare(const void* a, const void* b) { const Chapter* ca = static_cast<const Chapter*>(a); const Chapter* cb = static_cast<const Chapter*>(b); if (ca->rt > cb->rt) { return 1; } else if (ca->rt < cb->rt) { return -1; } return ca->order - cb->order; } bool Chapter::Less(const Chapter& a, const Chapter& b) { return Compare(&a, &b) < 0; } |
Код: // main.cpp #include <algorithm> #include <vector> #include <stdio.h> #include "chapter.h" void main() { std::vector<Chapter> chapters1 = {1000, 2000, 3000, 4000, 5000}; std::vector<Chapter> chapters2 = {1000, 2000, 3000, 4000, 5000}; qsort(chapters1.data(), chapters1.size(), sizeof(Chapter), Chapter::Compare); std::sort(chapters2.begin(), chapters2.end(), Chapter::Less); printf("\n"); printf("\nPress any key to quit... "); getchar(); } |
| Всего записей: 10455 | Зарегистр. 05-02-2003 | Отправлено: 20:31 12-12-2017 | Исправлено: V0lt, 06:02 13-12-2017 |
|