⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 matrix.h

📁 数据结构学习的小例子
💻 H
字号:
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
#include <exception>
#include <cstdlib>
namespace mylib {
	template<typename Ty>
	class matrix {	
	private:
		//proxy class vec:
        template<typename type = Ty>
        class vec {
        public:
            vec() : pvec(NULL),vecsize(0){}
            vec(const vec<type>& v)
            {
                init(v);
            }    
            void init(size_t sz = 0,
                      const type& value = type())
            {
                vecsize = sz;
                get_memory();
                for(size_t i = 0; i < vecsize; ++i)
                    pvec[i] = value;
            }
            void init(const vec<type>& v) 
            {
                vecsize = v.getsize();
                get_memory();
                for(size_t i = 0; i < vecsize; ++i)
                    pvec[i] = v[i];
            }
            void init(const type* array, size_t sz)             
            {
                vecsize = sz;
                get_memory();
                for(size_t i = 0; i < vecsize; ++i)
                    pvec[i] = array[i];
            }
            ~vec()
            {
                delete[] pvec;
            }
            vec<type>& operator=(const vec<type>& v)
            {
                if(this == &v)
                   return *this;
                if(v.getsize() != getsize())
                   vecsize = v.getsize();
                if(pvec != NULL)
                   delete[] pvec;
                get_memory();
                for(size_t i = 0; i < vecsize; ++i)
                    pvec[i] = v[i];
                return *this;                
            }
            size_t getsize(void) const
            { return vecsize;}
            type& operator[](size_t index)
            {
                try {
                    if(index >= vecsize)
                      throw std::out_of_range("index >= row_sz");
                }
                catch(std::out_of_range& e)
                {
                    std::cout << e.what() << std::endl;
                    std::exit(EXIT_FAILURE);
                }
                return pvec[index];    
            } 
            const type& operator[](size_t index) const 
            {
                try {
                    if(index >= vecsize)
                      throw std::out_of_range("index >= row_sz");
                }
                catch(std::out_of_range& e)
                {
                    std::cout << e.what() << std::endl;
                    std::exit(1);
                } 
                return pvec[index];   
            }                         
        private: 
            void get_memory(void)
            {
                 if(vecsize > 0)
                   pvec = new type[vecsize];                                
            }    
            type *pvec;
            size_t vecsize;
        }; 
    public:   		
		//constructors
		matrix(const size_t lsz = 0,
               const size_t rsz = 0,
               const Ty value = Ty())
		: vectors(NULL),line_sz(lsz), row_sz(rsz)
		{
			get_memory();
			for(size_t i = 0; i < line_sz; ++i)
				vectors[i].init(row_sz,value);
		}		
		matrix(const matrix<Ty>& cmat)
		: vectors(NULL),
		  line_sz(cmat.line_size()), 
		  row_sz(cmat.row_size())
		{
			get_memory();
			for(size_t i = 0; i < line_sz; ++i)
			 	vectors[i].init(cmat[i]);
		}
        
		//destructors
		~matrix(void)
		{
			destroy_memory();
		}
		
		//public member methods
		const size_t line_size(void) const
		{
			return line_sz;
		}
		const size_t row_size(void) const
		{
			return row_sz;
		}
		const size_t size(void) const
		{
			return size_t(line_sz * row_sz);
		}
        
		//operator member
		matrix<Ty>& operator= (const matrix<Ty> cmat)
		{
			if(this == &cmat)
				return *this;
			if((cmat.row_size()!=row_sz) 
                || (cmat.line_size()!=line_sz))
            {
                row_sz = cmat.row_size();
                line_sz = cmat.line_size();
            }    
			if(vectors != NULL)
				destroy_memory();
			get_memory();
			for(size_t i =  0; i < line_sz; ++i)
				vectors[i].init(cmat[i]);
		}
		vec<Ty>& operator[] (size_t index)
		{
		    try {
               if(index >= line_sz)
               throw std::out_of_range("indwx >= line_sz");
            }
            catch(std::out_of_range& e)
            {
               std::cout << e.what() << std::endl;
               std::exit(EXIT_FAILURE);
            }
			return vectors[index];
		}
		const vec<Ty>& operator[] (size_t index) const
		{
			try {
               if(index >= line_sz)
                 throw std::out_of_range("index >= line_sz");
            }
            catch(std::out_of_range& e)
            {
               std::cout << e.what() << std::endl;
               std::exit(EXIT_FAILURE);
            }		
			return vectors[index];
		}
			
	private:
		//private methods
		void get_memory(void)
		{
            if(line_sz > 0)
              vectors = new vec<Ty>[line_sz];
		}
		void destroy_memory(void)
		{
			delete[] vectors;
		}

		//date member
		vec<Ty> * vectors;
		size_t line_sz;
  		size_t row_sz;
	};
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -