matrix(c++实现,初版)

王朝c/c++·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

namespace mylib {

template<typename Ty,

const size_t line_sz,

const size_t row_sz>

class matrix {

public:

//typedef

typedef Ty value_type;

typedef size_t size_type;

typedef ptrdiff_t diffecence_type;

typedef Ty* pointer;

typedef const Ty* const_pointer;

typedef Ty& reference;

typedef const Ty& const_reference;

typedef Ty(*cmat_ptr)[row_sz];

typedef const Ty(*const_cmat_ptr)[row_sz];

//constructors

matrix(void)

: mat_ptr(NULL)

{

get_memory();

for(size_type i = 0; i < line_sz; ++i)

for(size_type j = 0; j < row_sz; ++j)

mat_ptr[i][j] = value_type();

}

matrix(const_cmat_ptr cmat)

: mat_ptr(NULL)

{

get_memory();

for(size_type i = 0; i < line_sz; ++i)

for(size_type j = 0; j < row_sz; ++j)

mat_ptr[i][j] = cmat[i][j];

}

matrix(const matrix<Ty,line_sz,row_sz>& mat)

: mat_ptr(NULL)

{

get_memory();

for(size_type i = 0; i < line_sz; ++i)

for(size_type j = 0; j < row_sz; ++j)

mat_ptr[i][j] = mat[i][j];

}

//destructors

~matrix(void)

{

destroy_memory();

}

//public member methods

const size_type line_size(void) const

{

return line_sz;

}

const size_type row_size(void) const

{

return row_sz;

}

const size_type size(void) const

{

return size_type(line_sz * row_sz);

}

//operator member

matrix<Ty,line_sz,row_sz>&

operator= (const matrix<Ty,line_sz,row_sz>& mat)

{

if(this == &mat)

return *this;

if(mat_ptr != BULL)

destroy_memory();

get_memory();

for(size_type i = 0; i < line_sz; ++i)

for(size_type j = 0; j < row_sz; ++j)

mat_ptr[i][j] = mat[i][j];

}

matrix<Ty,line_sz,row_sz>&

operator= (const_cmat_ptr cmat)

{

if(this == cmat)

return *this;

if(mat_ptr != BUL L)

destroy_memory();

get_memory();

for(size_type i = 0; i < line_sz; ++i)

for(size_type j = 0; j < row_sz; ++j)

mat_ptr[i][j] = cmat[i][j];

}

pointer operator[] (const size_type lidx)

{

if(lidx >= line_sz)

{

std::cerr << "lidx >= line_sz" << std::endl;

std::exit(EXIT_FAILURE);

}

return mat_ptr[lidx];

}

const_pointer operator[] (const size_type lidx) const

{

if(lidx >= line_sz)

{

std::cerr << "lidx >= line_sz" << std::endl;

std::exit(EXIT_FAILURE);

}

return mat_ptr[lidx];

}

operator const_cmat_ptr() const

{

return mat_ptr;

}

private:

//private methods

void get_memory(void)

{

mat_ptr = new value_type[line_sz][row_sz];

}

void destroy_memory(void)

{

delete [] mat_ptr;

}

//date member

cmat_ptr mat_ptr;

};

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航