Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // Created by trykr on 22.02.2020.
- //
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
- using namespace std;
- class Car
- {
- private:
- char Id[10];
- public:
- Car(const char id);
- bool lessId(const Car&) const;
- const char* id() const;
- Car(const char* id)
- {
- strncpy(Id, id, 10);
- }
- bool lessId(const Car &other)
- {
- return strcmp(Id, other.Id) < 0;
- }
- const char* id() const {
- return Id;
- }
- int Cmp(const void* c1_ptr, const void* c2_ptr)
- {
- Car **c1 = reinterpret_cast<Car**>(c1);
- Car **c2 = reinterpret_cast<Car**>(c2);
- return
- (*c1)->lessId(**c2) ? -1:
- (*c2)->lessId(**c2) ? 1 :
- 0;
- }
- };
- int main()
- {
- Car c1("1");
- Car c2("2");
- Car c3("3");
- Car* collection[3] = {&c1, &c2, &c3};
- qsort(collection, 3, sizeof(Car*), Car::Cmp(&с1, &c2));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement