设计Matrix类 [英] Designing a Matrix class

查看:82
本文介绍了设计Matrix类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在实施一个Matrix类,作为项目的一部分。这是界面

我设计的:


class Matrix()


{


私人:


vector<矢量<诠释> > m_Data;

pair< int,int> m_size;

public:


Matrix(unsigned int n);

Matrix(unsigned int m,unsigned int n);


~Matrix();

......................... ...........


}


我有几个问题:

1.除了

向量的向量之外,还有更好的方法来建模数据结构吗?我意识到我可以从头开始编写Matrix类,但我想要

来使用STL容器。


2.如果我使用矢量矢量,动态内存分配如何工作?我不想完全理解STL的矢量如何处理内存。例如,为了
调整矩阵的大小,我将如何删除?旧矩阵的记忆?

''resize''和'clear''功能保证适当的内存管理,

没有泄漏?


提前致谢!!


-CK

Hi,

I''m implementing a Matrix class, as part of a project. This is the interface
I''ve designed:

class Matrix( )

{

private:

vector< vector<int> > m_Data;
pair<int, int> m_size;
public:

Matrix(unsigned int n);
Matrix(unsigned int m, unsigned int n);

~Matrix();
....................................

}

I have a couple of questions:

1. Is there a better way to model the data structure other than a vector of
vectors? I realize I can write up the Matrix class from scratch, but I want
to use STL containers.

2. If I use a vector of vectors, how would dynamic memory allocation work? I
don''t fully understand how STL''s vector handles memory. For example, to
resize a matrix, how would I "delete" the memory for the old matrix? Would
the ''resize'' and ''clear'' functions guarantee proper memory management,
without leaks?

Thanks in advance!!

-CK


推荐答案

我正在实现一个Matrix类,作为项目的一部分。这是我设计的

界面:


class Matrix()

{
$ b $私人:

vector<矢量<诠释> > m_Data;

pair< int,int> m_size;

public:

Matrix(unsigned int n);

Matrix(unsigned int m,unsigned int n);

~Matrix();

................................. ...

}


我有几个问题:

1.有没有更好的方法来建模数据除了
向量的向量之外的结构?我意识到我可以从头开始编写Matrix类,但我想要

来使用STL容器。


2.如果我使用矢量矢量,动态内存分配如何工作?我不想完全理解STL的矢量如何处理内存。例如,为了
调整矩阵的大小,我将如何删除?旧矩阵的记忆?

''resize''和'clear''功能保证适当的内存管理,

没有泄漏?


在此先感谢!!

-CK

Hi, I''m implementing a Matrix class, as part of a project. This is the
interface I''ve designed:

class Matrix( )
{
private:
vector< vector<int> > m_Data;
pair<int, int> m_size;
public:
Matrix(unsigned int n);
Matrix(unsigned int m, unsigned int n);
~Matrix();
....................................
}

I have a couple of questions:
1. Is there a better way to model the data structure other than a vector of
vectors? I realize I can write up the Matrix class from scratch, but I want
to use STL containers.

2. If I use a vector of vectors, how would dynamic memory allocation work? I
don''t fully understand how STL''s vector handles memory. For example, to
resize a matrix, how would I "delete" the memory for the old matrix? Would
the ''resize'' and ''clear'' functions guarantee proper memory management,
without leaks?

Thanks in advance!!
-CK


我正在实施一个Matrix类,作为项目的一部分。这是我设计的

界面:


class Matrix()

{
$ b $私人:

vector<矢量<诠释> > m_Data;

pair< int,int> m_size;

public:

Matrix(unsigned int n);

Matrix(unsigned int m,unsigned int n);

~Matrix();

................................. ...

}


我有几个问题:

1.有没有更好的方法来建模数据除了
向量的向量之外的结构?我意识到我可以从头开始编写Matrix类,但我希望

使用STL容器。

2.如果我使用向量向量,动态内存将如何分配工作?我不想完全理解STL的矢量如何处理内存。例如,为了
调整矩阵的大小,我将如何删除?旧矩阵的记忆?

''resize''和'clear''功能保证适当的内存管理,

没有泄漏?


在此先感谢!!

-CK
Hi, I''m implementing a Matrix class, as part of a project. This is the
interface I''ve designed:

class Matrix( )
{
private:
vector< vector<int> > m_Data;
pair<int, int> m_size;
public:
Matrix(unsigned int n);
Matrix(unsigned int m, unsigned int n);
~Matrix();
....................................
}

I have a couple of questions:
1. Is there a better way to model the data structure other than a vector of
vectors? I realize I can write up the Matrix class from scratch, but I want
to use STL containers.
2. If I use a vector of vectors, how would dynamic memory allocation work? I
don''t fully understand how STL''s vector handles memory. For example, to
resize a matrix, how would I "delete" the memory for the old matrix? Would
the ''resize'' and ''clear'' functions guarantee proper memory management,
without leaks?

Thanks in advance!!
-CK


我正在实施一个Matrix类,作为项目的一部分。这是我设计的

界面:


class Matrix()

{
$ b $私人:

vector<矢量<诠释> > m_Data;

pair< int,int> m_size;

public:

Matrix(unsigned int n);

Matrix(unsigned int m,unsigned int n);

~Matrix();

................................. ...

}


我有几个问题:

1.有没有更好的方法来建模数据除了
向量的向量之外的结构?我意识到我可以从头开始编写Matrix类,但我想要

来使用STL容器。


2.如果我使用矢量矢量,动态内存分配如何工作?我不想完全理解STL的矢量如何处理内存。例如,为了
调整矩阵的大小,我将如何删除?旧矩阵的记忆?

''resize''和'clear''功能保证适当的内存管理,

没有泄漏?


提前致谢!!

-CK

Hi, I''m implementing a Matrix class, as part of a project. This is the
interface I''ve designed:

class Matrix( )
{
private:
vector< vector<int> > m_Data;
pair<int, int> m_size;
public:
Matrix(unsigned int n);
Matrix(unsigned int m, unsigned int n);
~Matrix();
....................................
}

I have a couple of questions:
1. Is there a better way to model the data structure other than a vector of
vectors? I realize I can write up the Matrix class from scratch, but I want
to use STL containers.

2. If I use a vector of vectors, how would dynamic memory allocation work? I
don''t fully understand how STL''s vector handles memory. For example, to
resize a matrix, how would I "delete" the memory for the old matrix? Would
the ''resize'' and ''clear'' functions guarantee proper memory management,
without leaks?

Thanks in advance!!
-CK


这篇关于设计Matrix类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆