經典資料結構之一維矩陣的基本操作
阿新 • • 發佈:2019-02-20
矩陣是用來進行大型資料處理的常用資料表達形式。最基本的矩陣自然是一維矩陣,而從概念上,陣列可以看作一個一維矩陣。另一方面,陣列的陣列即是二維矩陣。其實,通過下標對映,一維矩陣可以表達所有維度的矩陣。下面先由一維矩陣說起。
.h檔案
#ifndef ARRAY1D_HHH #define ARRAY1D_HHH #include <stdlib.h> #include <iostream> #include <cassert> template<typename T> class CArray1D{ private: // members; int m_nSize; T* m_pElem; public: // methods; // constructors; CArray1D(): m_nSize(10) { m_pElem = new T[10]; } CArray1D(int size); CArray1D(const CArray1D<T>& v); ~CArray1D() {delete [] m_pElem;}; // [] operator; T& operator[](int i) const { assert(i >=0 && i < m_nSize); return m_pElem[i]; } // = operator; CArray1D<T>& operator=(const CArray1D<T>& v); // + operator; CArray1D<T> operator+() const; CArray1D<T> operator+(const CArray1D<T>& v); // - operator; CArray1D<T> operator-() const; CArray1D<T> operator-(const CArray1D<T>& v); // +=operator; CArray1D<T>& operator+=(const CArray1D<T>& v); // * operator; CArray1D<T> operator*(const CArray1D<T>& v); // size; int mSize() const; void print(std::ostream& out) const; void mResize(int size){ m_nSize = size; delete [] m_pElem; m_pElem = new T[m_nSize]; } }; template<typename T> CArray1D<T>::CArray1D(int size): m_nSize(size){ m_pElem = new T[m_nSize]; } template<typename T> CArray1D<T>::CArray1D(const CArray1D<T>& v){ m_nSize = v.m_nSize; m_pElem = new T[m_nSize]; for(int i = 0; i < m_nSize; i ++){ m_pElem[i] = v.m_pElem[i]; } } template<typename T> int CArray1D<T>::mSize() const{ return this->m_nSize; } template<typename T> CArray1D<T>& CArray1D<T>::operator=(const CArray1D& v){ if(this != &v){ m_nSize = v.m_nSize; delete [] m_pElem; m_pElem = new T[m_nSize]; for(int i = 0; i < m_nSize; i++){ m_pElem[i] = v.m_pElem[i]; } } return *this; } template<typename T> CArray1D<T> CArray1D<T>::operator+() const{ CArray1D<T> tmp(m_nSize); for(int i = 0; i < m_nSize; i ++) tmp.m_pElem[i] = abs(m_pElem[i]); return tmp; } template<typename T> CArray1D<T> CArray1D<T>::operator-() const{ CArray1D<T> tmp(m_nSize); for(int i = 0; i < m_nSize; i ++) tmp.m_pElem[i] = -(m_pElem[i]); return tmp; } template<typename T> CArray1D<T> CArray1D<T>::operator+(const CArray1D& v){ assert(m_nSize == v.m_nSize); CArray1D<T> tmp(m_nSize); for(int i = 0; i < m_nSize; i ++) tmp.m_pElem[i] = v.m_pElem[i] + m_pElem[i]; return tmp; } template<typename T> CArray1D<T> CArray1D<T>::operator-(const CArray1D& v){ assert(m_nSize == v.m_nSize); CArray1D<T> tmp(m_nSize); for(int i = 0; i < m_nSize; i ++) tmp.m_pElem[i] =m_pElem[i] - v.m_pElem[i]; return tmp; } template<typename T> CArray1D<T> CArray1D<T>::operator*(const CArray1D& v){ assert(v.m_nSize == m_nSize); CArray1D<T> tmp(m_nSize); for(int i = 0; i < m_nSize; i ++) tmp.m_pElem[i] = m_pElem[i] * v.m_pElem[i]; return tmp; } template<typename T> CArray1D<T>& CArray1D<T>::operator+=(const CArray1D& v){ assert(v.m_nSize == m_nSize); for(int i = 0; i < m_nSize; i ++) m_pElem[i] += v.m_pElem[i]; return *this; } template<typename T> void CArray1D<T>::print(std::ostream& out) const{ for(int j = 1; j <= m_nSize; j ++){ out << m_pElem[j - 1] << ' '; if( 0 == j % 5) out << std::endl; } out << std::endl; } #endif
main檔案
#include <stdlib.h> #include <iostream> #include "Array1D.h" using namespace std; #define ARRAY_SIZE 10 int main(){ CArray1D<int> tmpArray1(ARRAY_SIZE); // input array1 for(int i = 0; i < ARRAY_SIZE; i ++) tmpArray1[i] = -i; cout << "output array1" << endl; tmpArray1.print(cout); // input array2 CArray1D<int> tmpArray2(ARRAY_SIZE); for(int i = 0; i < ARRAY_SIZE; i++) tmpArray2[i] = i * 2; cout << "output array2" << endl; tmpArray2.print(cout); CArray1D<int> tmpArray3(ARRAY_SIZE); tmpArray3 = (-tmpArray1); cout << "output -array1" << endl; tmpArray3.print(cout); tmpArray3 = (+tmpArray1); cout << "output +array1" << endl; tmpArray3.print(cout); tmpArray3 = tmpArray1 * tmpArray2; cout << "output array1 * array2" << endl; tmpArray3.print(cout); tmpArray3 = tmpArray1 + tmpArray2; cout << "output array1 + array2" << endl; tmpArray3.print(cout); tmpArray3 = tmpArray1 - tmpArray2; cout << "output array1 - array2" << endl; tmpArray3.print(cout); system("pause"); return 0; }
測試輸出:
0 2 4 6 8
10 12 14 16 18
output -array1
0 1 2 3 4
5 6 7 8 9
output +array1
0 1 2 3 4
5 6 7 8 9
output array1 * array2
0 -2 -8 -18 -32
-50 -72 -98 -128 -162
output array1 + array2
0 1 2 3 4
5 6 7 8 9
output array1 - array2
0 -3 -6 -9 -12
-15 -18 -21 -24 -27