在创建具有可变维度的矩阵类的类模板时,我已经多次使用过这种模式。
Matrix.h
#ifndef MATRIX_H
template<typename Type, size_t... Dims>
class Matrix {
public:
static const size_t numDims_ = sizeof...(Dims);
private:
size_t numElements_;
std::vector<Type> elements_;
std::vector<size_t> strides_; // Technically this vector contains the size of each dimension... (its shape)
// actual strides would be the width in memory of each element to that dimension of the container.
// A better name for this container would be dimensionSizes_ or shape_
public:
Matrix() noexcept;
template<typename... Arg>
Matrix( Arg&&... as ) noexcept;
const Type& operator[]( size_t idx ) const;
size_t numElements() const {
return elements_.size();
}
const std::vector<size_t>& strides() const {
return strides_;
}
const std::vector<Type>& elements() const {
return elements_;
}
}; // matrix
#include "Matrix.inl"
#endif // MATRIX_H
Matrix.inl
template<typename Type, size_t... Dims>
Matrix<Type, Dims...>::Matrix() noexcept :
strides_( { Dims... } ) {
using std::begin;
using std::end;
auto mult = std::accumulate( begin( strides_ ), end( strides_ ), 1, std::multiplies<>() );
numElements_ = mult;
elements_.resize( numElements_ );
} // Matrix
template<typename Type, size_t... Dims>
template<typename... Arg>
Matrix<Type, Dims...>::Matrix( Arg&&... as ) noexcept :
elements_( { as... } ),
strides_( { Dims... } ){
numElements_ = elements_.size();
} // Matrix
template<typename T, size_t... d>
const T& Matrix<T,d...>::operator[]( size_t idx ) const {
return elements_[idx];
} // Operator[]
Matrix.cpp
#include "Matrix.h"
#include <vector>
#include <numeric>
#include <functional>
#include <algorithm>
main.cpp
#include <vector>
#include <iostream>
#include "matrix.h"
int main() {
Matrix<int, 3, 3> mat3x3( 1, 2, 3, 4, 5, 6, 7, 8, 9 );
for ( size_t idx = 0; idx < mat3x3.numElements(); idx++ ) {
std::cout << mat3x3.elements()[idx] << " ";
}
std::cout << "\n\nnow using array index operator\n\n";
for ( size_t idx = 0; idx < mat3x3.numElements(); idx++ ) {
std::cout << mat3x3[idx] << " ";
}
std::cout << "\n\ncheck the strides\n\n";
for ( size_t idx = 0; idx < mat3x3.numDims_; idx++ ) {
std::cout << mat3x3.strides()[idx] << " ";
}
std::cout << "\n\n";
std::cout << "=================================\n\n";
Matrix<float, 5, 2, 9, 7> mf5x2x9x7;
// Check Strides
// Num Elements
// Total Size
std::cout << "The total number of dimensions are: " << mf5x2x9x7.numDims_ << "\n";
std::cout << "The total number of elements are: " << mf5x2x9x7.numElements() << "\n";
std::cout << "These are the strides: \n";
for ( size_t n = 0; n < mf5x2x9x7.numDims_; n++ ) {
std::cout << mf5x2x9x7.strides()[n] << " ";
}
std::cout << "\n";
std::cout << "The elements are: ";
for ( size_t n = 0; n < mf5x2x9x7.numElements(); n++ ) {
std::cout << mf5x2x9x7[n] << " ";
}
std::cout << "\n";
std::cout << "\nPress any key and enter to quit." << std::endl;
char c;
std::cin >> c;
return 0;
} // main
这是Same Type <T>的简单可变多维矩阵类
您可以创建不同大小的浮点数、整数、字符等矩阵,例如2x2、2x3、5x3x7、4x9x8x12x2x19。这是一个非常简单但用途广泛的类。
它使用std::vector<>,所以搜索时间是线性的。多维矩阵在维度上增长越大,内部容器将根据每个维度的大小而增长;如果每个单独的维度都具有很大的维度,这可能会很快“爆炸”,例如:9x9x9 只是一个3 dimensional volumetric matrix,它比2x2x2x2x2 具有更多的元素,5 dimensional volumetric matrix。第一个矩阵有729 元素,而第二个矩阵只有32 元素。
我没有包含默认构造函数、复制构造函数、移动构造函数,也没有包含任何可以接受std::container<T> 或另一个Matrix<T,...> 的重载构造函数。这可以作为 OP 的练习来完成。
我也没有包含任何简单的函数来给出主容器中总元素的大小,也没有包含与strides 容器大小相同的总维度数。 OP 应该能够非常简单地实现这些。
至于strides 和多维坐标索引,OP 需要使用stride 值再次计算适当的索引,我将其作为主要练习。
编辑 - 我继续添加了一个默认构造函数,将一些成员移动到类的私有部分,并添加了一些访问函数。我这样做是因为我只是想在 main 函数中展示这个类的强大功能,即使在创建它的类型的空容器时也是如此。
您甚至可以使用用户 Yakk 的“步幅和切片”算法来回答,并且应该能够轻松地将其直接插入到此类中,从而为您提供所需的全部功能。