<vector> template实现

来源:互联网 发布:基因比对软件 编辑:程序博客网 时间:2024/05/29 16:28
#include <iostream>/********************************* vector *******************************************/template <class T>class vector{public:typedef T* iterator;typedef const T* const_iterator;vector(int size = 0, const T& value = T());vector(const vector<T>& obj);vector(const_iterator first, const_iterator last);~vector() {if(start) delete [] start;}vector& operator= (const vector<T>& rhs);T& front();const T& front() const;T& back();const T& back() const;void push_back(const T& item);void pop_back();int size() const {return sz;}void resize(int new_size, const T& x = T());int capacity()const {return maxsz;}bool empty() const {return sz == 0;}iterator begin() {return start;}const_iterator begin() const {return start;}iterator end() {return start+sz;}const_iterator end() const {return start+sz;}T& operator[] (int n) {return start[n];}const T& operator[] (int n) const {return start[n];}void print_vector(std::ostream& out)const;void print_reverse(std::ostream& out)const;private:iterator start;int sz;int maxsz;void reserve(int n, bool copy);};template <class T>void vector<T>::reserve(int n, bool copy){T *tmp = new T[n];if(0 == tmp){std::cout << "no_mem()" << std::endl; //throw no_mem();return ;}for(int i = 0; i < n; ++i) tmp[i] = T();if(sz > n) sz = n;if(copy){for(int i = 0; i < sz; ++i)tmp[i] = start[i];}if(start) delete [] start;maxsz = n;start = tmp;}template <class T>vector<T>::vector(int size, const T& value):start(0), sz(0), maxsz(0){if (0 == size) return ;reserve(size, false);for (int i = 0; i < size; ++i) start[i] = value;sz = size;}template <class T>vector<T>::vector(const vector<T>& obj):start(0), sz(0), maxsz(0){if ( obj.sz == 0) return ;reserve(obj.sz, false);for (int i = 0; i < obj.sz; ++i) start[i] = obj.start[i];sz = obj.sz;}template <class T>vector<T>::vector(const_iterator first, const_iterator last):start(0), sz(0), maxsz(0){sz = last - first;reserve(sz, false);for (int i = 0; i < sz; ++i) start[i] = first[i];}template <class T>vector<T>& vector<T>::operator= (const vector<T>& rhs){if (&rhs == this) return *this;if (maxsz < rhs.sz) reserve(rhs.sz, false);for (int i = 0; i < rhs.sz; ++i) start[i] = rhs.start[i];sz = rhs.sz;return *this;}template <class T>T& vector<T>::front(){if(sz == 0) {std::cout << "out_bounds()" <<std::endl;exit(1);//throw out_bounds();}return start[0];}template <class T>const T& vector<T>::front() const{if(sz == 0) {std::cout << "out_bounds()" <<std::endl;exit(1);//throw out_bounds();}return start[0];}template <class T>T& vector<T>::back(){if(sz == 0) {std::cout << "out_bounds()" <<std::endl;exit(1);//throw out_bounds();}return start[sz-1];}template <class T>const T& vector<T>::back() const{if(sz == 0) {std::cout << "out_bounds()" <<std::endl;exit(1);//throw out_bounds();}return start[sz-1];}template <class T>void vector<T>::resize(int new_size, const T& x){reserve(new_size, true);for (int i = sz; i < new_size; ++i) start[i] = x;sz = new_size;}template <class T>void vector<T>::push_back(const T& item){if (sz == maxsz){if(maxsz == 0) reserve(1, false);else reserve(2*maxsz, true);}start[sz] = item;++sz;}template <class T>void vector<T>::pop_back(){if (sz == 0) {std::cout << "out_bounds()" <<std::endl;exit(1);//throw out_bounds();}--sz;}template <class T>void vector<T>::print_vector(std::ostream& out)const{const_iterator iter;for (iter = begin(); iter != end(); ++iter) out << *iter << " ";out << std::endl;}template <class T>void vector<T>::print_reverse(std::ostream& out)const{const_iterator iter;for (iter = end() - 1; iter >= begin(); --iter)out << *iter << " ";out << std::endl;}template <class T>std::ostream& operator<< (std::ostream& out, const vector<T>& x){x.print_vector(out);return out;}/********************************* end vector *******************************************//********************************* matrix *******************************************/template <class T>class matrix{public:matrix(int rows = 1, int cols = 1, const T& value = T());vector<T>& operator[] (int i);const vector<T>& operator[] (int i) const;void resize(int rows, int cols);int cols() const {return col;}int rows() const {return row;}void print_matrix(std::ostream& out) const;private:int row, col;vector< vector<T> > mat;};template <class T>matrix<T>::matrix(int rows, int cols, const T& value):row(rows), col(cols), mat(rows, vector<T>(cols, value)) {}template <class T>vector<T>& matrix<T>::operator[](int i){if (i < 0 || i >= row){std::cout << "out_bounds()" <<std::endl;exit(1);}return mat[i];}template <class T>const vector<T>& matrix<T>::operator[](int i) const{if (i < 0 || i >= row){std::cout << "out_bounds()" <<std::endl;exit(1);}return mat[i];}template <class T>void matrix<T>::resize(int rows, int cols){if(row == rows || col == cols) return ;row = rows;col = cols;mat.resize(row);for (int i = 0; i < row; ++i){mat[i].resize(col);}}template <class T>void matrix<T>::print_matrix(std::ostream& out) const {for(int i = 0; i < row; ++i){mat[i].print_vector(out);}std::cout << std::endl;}template <class T>std::ostream& operator<< (std::ostream& out, const matrix<T>& rhs){rhs.print_matrix(out);return out;}/********************************* end matrix *******************************************/int main(void){matrix<int> mat(5, 8, 5);//mat[0][0] = 2;//std::cout << mat[1][0] << std::endl;mat.resize(3, 4);std::cout << mat.cols() << " " << mat.rows() << std::endl;mat.print_matrix(std::cout);mat.resize(11, 22);std::cout << mat << std::endl;system("pause");return 0;}


 

原创粉丝点击