fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. class someType {
  8. public:
  9. someType() = default;
  10. ~someType() = default;
  11. virtual void print(ostream&) {}
  12. friend ostream& operator << (ostream &os, someType *&value);
  13. virtual double getData() {};
  14. };
  15.  
  16. ostream& operator << (ostream &os, someType *&value){
  17. value->print(os);
  18. return os;
  19. }
  20.  
  21. class doubleType : public someType {
  22. private:
  23. double data;
  24. public:
  25. doubleType(double data) : someType() {
  26. this->data = data;
  27. }
  28. void print(ostream& os){
  29. os << data;
  30. }
  31. double getData(){
  32. return data;
  33. }
  34. };
  35.  
  36.  
  37. class intType : public someType {
  38. private:
  39. int data;
  40. public:
  41. intType(int data) : someType() {
  42. this->data = data;
  43. }
  44. void print(ostream& os){
  45. os << data;
  46. }
  47. double getData(){
  48. return data;
  49. }
  50. };
  51.  
  52. class charType : public someType {
  53. private:
  54. char data;
  55. public:
  56. charType(char data) : someType() {
  57. this->data = data;
  58. }
  59. void print(ostream& os){
  60. os << data;
  61. }
  62. double getData(){
  63. return data;
  64. }
  65. };
  66.  
  67.  
  68.  
  69. int main(){
  70. vector<someType*> el;
  71. el.push_back(new charType('z'));
  72. el.push_back(new intType(10));
  73. el.push_back(new doubleType(10.01));
  74. el.push_back(new charType('c'));
  75.  
  76.  
  77. for_each(el.begin(), el.end(), [](someType* value) -> void {
  78. cout << value << endl;
  79. });
  80.  
  81. sort(el.begin(), el.end(), [] (someType* first, someType* second) -> bool {
  82. return first->getData() < second->getData();
  83. });
  84. cout << endl;
  85. for_each(el.begin(), el.end(), [](someType* value) -> void {
  86. cout << value << endl;
  87. });
  88. }
  89.  
Success #stdin #stdout 0s 4440KB
stdin
Standard input is empty
stdout
z
10
10.01
c

10
10.01
c
z