fork download
  1. #include <vector>
  2. #include <algorithm>
  3. #include <type_traits>
  4.  
  5. template<typename T> class Row;
  6. template<typename T> class ConstRow;
  7.  
  8. template<typename T>
  9. class Matrix2D
  10. {
  11. // Matrix aus bool nicht erlauben
  12. static_assert( !std::is_same<bool,T>::value, "T must not be bool because of std::vector<bool> anomaly" );
  13.  
  14. public:
  15. using value_type = T;
  16. using reference = T&;
  17. using const_reference = T const&;
  18. using pointer = T*;
  19. using const_pointer = T const*;
  20. using size_type = std::size_t;
  21. using difference_type = std::ptrdiff_t;
  22. using row_type = Row<T>;
  23. using const_row_type = ConstRow<T>;
  24.  
  25. private:
  26. std::vector<T> Elements_;
  27. size_type Rows_ = 0;
  28. size_type Cols_ = 0;
  29.  
  30. public:
  31. Matrix2D() = default;
  32. Matrix2D( size_type rows, size_type cols ) :
  33. Matrix2D( rows, cols, value_type() )
  34. {
  35. }
  36.  
  37. Matrix2D(size_type rows, size_type cols, const_reference v ) :
  38. Elements_( rows * cols, v ),
  39. Rows_( rows ),
  40. Cols_( cols )
  41. {
  42. }
  43.  
  44. row_type operator[]( size_type index )
  45. {
  46. pointer beg = Elements_.data() + index * Cols_;
  47. return row_type( beg, beg + Cols_ );
  48. }
  49.  
  50. const_row_type operator[]( size_type index ) const
  51. {
  52. const_pointer beg = Elements_.data() + index * Cols_;
  53. return const_row_type( beg, beg + Cols_ );
  54. }
  55. };
  56.  
  57. template<typename T>
  58. class Row
  59. {
  60. T* Begin_= nullptr;
  61. T* End_ = nullptr;
  62.  
  63. public:
  64. Row( T* beg, T* end ) :
  65. Begin_( beg ),
  66. End_( end )
  67. {
  68. }
  69.  
  70. bool empty() const
  71. {
  72. return Begin_ == End_;
  73. }
  74.  
  75. std::size_t size() const
  76. {
  77. return std::distance( begin(), end() );
  78. }
  79.  
  80. T& operator[]( std::size_t index )
  81. {
  82. return Begin_[index];
  83. }
  84.  
  85. T const& operator[]( std::size_t index ) const
  86. {
  87. return Begin_[index];
  88. }
  89.  
  90. T* begin()
  91. {
  92. return Begin_;
  93. }
  94.  
  95. T* end()
  96. {
  97. return End_;
  98. }
  99.  
  100. T const* begin() const
  101. {
  102. return Begin_;
  103. }
  104.  
  105. T const* end() const
  106. {
  107. return End_;
  108. }
  109. };
  110.  
  111. template<typename T>
  112. class ConstRow
  113. {
  114. T const* Begin_ = nullptr;
  115. T const* End_ = nullptr;
  116.  
  117. public:
  118. ConstRow( T const* beg, T const* end ) :
  119. Begin_( beg ),
  120. End_( end )
  121. {
  122. }
  123.  
  124. ConstRow( Row<T> const& r ) :
  125. Begin_( r.begin() ),
  126. End_( r.end() )
  127. {
  128. }
  129.  
  130. T const& operator[]( std::size_t index ) const
  131. {
  132. return Begin_[index];
  133. }
  134. };
  135.  
  136. int main()
  137. {
  138. Matrix2D<int> m( 2, 2,-1 );
  139. m[0][0] = 1;
  140. }
Success #stdin #stdout 0s 5500KB
stdin
Standard input is empty
stdout
Standard output is empty