fork download
  1. int MyMinFunc(int a, int b)
  2. {
  3. return a < b ? a : b;
  4. }
  5. int MyMaxFunc(int a, int b)
  6. {
  7. return a > b ? a : b;
  8. }
  9.  
  10. template<typename T>
  11. class RefList
  12. {
  13. struct Node
  14. {
  15. T &t;
  16. Node *n;
  17. Node() = delete;
  18. Node(T &t) : t(t), n(0) {}
  19. Node(const Node &) = delete;
  20. Node &operator=(const Node &) = delete;
  21. ~Node(){}
  22. } *h;
  23. public:
  24. RefList() : h(0) {}
  25. RefList(const RefList &) = delete;
  26. RefList &operator=(const RefList &) = delete;
  27. void AddLast(T &t)
  28. {
  29. if(!h) h = new Node(t);
  30. else
  31. {
  32. Node *n = h;
  33. while(n->n) n = n->n;
  34. n->n = new Node(t);
  35. }
  36. }
  37. T &operator[](unsigned i)
  38. {
  39. if(!i) return h->t;
  40. Node *n = h;
  41. while(i--) n = n->n;
  42. return n->t;
  43. }
  44. ~RefList()
  45. {
  46. while(h)
  47. {
  48. Node *n = h;
  49. h = h->n;
  50. delete n;
  51. }
  52. }
  53. };
  54.  
  55. #include <iostream>
  56.  
  57. int main()
  58. {
  59. RefList<int (int, int)> MyArray;
  60. MyArray.AddLast(MyMinFunc);
  61. MyArray.AddLast(MyMaxFunc);
  62. std::cout << MyArray[0](4, 7) << std::endl;
  63. std::cout << MyArray[1](4, 7) << std::endl;
  64. }
Success #stdin #stdout 0s 3016KB
stdin
Standard input is empty
stdout
4
7