fork(1) download
  1. #include <iostream>
  2. #include <cassert>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. template<typename T>
  8. class Array;
  9.  
  10. template<typename T>
  11. ostream & operator <<(ostream &, const Array<T> &);
  12.  
  13. template<typename T>
  14. istream & operator >>(istream &, Array<T> &);
  15.  
  16. template <class T> class Array {
  17. friend ostream & operator << <>(ostream &, const Array<T> &);
  18. friend istream & operator >> <>(istream &, Array<T> &);
  19. public:
  20. Array(int = 10);
  21. ~Array();
  22. private:
  23. T *ptr; //указатель на 1й элемент
  24. int size; //размер массива
  25. // static
  26. };
  27.  
  28. template <class T> Array<T>::Array(int arraySize) {
  29. size = arraySize;
  30. ptr = new T[size];
  31. assert(ptr != 0); //завершить, если память не выделена
  32. }
  33.  
  34. template <class T> Array<T>::~Array() {
  35. delete [] ptr;
  36. }
  37.  
  38. template <class T> istream& operator>> (istream& in, Array<T>& x) {
  39. for (int i = 0; i < x.size; ++i) {
  40. in >> x.ptr[i];
  41. }
  42. return in;
  43. }
  44.  
  45. template <class T> ostream& operator << (ostream & out, const Array<T> &x) {
  46. for (int i = 0; i < x.size; ++i) {
  47. out << x.ptr[i] << " ";
  48.  
  49. }
  50. return out;
  51. }
  52.  
  53. int main() {
  54. Array<int> a(5);
  55. cin >> a;
  56. cout << a;
  57.  
  58. return 0;
  59. }
  60.  
Success #stdin #stdout 0s 3432KB
stdin
1 2 3 4 5
stdout
1 2 3 4 5