Advertisement
Guest User

Untitled

a guest
May 23rd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. #include <iostream>
  2. #include "Matrix.h"
  3.  
  4. int main() {
  5. const int m = 3, n = 4;
  6. Matrix<int, m, n> a;
  7. int k = 1;
  8. std::cout << "Matrix:" << std::endl;
  9. for (int i = 0; i < m; i++) {
  10. for (int j = 0; j < n; j++) {
  11. a[i][j] = i * j * (k *= -1);
  12. std::cout << a[i][j] << ' ';
  13. }
  14. std::cout << std::endl;
  15. }
  16. std::cout << std::endl << "Positive elements of matrix:";
  17. for (int elem : a) {
  18. std::cout << ' ' << elem;
  19. }
  20. std::cout << std::endl << "Positive elements of matrix (reversed order):";
  21. for (auto elem = a.end()--; elem != a.begin(); elem--) {
  22. std::cout << ' ' << *elem;
  23. }
  24. std::cout << std::endl;
  25. return 0;
  26. }
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43. #include <iterator>
  44. #include <vector>
  45.  
  46. template <typename T, int m, int n>
  47. class Matrix {
  48. private:
  49. std::vector<T> matrix;
  50. public:
  51. Matrix(): matrix(m * n) {
  52. }
  53. class Row {
  54. private:
  55. std::vector<T>& matrix;
  56. int rowIndex;
  57. public:
  58. Row(std::vector<T>& matrix, int rowIndex): matrix(matrix), rowIndex(rowIndex) {
  59. }
  60. T& operator [] (int columnIndex) {
  61. return matrix[rowIndex * n + columnIndex];
  62. }
  63. };
  64. Matrix::Row operator [] (int rowIndex) {
  65. return Row(matrix, rowIndex);
  66. }
  67. class PositiveIterator: public std::iterator<std::forward_iterator_tag, int, ptrdiff_t, T*, T> {
  68. private:
  69. bool isDefault = true;
  70. std::vector<T>& matrix;
  71. int index;
  72. void findNext() {
  73. do {
  74. index++;
  75. } while (index < matrix.size() && matrix[index] <= 0);
  76. isDefault = index == matrix.size();
  77. }
  78. void findPrevious() {
  79. do {
  80. index--;
  81. } while (index >= 0 && matrix[index] <= 0);
  82. isDefault = index == -1;
  83. }
  84. public:
  85. PositiveIterator(std::vector<T>& matrix, bool isDefault): isDefault(false), matrix(matrix) {
  86. index = isDefault ? m * n : -1;
  87. }
  88. bool operator == (PositiveIterator other) {
  89. return (isDefault && other.isDefault) || (index == other.index);
  90. }
  91. bool operator != (PositiveIterator other) {
  92. return !(*this == other);
  93. }
  94. T& operator * () {
  95. if (isDefault) {
  96. throw "not initialized iterator";
  97. }
  98. switch (index) {
  99. case -1:
  100. findNext();
  101. break;
  102. case m * n:
  103. findPrevious();
  104. break;
  105. }
  106. return matrix[index];
  107. }
  108. PositiveIterator& operator ++ () {
  109. if (isDefault) {
  110. throw "not initialized iterator";
  111. }
  112. findNext();
  113. return *this;
  114. }
  115. PositiveIterator& operator ++ (int) {
  116. PositiveIterator& tmp = *this;
  117. operator++();
  118. return tmp;
  119. }
  120. PositiveIterator& operator -- () {
  121. if (isDefault) {
  122. throw "not initialized iterator";
  123. }
  124. findPrevious();
  125. return *this;
  126. }
  127. PositiveIterator& operator -- (int) {
  128. PositiveIterator& tmp = *this;
  129. operator--();
  130. return tmp;
  131. }
  132. };
  133. PositiveIterator begin() {
  134. return PositiveIterator(matrix, false);
  135. }
  136. PositiveIterator end() {
  137. return PositiveIterator(matrix, true);
  138. }
  139. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement