gpt4 book ai didi

c++ - 为什么在创建矩阵类时使用 vector 不好?

转载 作者:太空狗 更新时间:2023-10-29 19:46:09 27 4
gpt4 key购买 nike

对于我的矩阵类,我做了:

template<typename T, std::uint32_t Height, std::uint32_t Width>
class Matrix
{
private:
std::array<std::array<T, Width>, Height> Elements;
static_assert(std::is_arithmetic<T>::value, "Argument T must be of arithmetic type.");

public:
Matrix();
Matrix(T* Data);
Matrix(T** Data);
Matrix(T Data[Height][Width]);
Matrix(const std::array<std::array<T, Width>, Height> &Data);

inline int size() {return Width * Height;}
inline const int size() const {return Width * Height;}

inline int width() {return Width;}
inline const int width() const {return Width;}

inline int height() {return Height;}
inline const int height() const {return Height;}

std::array<T, Width>& operator[](int Index);
const std::array<T, Width>& operator[](int Index) const;

Matrix& operator = (const Matrix &M);

Matrix& operator + (const Matrix &M);

Matrix& operator - (const Matrix &M);

Matrix& operator * (const Matrix &M);
};

template<typename T, std::uint32_t Height, std::uint32_t Width>
Matrix<T, Height, Width>::Matrix() {std::memset(&Elements, 0, sizeof(T) * Width * Height);}

template<typename T, std::uint32_t Height, std::uint32_t Width>
Matrix<T, Height, Width>::Matrix(T* Data) {if (Data) std::memcpy(&Elements, Data, sizeof(T) * Width * Height);}

template<typename T, std::uint32_t Height, std::uint32_t Width>
Matrix<T, Height, Width>::Matrix(T** Data) {if (Data) std::memcpy(&Elements, &Data[0][0], sizeof(T) * Width * Height);}

template<typename T, std::uint32_t Height, std::uint32_t Width>
Matrix<T, Height, Width>::Matrix(T Data[Height][Width]) {std::memcpy(&Elements, &Data[0][0], sizeof(T) * Width * Height);}

template<typename T, std::uint32_t Height, std::uint32_t Width>
Matrix<T, Height, Width>::Matrix(const std::array<std::array<T, Width>, Height> &Data) {std::memcpy(&Elements, &Data[0][0], sizeof(T) * Width * Height);}

template<typename T, std::uint32_t Height, std::uint32_t Width>
std::array<T, Width>& Matrix<T, Height, Width>::operator[](int Index) {return Elements[Index];}

template<typename T, std::uint32_t Height, std::uint32_t Width>
const std::array<T, Width>& Matrix<T, Height, Width>::operator[](int Index) const {return Elements[Index];}

因为我在网上看到许多评论说不要使用 vector 而是使用数组或使用 std::valarray..

现在我问的原因是因为我想重写我的矩阵类,这样我就不必继续做:Matrix<Type, Width, Height>每次.. 我宁愿在构造函数中执行一次,而不必为每个函数都键入它.. 如上所述。例如,我必须为每个函数和每个 Matrix 参数写出很长的模板声明。此外,我不确定如何删除 vector 的调整大小/推回,以便当用户索引 vector 时,他们将无法调整它的大小,所以我使用了数组。

我打算使用一维数组并通过 (I * Width + J) 对其进行索引,但后来我失去了 [][] 运算符。

使用 vector 的 vector 不好吗?有什么想法可以改进我的类(class)并使其符合 RAII 标准吗?我不太明白如何使用 valarray 并且上面的维护很烦人。任何想法表示赞赏。

最佳答案

的确, vector 的 vector 并不是最好的方法。假设数据是矩形的(不是锯齿状的), vector 中的 vector 方法进行的分配效率低下,更不用说它阻止了一些常见的“ reshape ”操作(“将我的 2x3 矩阵视为 3x2 或 6x1 而不复制”)。

I was going to use a 1D array and index it doing (I * Width + J) but then I lose my [][] operator.

当然,使用一维 vector 。那太棒了。然后您可以调整它的大小、 reshape 它的形状等,并为许多操作提供接近最佳的性能。你保留 RAII。

但是您不想失去双下标 ([x][y]) 的能力?好的。只需让您的 operator[] 返回一个代理对象(您实现的)。代理将拥有自己的 operator[],它将在第二个轴上运行。也就是说,第一个 [] 将返回一个轻量级对象,该对象只知道足以实现第二个 []

关于c++ - 为什么在创建矩阵类时使用 vector 不好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17005704/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com