fork(1) download
  1. #include <vector>
  2.  
  3. template <typename T>
  4. class matrix
  5. {
  6. std::size_t mRowdim, mColdim;
  7. std::vector<T> mValues;
  8.  
  9. public:
  10. matrix(std::size_t rowdim, std::size_t coldim)
  11. : mRowdim(rowdim), mColdim(coldim), mValues(rowdim*coldim) {} // rowdim x coldim matrix
  12.  
  13. matrix(const matrix& other)
  14. : mRowdim(other.mRowdim), mColdim(other.mColdim), mValues(other.mValues) {} // copy ctor
  15.  
  16. matrix(matrix&& other)
  17. : mRowdim(other.mRowdim), mColdim(other.mColdim), mValues(std::move(other.mValues)) {} // move ctor
  18.  
  19. matrix &operator= (const matrix &other) // copy operator
  20. {
  21. matrix other_cpy(other);
  22. other_cpy.swap(*this);
  23. return *this;
  24. }
  25.  
  26. matrix &operator= (matrix &&other) // move operator
  27. {
  28. other.swap(*this);
  29. return *this;
  30. }
  31.  
  32. void swap(matrix &other) // swaperator
  33. {
  34. std::swap(mRowdim, other.mRowdim);
  35. std::swap(mColdim, other.mColdim);
  36. mValues.swap(other.mValues);
  37. }
  38.  
  39. T &operator() (std::size_t row, std::size_t col)
  40. {
  41. return mValues[mColdim*row + col];
  42. }
  43.  
  44. const T &operator() (std::size_t row, std::size_t col) const
  45. {
  46. // ... range check ...
  47. return mValues[mColdim*row + col];
  48. }
  49. };
  50.  
  51. int main()
  52. {
  53. matrix<double> my_matrix(10,10);
  54.  
  55. return 0;
  56. }
Success #stdin #stdout 0s 3012KB
stdin
Standard input is empty
stdout
Standard output is empty