如何用容器类构造矩阵?

wengzuliang 2003-09-30 09:20:22
如题
...全文
94 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
iBuffer 2003-10-13
  • 打赏
  • 举报
回复
稀疏矩阵之我的方法:
用一个一维数组表示列,然后以数组的每个元素建立map作为行。
各位大侠给点意见
snipersu 2003-10-10
  • 打赏
  • 举报
回复
牛X!
Robin 2003-10-07
  • 打赏
  • 举报
回复
hehe!
都不错!
短歌如风 2003-10-07
  • 打赏
  • 举报
回复
稀疏矩阵则可以用map实现:
template<typename T>
class matrix
{
int row_count;
int col_count;
T default_value;
mutable map<pair<int, int>, T> data;
public:
class value_ref//封装元素引用类型的类,在赋值时可能会向map中添加新的元素或删除元素,取值时会在map中不存在指定行、列号时返回缺省值。

{
map<pair<int, int>, T> & data_ref;
int row_index;
int col_index;
T default_value;
public:
value_ref(map<pair<int, int>, T>& init_data_ref, int init_row_index, int init_col_index, T init_default_value):
data_ref(init_data_ref),
row_index(init_row_index),
col_index(init_col_index),
default_value(init_default_value)
{
}
T& operator = (const T& new_value)
{
if (new_value == default_value)
{
map<pair<int,int>,T>::iterator i = data_ref.find(pair<int, int>(row_index, col_index));
if (i != data_ref.end())
data_ref.erase(i);
}
else
data_ref[pair<int, int>(row_index, col_index)] = new_value;
}
operator T() const
{
map<pair<int, int>, T>::iterator i = data_ref.find(pair<int, int>(row_index, col_index));
if (i == data_ref.end())
return default_value;
else
return i->second;
}
};
matrix(int init_row_count, int init_col_count, T init_default_value):
col_count(init_row_count),
row_count(init_col_count),
default_value(init_default_value)
{}
value_ref get_value(int row_index, int col_index)
{
return value_ref(data, row_index, col_index, default_value);
}
const value_ref get_value(int row_index, int col_index) const
{
return value_ref(data, row_index, col_index, default_value);
}
int get_col_count() const
{
return col_count;
}
int get_row_count() const
{
return row_count;
}
value_ref operator()(int row_index, int col_index)
{
return get_value(row_index, col_index);
}
const value_ref operator()(int row_index, int col_index) const
{
return get_value(row_index, col_index);
}
};

上面的代码没有考虑行、列号越限的情况,如果要求更安全,可以加上这一内容。
短歌如风 2003-10-07
  • 打赏
  • 举报
回复
Matrix本身就是一个逻辑概念,不适合于直接使用vector等容器表现,可以使用某一容器实现,但应该使用组合而不是继承。
要实现一个一般的矩阵可以这样:

template<typename T>
class matrix
{
int row_count;
int col_count;
vector<T> data;
public:
matrix(int init_row_count, int init_col_count):
col_count(init_row_count),
row_count(init_col_count),
data(init_row_count * init_col_count)
{}
T& get_value(int row_index, int col_index)
{
return data[row_index * col_count + col_index];
}
const T& get_value(int row_index, int col_index) const
{
return data[row_index * col_count + col_index];
}
int get_col_count() const
{
return col_count;
}
int get_row_count() const
{
return row_count;
}
T& operator()(int row_index, int col_index)
{
return get_value(row_index, col_index);
}
const T& operator()(int row_index, int col_index) const
{
return get_value(row_index, col_index);
}
};

由于矩阵运算经常要在不同行列数的矩阵间进行(如乘法),所以把矩阵的行、列数作为状态而不是类型。

fifo333 2003-10-06
  • 打赏
  • 举报
回复
我也是用上述方法,很方便。
维数可以用模板参数设定,也可以用构造函数设定。
但如果要实现矩阵分解、稀疏矩阵、求秩等功能就比较复杂了。
这种方法基本的功能和运算都可以实现。
数据结构STL实现(好像是这个书名)中也是这样实现的。
C++和面向对象数值计算一书中有更复杂完善的实现,可以参考。
xueweizhong 2003-10-06
  • 打赏
  • 举报
回复
template <class T>
struct Matrix : vector<vector<T> >
{
T& operator () (int i, int j)
{
return at(i).at(j);
}
T const& operator () (int i, int j) const
{
return at(i).at(j);
}
....
};

24,854

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 工具平台和程序库
社区管理员
  • 工具平台和程序库社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧