Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.93 KB | None | 0 0
  1. //
  2. // Created by trykr on 22.02.2020.
  3. //
  4.  
  5. #include <iostream>
  6. #include <cstdlib>
  7. #include <cstring>
  8. using namespace std;
  9. class Car
  10. {
  11. private:
  12. char Id[10];
  13. public:
  14. Car(const char id);
  15. bool lessId(const Car&) const;
  16. const char* id() const;
  17. Car(const char* id)
  18. {
  19. strncpy(Id, id, 10);
  20. }
  21.  
  22. bool lessId(const Car &other)
  23. {
  24. return strcmp(Id, other.Id) < 0;
  25. }
  26. const char* id() const {
  27. return Id;
  28. }
  29. int Cmp(const void* c1_ptr, const void* c2_ptr)
  30. {
  31. Car **c1 = reinterpret_cast<Car**>(c1);
  32. Car **c2 = reinterpret_cast<Car**>(c2);
  33.  
  34. return
  35. (*c1)->lessId(**c2) ? -1:
  36. (*c2)->lessId(**c2) ? 1 :
  37. 0;
  38. }
  39. };
  40. int main()
  41. {
  42. Car c1("1");
  43. Car c2("2");
  44. Car c3("3");
  45. Car* collection[3] = {&c1, &c2, &c3};
  46. qsort(collection, 3, sizeof(Car*), Car::Cmp(&с1, &c2));
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement