简体   繁体   English

C ++通过函数std :: array的std :: array

[英]C++ Passing through a function an std::array of std::array

I'm new with std::array. 我是std :: array的新手。 I have to create this function 我必须创建此功能

void foo(std::array < std::array <double, a>& , b> & previous)

where a and b are two fixed integer values. 其中a和b是两个固定的整数值。 How should I do? 我应该怎么做?

Thank you in advance! 先感谢您!

The "variables" a and b must be compile-time constants. “变量” ab 必须是编译时常量。 If you want them to be variable or set during runtime, you must use std::vector instead. 如果希望它们在运行时是可变的或设置的,则必须改用std::vector

Template arguments (such as those to std::array ) must be known at compile time. 模板参数(例如std::array )必须在编译时知道。 To be general, you can write a function that is templated on the two sizes, eg: 一般而言,您可以编写以两种尺寸为模板的函数,例如:

template <std::size_t N, std::size_t M>
void foo(std::array<std::array<double, N>, M> &previous) {
    // code goes here, e.g.:
    previous[M - 1][N - 1] = static_cast<double>(M * N);
}

The template arguments will be deduced by the function argument type, so your array of arrays can have any dimensions you want and you don't need to specify them when calling foo . 模板参数将由函数参数类型推导,因此您的数组数组可以具有所需的任何维,并且在调用foo时无需指定它们。

std::array<std::array<double, 10>, 20> my_array;
foo(my_array); // equivalent to foo<10, 20>(my_array);

By the way, using std::vector<std::vector<T>> is a terrible, awful idea if you want your program to be robust or fast. 顺便说一句,如果您希望程序健壮或快速,那么使用std::vector<std::vector<T>>是一个可怕的可怕想法。 Not only must you manually manange and carefully track each inner vector 's length, you also take a huge performance hit by individually heap-allocating N M -length arrays rather than, say, a single N x M -length array. 不仅必须手动调整并仔细跟踪每个内部vector的长度,而且通过单独堆分配N M长度的数组而不是单个N x M长度的数组,也会带来巨大的性能损失。

As mentioned pmttavara you can perform compile time deduction of the dimensions and type. 如前所述,您可以执行尺寸和类型的编译时推导。

template <typename Scalar, std::size_t I, std::size_t J>
void
Foo(std::array<std::array<Scalar, I>, J>& previous) {
}

int main() {
        std::array<std::array<double, 10>, 20> data;
        Foo(data);
}

Alternatively you can treat this data like a matrix. 或者,您可以将此数据视为矩阵。

template <typename Scalar>
class Matrix {
public:
    Matrix(std::size_t rows, std::size_t cols)
        : rows(rows)
        , cols(cols)
        , data(rows * cols, Scalar(0))
    {}

    Scalar&
    at(std::size_t row, std::size_t col) {
        std::size_t n = (row * cols) + col;
        return data.at(n);
    }

private:
    std::size_t rows;
    std::size_t cols;
    std::vector<Scalar> data;
};

int main() {
        Matrix<double> m(3, 3);
        m.at(2, 2) = 3;
}

a and b must be known at compil time. 必须在编译时知道ab You can declare them as 您可以将它们声明为

constexpr int a = 10;
constexpr int b = 100;

If is not possible to know at compile-time this two variable you have to use std::vectors instead: 如果在编译时无法知道这两个变量,则必须改用std::vectors

typedef std::vector< const std::vector > Matrix;
void f( const Matrix& m );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM