join_rows()函数的正确类型是什么? [英] What's the right type for a join_rows() function?

查看:168
本文介绍了join_rows()函数的正确类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个函数,将两个2D的行连接起来数组

template <typename S, typename T>
Array<typename S::Scalar, Dynamic, Dynamic> join_rows(const ArrayBase<S> & A, const ArrayBase<T> & B) {
    Array<typename S::Scalar, Dynamic, Dynamic> C (A.rows(), A.cols()+B.cols());
    C << A, B;
    return C;
}

我想写一个更通用的函数,可以连接两个以上的数组

I would like to write a more general function that can join more than two arrays.

它应该能够使用任何可迭代的容器,例如。 std :: list std :: vector ,所以我将使用模板模板参数计。

It should be able to work with any iterable container, eg. std::list or std::vector, so I would use a template template paratemeter.

我可以轻松地通过两个for循环来纠正函数体,这不是问题,我只是在努力弄清楚该函数的正确类型是什么。

I can easily right the function body with two for loops, that's not the issue, I'm just struggling to figure out what the right type for such a function would be.

(ps。我什至不确定我上面的代码是否具有最佳类型,但似乎能胜任工作)

(ps. I'm not even sure if my above code has the best type, but it seems to do the job)

推荐答案

我不确定如何声明任意 Array s的向量,但是您可以实现将一个或更多参数直接传递给它。通常,这是通过递归调用自身,处理每个连续的参数来完成的:

I'm not sure how to declare a vector of arbitrary Arrays, but you can implement a function template that combines one or more arguments directly passed to it. This is typically done by calling itself recursively, processing each successive argument:

// end case (one argument): just forward the same array
template <typename T>
T&& join_rows(T&& A) {
    return std::forward<T>(A);
}

// main function template: two or more arguments
template <typename S, typename T, typename... R>
Array<typename S::Scalar, Dynamic, Dynamic> join_rows(const ArrayBase<S>& A,
                                                      const ArrayBase<T>& B,
                                                      const ArrayBase<R>&... rest) {
    Array<typename S::Scalar, Dynamic, Dynamic> C(A.rows(), A.cols()+B.cols());
    C << A, B;
    return join_rows(C, rest...); // call with the first two arguments combined
}

示例以说明用法:

int main() {
    Array<int, 1, 3> arr1 = {1, 2, 3};
    Array<int, 1, 2> arr2 = {4, 5};
    Array<int, 1, 4> arr3 = {9, 8, 7, 6};

    cout << join_rows(arr1, arr2, arr3.reverse()) << endl; // 1 2 3 4 5 6 7 8 9

    return 0;
}

如果要限制一个参数,请 join_rows 仅接受 Eigen :: Array s,然后添加 std :: enable_if 检查 ArrayBase< T> 基类:

If you want to restrict the one-argument join_rows to only accept Eigen::Arrays, add an std::enable_if checking for an ArrayBase<T> base class:

template <typename T>
std::enable_if_t<std::is_base_of<ArrayBase<std::decay_t<T>>,std::decay_t<T>>::value, T&&>
join_rows(T&& A) {
    return std::forward<T>(A);
}

对于大型 Array s,可能会有更有效的方法来实现。您可能会返回一个仅分配一个新的 Array 对象的代理对象。

For large Arrays, there might be more efficient ways to implement this. You could probably return a proxy object that will only allocate one new Array object.

这篇关于join_rows()函数的正确类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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