Guest User

Untitled

a guest
Apr 27th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main(){
  4. int rows;
  5. int cols;
  6. int * arr;
  7. arr = new int[rows][cols];
  8. }
  9.  
  10. int rows = ..., cols = ...;
  11. int** matrix = new int*[rows];
  12. for (int i = 0; i < rows; ++i)
  13. matrix[i] = new int[cols];
  14.  
  15. for (int i = 0; i < rows; ++i)
  16. delete [] matrix[i];
  17. delete [] matrix;
  18.  
  19. int rows = ..., cols = ...;
  20. int** matrix = new int*[rows];
  21. if (rows)
  22. {
  23. matrix[0] = new int[rows * cols];
  24. for (int i = 1; i < rows; ++i)
  25. matrix[i] = matrix[0] + i * cols;
  26. }
  27.  
  28. if (rows) delete [] matrix[0];
  29. delete [] matrix;
  30.  
  31. std::vector< std::vector<int> > a;
  32.  
  33. //m * n is the size of the matrix
  34.  
  35. int m = 2, n = 4;
  36. //Grow rows by m
  37. a.resize(m);
  38. for(int i = 0 ; i < m ; ++i)
  39. {
  40. //Grow Columns by n
  41. a[i].resize(n);
  42. }
  43. //Now you have matrix m*n with default values
  44.  
  45. //you can use the Matrix, now
  46. a[1][0]=1;
  47. a[1][1]=2;
  48. a[1][2]=3;
  49. a[1][3]=4;
  50.  
  51. //OR
  52. for(i = 0 ; i < m ; ++i)
  53. {
  54. for(int j = 0 ; j < n ; ++j)
  55. { //modify matrix
  56. int x = a[i][j];
  57. }
  58.  
  59. }
  60.  
  61. #include <boost/multi_array.hpp>
  62.  
  63. int main(){
  64. int rows;
  65. int cols;
  66. boost::multi_array<int, 2> arr(boost::extents[rows][cols] ;
  67. }
  68.  
  69. #include <iostream>
  70.  
  71. int main(){
  72. int rows=4;
  73. int cols=4;
  74. int **arr;
  75.  
  76. arr = new int*[rows];
  77. for(int i=0;i<rows;i++){
  78. arr[i]=new int[cols];
  79. }
  80. // statements
  81.  
  82. for(int i=0;i<rows;i++){
  83. delete []arr[i];
  84. }
  85. delete []arr;
  86. return 0;
  87. }
  88.  
  89. arr = new int[cols*rows];
  90.  
  91. arr[row * cols + col] = Aij;
  92.  
  93. for(int i=0;i < rows*cols;++i)
  94. matrix[i]=someOtherMatrix[i];
  95.  
  96. for(int r=0;i < rows;++r)
  97. for(int c=0;i < cols;++s)
  98. matrix[r][c]=someOtherMatrix[r][c];
  99.  
  100. /**
  101. * Represents a grid of values.
  102. * Indices are zero-based.
  103. */
  104. template<class T>
  105. class GenericGrid
  106. {
  107. public:
  108. GenericGrid(size_t numRows, size_t numColumns);
  109.  
  110. GenericGrid(size_t numRows, size_t numColumns, const T & inInitialValue);
  111.  
  112. const T & get(size_t row, size_t col) const;
  113.  
  114. T & get(size_t row, size_t col);
  115.  
  116. void set(size_t row, size_t col, const T & inT);
  117.  
  118. size_t numRows() const;
  119.  
  120. size_t numColumns() const;
  121.  
  122. private:
  123. size_t mNumRows;
  124. size_t mNumColumns;
  125. std::vector<T> mData;
  126. };
  127.  
  128.  
  129. template<class T>
  130. GenericGrid<T>::GenericGrid(size_t numRows, size_t numColumns):
  131. mNumRows(numRows),
  132. mNumColumns(numColumns)
  133. {
  134. mData.resize(numRows*numColumns);
  135. }
  136.  
  137.  
  138. template<class T>
  139. GenericGrid<T>::GenericGrid(size_t numRows, size_t numColumns, const T & inInitialValue):
  140. mNumRows(numRows),
  141. mNumColumns(numColumns)
  142. {
  143. mData.resize(numRows*numColumns, inInitialValue);
  144. }
  145.  
  146.  
  147. template<class T>
  148. const T & GenericGrid<T>::get(size_t rowIdx, size_t colIdx) const
  149. {
  150. return mData[rowIdx*mNumColumns + colIdx];
  151. }
  152.  
  153.  
  154. template<class T>
  155. T & GenericGrid<T>::get(size_t rowIdx, size_t colIdx)
  156. {
  157. return mData[rowIdx*mNumColumns + colIdx];
  158. }
  159.  
  160.  
  161. template<class T>
  162. void GenericGrid<T>::set(size_t rowIdx, size_t colIdx, const T & inT)
  163. {
  164. mData[rowIdx*mNumColumns + colIdx] = inT;
  165. }
  166.  
  167.  
  168. template<class T>
  169. size_t GenericGrid<T>::numRows() const
  170. {
  171. return mNumRows;
  172. }
  173.  
  174.  
  175. template<class T>
  176. size_t GenericGrid<T>::numColumns() const
  177. {
  178. return mNumColumns;
  179. }
  180.  
  181. ///
  182. /// Matrix Allocator Utility
  183. /// @param pppArray Pointer to the double-pointer where the matrix should be allocated.
  184. /// @param iRows Number of rows.
  185. /// @param iColumns Number of columns.
  186. /// @return Successful allocation returns true, else false.
  187. template <typename T>
  188. bool NewMatrix(T*** pppArray,
  189. size_t iRows,
  190. size_t iColumns)
  191. {
  192. bool l_bResult = false;
  193. if (pppArray != 0) // Test if pointer holds a valid address.
  194. { // I prefer using the shorter 0 in stead of NULL.
  195. if (!((*pppArray) != 0)) // Test if the first element is currently unassigned.
  196. { // The "double-not" evaluates a little quicker in general.
  197. // Allocate and assign pointer array.
  198. (*pppArray) = new T* [iRows];
  199. if ((*pppArray) != 0) // Test if pointer-array allocation was successful.
  200. {
  201. // Allocate and assign common data storage array.
  202. (*pppArray)[0] = new T [iRows * iColumns];
  203. if ((*pppArray)[0] != 0) // Test if data array allocation was successful.
  204. {
  205. // Using pointer arithmetic requires the least overhead. There is no
  206. // expensive repeated multiplication involved and very little additional
  207. // memory is used for temporary variables.
  208. T** l_ppRow = (*pppArray);
  209. T* l_pRowFirstElement = l_ppRow[0];
  210. for (size_t l_iRow = 1; l_iRow < iRows; l_iRow++)
  211. {
  212. l_ppRow++;
  213. l_pRowFirstElement += iColumns;
  214. l_ppRow[0] = l_pRowFirstElement;
  215. }
  216. l_bResult = true;
  217. }
  218. }
  219. }
  220. }
  221. }
  222.  
  223. ///
  224. /// Matrix De-Allocator Utility
  225. /// @param pppArray Pointer to the double-pointer where the matrix should be de-allocated.
  226. /// @return Successful de-allocation returns true, else false.
  227. template <typename T>
  228. bool DeleteMatrix(T*** pppArray)
  229. {
  230. bool l_bResult = false;
  231. if (pppArray != 0) // Test if pointer holds a valid address.
  232. {
  233. if ((*pppArray) != 0) // Test if pointer array was assigned.
  234. {
  235. if ((*pppArray)[0] != 0) // Test if data array was assigned.
  236. {
  237. // De-allocate common storage array.
  238. delete [] (*pppArray)[0];
  239. }
  240. }
  241. // De-allocate pointer array.
  242. delete [] (*pppArray);
  243. (*pppArray) = 0;
  244. l_bResult = true;
  245. }
  246. }
  247. }
  248.  
  249. .
  250. .
  251. .
  252. double l_ppMatrix = 0;
  253. NewMatrix(&l_ppMatrix, 3, 3); // Create a 3 x 3 Matrix and store it in l_ppMatrix.
  254. .
  255. .
  256. .
  257. DeleteMatrix(&l_ppMatrix);
  258.  
  259. const int nRows = 20;
  260. const int nCols = 10;
  261. int (*name)[nCols] = new int[nRows][nCols];
  262. std::memset(name, 0, sizeof(int) * nRows * nCols); //row major contiguous memory
  263. name[0][0] = 1; //first element
  264. name[nRows-1][nCols-1] = 1; //last element
  265. delete[] name;
Add Comment
Please, Sign In to add comment