matrix(c++实现,初版)
来源:互联网 发布:mac系统 编辑:程序博客网 时间:2024/04/27 18:24
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;
};
}
- matrix(c++实现,初版)
- Android ClickableRoundedBackground Span实现(初版)
- Linux 图片传输功能c/c++(初版)
- 经典算法研究:模式匹配(子串匹配)之 BM 算法(C语言实现版)初版
- Cocos2d-x TitleMap A*算法实现。(初版)
- TensorFlow DNN 实现MNIST数字识别(初版)
- Android计算器(最初版)
- (原创)斗牛游戏 初版
- LNMP监控脚本(初版)
- c#——窗体实验-最初版
- 自己实现基于key-value的NoSQL数据库(一)——起步,初版
- Solaris库线程实现分析 初版
- Matrix(方法实现)
- C语言实现dijkstra(adjacence matrix)
- 同名复选框的解决方案(初版)
- “三段式”编程结构(最初版)
- 我的vb抽奖程序(初版)
- 快速排序学习3(最初版)
- 小论商人性格
- 三种企业:家族式、金融式和经理式 (理论)
- 创业警言:创业阶段之优势
- 北京不是我们的家
- (对话)金钱不能使你富有
- matrix(c++实现,初版)
- listen to
- 创业家的四种精神
- C++编程思想读书笔记--之const
- 我这里有一个寓言故事
- 我们所担心的事,百分之九十都不会发生,而对永远不会发生的事情瞎操心是很悲哀的。
- 想……
- IBM aio4j初见
- 完美的人生