Advertisement
Guest User

Untitled

a guest
Jan 8th, 2015
452
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.59 KB | None | 0 0
  1. #include<ostream>
  2. #include<iostream>
  3.  
  4. class TwoDBoolArray
  5. {
  6.   public:
  7.   // Baue leeres Array
  8.   TwoDBoolArray() : n(0), m(0)
  9.   {
  10.     data = NULL;
  11.   }
  12.  
  13.   // Baue ein Array mit n Zeilen und m Spalten
  14.   TwoDBoolArray(int _n, int _m) : n(_n), m(_m)
  15.   {
  16.     data = new bool[n*m];
  17.   }
  18.  
  19.   // Kopiere ein Array: mach tiefe Kopie
  20.   TwoDBoolArray(const TwoDBoolArray& other) : n(other.n), m(other.m)
  21.   {
  22.     data = new bool[n*m];
  23.     for (int i=0; i<n*m; ++i)
  24.       data[i] = other.data[i];
  25.   }
  26.  
  27.   // gebe Speicher beim Zerstören frei
  28.   ~TwoDBoolArray()
  29.   {
  30.     if (data)
  31.       delete[] data;
  32.   }
  33.  
  34.   // Zuweisungsoperator
  35.   TwoDBoolArray& operator=(const TwoDBoolArray& other)
  36.   {
  37.     // Vermeide Selbstzuweisung
  38.     if (this != &other)
  39.     {
  40.       // Vermeide Allokationen, wenn die Größe bereits stimmt
  41.       if (other.n * other.m != n*m)
  42.       {
  43.         if (data)
  44.           delete[] data;
  45.         data = new bool[other.n*other.m];
  46.       }
  47.       n = other.n;
  48.       m = other.m;
  49.    
  50.       // Kopiere die Daten
  51.       for (int i=0; i<n*m; ++i)
  52.         data[i] = other.data[i];
  53.     }
  54.     return *this;
  55.   }
  56. private:
  57.   // Ein temporäres Objekt, auf dem man nur den operator[] aufrufen kann
  58.   class RowProxy
  59.   {
  60.     public:
  61.     // Merke alle Daten der äußeren Klasse, die wir für den Aufruf benötigen
  62.     RowProxy(bool* _data, int _m, int _i) : data(_data), m(_m), i(_i) {}
  63.  
  64.     // Rechne korrekten Index im Datenarray aus und gebe Referenz zurück
  65.     bool& operator[](int j)
  66.     {
  67.       return data[i*m+j];
  68.     }
  69.  
  70.     private:
  71.     bool* data;
  72.     int m;
  73.     int i;
  74.   };
  75. public:
  76.   // Der äußere operator[] gibt ein Proxyobjekt zurück
  77.   RowProxy operator[](int i)
  78.   {
  79.     return RowProxy(data,m,i);
  80.   }
  81.  
  82.   // Gebe Zeilenzahl zurück
  83.   int rows()
  84.   {
  85.     return n;
  86.   }
  87.  
  88.   // Gebe Spaltenzahl zurück
  89.   int cols()
  90.   {
  91.     return m;
  92.   }
  93.  
  94.   private:
  95.   int n,m;
  96.   bool* data;
  97. };
  98.  
  99. // Der Output Filestream
  100. std::ostream& operator<<(std::ostream& stream, TwoDBoolArray& array)
  101. {
  102.   stream << array.rows() << "x" << array.cols() << " array:" << std::endl;
  103.   for (int i=0; i<array.rows(); ++i)
  104.   {
  105.     for (int j=0; j<array.cols(); ++j)
  106.       stream << (array[i][j] ? "O" : " ");
  107.     stream << std::endl;
  108.   }
  109.   return stream;  
  110. }
  111.  
  112. // Der Input Filestream
  113. std::istream& operator>>(std::istream& stream, TwoDBoolArray& array)
  114. {
  115.   int n,m;
  116.   stream >> n;
  117.   stream >> m;
  118.  
  119.   TwoDBoolArray tmp(n,m);
  120.   for (int i=0; i<n; ++i)
  121.     for (int j=0; j<m; ++j)
  122.       stream >> tmp[i][j];
  123.  
  124.   array = tmp;
  125.   return stream;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement