fork download
  1. #include <iostream>
  2. #include <memory>
  3.  
  4. template <typename T>
  5. struct Array
  6. {
  7. T& operator[](size_t i) const
  8. {
  9. auto current_node = node;
  10. for (size_t j(0); j != i; ++j)
  11. current_node = current_node->next;
  12. return current_node->value;
  13. }
  14. Array<T>& operator<<(const T& n)
  15. {
  16. if (node) {
  17. auto current_node = node;
  18. while(current_node->next)
  19. current_node = current_node->next;
  20. current_node->next = std::make_shared<Node>(n);
  21. }
  22. else
  23. node = std::make_shared<Node>(n);
  24. return *this;
  25. }
  26. friend std::ostream& operator <<(std::ostream &stream, const Array<T>& array)
  27. {
  28. auto current_node = array.node;
  29. do {
  30. stream << current_node->value << ", ";
  31. current_node = current_node->next;
  32. } while(current_node->next);
  33. stream << current_node->value << std::endl;
  34. return stream;
  35. }
  36. private:
  37. struct Node
  38. {
  39. Node(const T& n) : value(n) {}
  40. T value;
  41. std::shared_ptr<Node> next;
  42. };
  43. std::shared_ptr<Node> node;
  44. };
  45.  
  46. int main() {
  47. Array<int> array;
  48.  
  49. array << 1;
  50. array << 4;
  51. array << 8 << 8;
  52.  
  53. std::cout << array;
  54.  
  55. return 0;
  56. }
  57.  
Success #stdin #stdout 0s 4384KB
stdin
Standard input is empty
stdout
1, 4, 8, 8