Hì hì, nếu ai từng học Pascal có lẽ cũng biết là mãng Pascal ta có thể khai báo ví dụ như A[-1..100].., nhưng trong C++ thì ta chỉ có thể dùng mãng có index từ 0->. Nhưng với quá tải toán tử thì cái gì cũng có thể xảy ra. Khi ta có thể xài index âm thì sẽ rất tiện trong việc cài đặt các giải thuật như lính canh hay QHD, bài này mình viết để có thể ứng dụng trong các thuật toán, vì mình từng đọc các giải thuật pascal đôi khi lời giải hiểu nhưng không cách gì trình bày qua C++ được. Mơ ước lâu lắm rùi bây h mới viết xong đó hì hì. Các bạn có thể cài đặt thử rùi nếu có bug gì thì xin chỉ cho mình với nhé, để mình hoàn thiện thêm, thanks T_T.
All feedbacks and comments are appreciated.![]()
Dùng mãng này có thể xài với char, double, anytype of data, và có safe checking tránh những truy xuất out of range.
Mãng 1 chiều
C++ Code:
#include <iostream> #include <cmath> class _Err{ public: void report() }; template <class T> class arrD{ private : int start, end; T *iter; public: arrD(int s, int e):start(s), end(e){ iter = new T[abs(end-start) + 1]; } arrD(int size):start(0), end(size){ iter = new T[size]; } ~arrD(){delete[] iter;} const arrD &operator = (const arrD &os); T& operator[](int offset); const T& operator[](int offset) const; int arrend() const { return end; } int arrstart() const { return start; } int size() const { return (end - start);} }; template <class T> T& arrD<T>::operator[](int offset){ if(offset < start || offset >= end){ throw _Err(); } else{ int pos = 0; for(int x = start; x < end; ++x){ ++pos; if(offset == x) offset = pos; } } return iter[offset]; } template <class T> const T& arrD<T>::operator[](int offset) const{ if(offset < start || offset >= end){ throw _Err(); } else{ unsigned pos = 0; for(int x = start; x < end; ++x){ ++pos; if(offset == x) offset = pos; } } return iter[offset]; } template <class T> const arrD<T>& arrD<T>::operator = (const arrD<T> &os) { if(&os != this){ if((end - start) != (os.end - os.start)){ delete []iter; end = os.end; start = os.start; iter = new T[abs(end-start) + 1]; } for(int i = start; i < end; ++i) iter[i] = os.iter[i]; } return *this; } int main() { arrD<char> A(-3,3); try{ for(int i = -3; i < 3; ++i){ A[i] = 'a'; } for(int i = -3; i < 3; ++i) } catch(_Err e){ e.report(); } return 0; }
Phiên bản vector :
C++ Code:
// a.cpp : Defines the entry point for the console application. // #include <iostream> #include <cmath> #include <vector> class _Err{ public: void report(){ } }; template <class T> class arrD{ private : int start, end; std::vector<T> iter; public: arrD(int s, int e):start(s), end(e), iter(e - s + 1) {} T& operator[](int offset); const T& operator[](int offset) const; int arrend() const { return end; } int arrstart() const { return start; } int size() const { return (end - start);} }; template <class T> T& arrD<T>::operator[](int offset) { if(offset < start || offset >= end){ throw _Err(); } else{ int pos = 0; for(int x = start; x < end; ++x){ ++pos; if(offset == x) offset = pos; } } return iter[offset]; } template <class T> const T& arrD<T>::operator[](int offset) const { if(offset < start || offset >= end){ throw _Err(); } else { unsigned pos = 0; for(int x = start; x < end; ++x){ ++pos; if(offset == x) offset = pos; } } return iter[offset]; } int main() { arrD<char> A(-3, 3); for(int x = -3; x < 3; ++x){ A[x] = 'c'; } for(int x = -3; x < 3; ++x){ } }
Đã được chỉnh sửa lần cuối bởi rox_rook : 02-03-2008 lúc 03:03 AM.
Mãng 2 chiều
C++ Code:
#include <iostream> #include <vector> #include <cmath> #include <string> class _Err{ void report(){ } }; template <class T> class arr2D{ int sRow, eRow, sCol, eCol; std::vector<T> data; public: arr2D(int sRow, int eRow, int sCol, int eCol) :sRow(sRow), eRow(eRow), sCol(sCol), eCol(eCol), data(abs((eRow - sRow + 1)*(eCol - sCol + 1))) {} int size() const{ return abs((eRow - sRow + 1)*(eCol - sCol + 1)); } T &element(int x, int y){ return data[x*abs(eCol - sCol) + y]; } class Row{ arr2D &im; int row; public : Row(arr2D &im, int row):im(im), row(row){} T &operator[](int offset){ if(offset < im.sCol || offset >= im.eCol){ throw _Err(); } else{ unsigned pos = 0; for(int x = im.sCol; x < im.eCol; ++x){ ++pos; if(offset == x) offset = pos; } return im.element(row, offset); } } }; Row operator[](int offset){ if(offset < sRow || offset >= eRow){ throw _Err(); } else{ unsigned pos = 0; for(int x = sRow; x < eRow; ++x){ ++pos; if(offset == x) offset = pos; } } return Row(*this, offset); } }; int main() { arr2D<std::string> M(-3, 3, -3, 4); for(int x = -3; x < 3; x++){ for(int y = -3; y < 4; y++){ M[x][y] = "IloveYou"; } } for(int x = -3; x < 3; x++){ for(int y = -3; y < 4; y++){ } } return 0; }
Đã được chỉnh sửa lần cuối bởi rox_rook : 02-03-2008 lúc 03:04 AM.