基于模板C ++创建转置矩阵函数

时间:2013-09-17 11:09:17

标签: c++ function templates transpose

我正在尝试使用此amazing guide创建我的第一个模板,但是遇到了我不知道如何处理多个嵌套参数的问题:

我创建的原始函数实现如下:

QVector< QVector<double> > transpose(QVector< QVector<double> > &matrix)
{
    int pre_numcols = matrix.size();
    int pre_numrows = matrix[0].size();
    QVector< QVector<double> > transposed(pre_numrows);
    QVector<double> newcols(pre_numcols);
    qFill(newcols.begin(), newcols.end(), 0.0);
    qFill(transposed.begin(), transposed.end(), newcols);
    qDebug()<<transposed.size();
    qDebug()<<transposed[0].size();
    for (int i = 0; i < pre_numcols; ++i)
    {
        for (int j = 0; j < pre_numrows; ++j)
        {
            transposed[j][i] = matrix[i][j];
        }
    }
    return transposed;
}

考虑到我只是重新调整点数,很多类型都是可能的。

QVector可以替换为std::vectordouble甚至可以替换为string

我到目前为止:

template<class T>
T transpose(T &matrix)
{
    int pre_numcols = matrix.size();
    int pre_numrows = matrix[0].size();
    T transposed(pre_numrows);
    QVector<double> newcols(pre_numcols);  // How to do this one?

如果我将T作为“容器容纳容器拿着一些小型”,我怎么能声明newcols变量,因为它是一个子集? / p>

注意:我将稍后编辑qFill()部分以适应其他情况,例如std::

1 个答案:

答案 0 :(得分:2)

您可以将模板用作模板参数:

template <
    template <typename> class Container,
    typename ValueType
>
void foo(Container<ValueType> const & c);

所以你的模板会变成:

template <
    template <typename> class Container,
    typename ValueType
>
Container< Container<ValueType> > transpose(Container< Container<ValueType> > & matrix)
{
    int pre_numcols = matrix.size();
    int pre_numrows = matrix[0].size();
    Container< Container<ValueType> > transposed(pre_numrows);
    Container<ValueType> newcols(pre_numcols);
    // ...
    return transposed;
}

如果它像那样工作那将是整洁的。但是,与往常一样,新问题出现了! std::vector不是template <typename T> class vector,而是

template <
    typename T,
    typename Allocator = allocator<T>
>
class vector

所以我们必须将我们的功能改为

template <
    template <typename, typename> class C,
    typename T,
    template <typename> class A = std::allocator,
    typename InnerType = C< T, A<T> >,
    typename OuterType = C< InnerType, A<InnerType> >
>
OuterType transpose(OuterType & matrix)
{
    int pre_numcols = matrix.size();
    int pre_numrows = matrix[0].size();
    OuterType transposed(pre_numrows);
    InnerType newcols(pre_numcols);
    // ...
    return transposed;
}

这不太整齐,我不确定Qt容器是否兼容。

既然你说可以使用C ++ 11,你就可以使用第一个函数(使用template <typename> class Container)并使用标准容器的模板别名:

template <typename T> using vector = std::vector<T, std::allocator<T>>;
vector<vector<int>> v;
auto vprime = transpose<vector>(v);

这是使用辅助模板元函数的另一种可能的解决方案,我认为它比以前更清晰:

namespace
{
    template <typename T> struct get_inner_i {};

    template <template <typename> class T, typename Inner> 
    struct get_inner_i<T<Inner>> { typedef Inner type; };

    template <
        template <typename, typename> class T, 
        typename Inner,
        template <typename> class Allocator
    > struct get_inner_i<T<Inner, Allocator<Inner>>> { typedef Inner type; };

    template <typename T> using get_inner = typename get_inner_i<T>::type;
}

template <typename MatrixType>
MatrixType transpose(MatrixType const & matrix)
{
    auto const nrows = matrix.size();
    auto const ncols = nrows > 0 ? matrix[0].size() : 0;

    MatrixType transposed(ncols, get_inner<MatrixType>(nrows));
    for(auto k = 0; k < nrows; ++k)
        for(auto j = 0; j < ncols; ++j)
            transposed[j][k] = matrix[k][j];

    return transposed;
}

为什么这个更加冗长的解决方案更优秀?这不是我们要编写多少代码,而是我们的功能对用户来说是多么容易和直观。在这个版本中,我们再次只有T作为模板参数,因此没有必要明确指定函数的模板参数。

此外,我们的元函数可以推断它是否是具有自定义分配器参数的容器,并且用户不必执行上面提到的using模板别名技巧。


这是另一种解决方案,再次优于之前的解决方案,使用std::begin来确定容器的值类型而不考虑其模板参数,或者它是否甚至是模板,只要它提供beginend迭代器,或者是C样式数组:

namespace
{
    template <typename Container>
    struct value_type_i
    {
        typedef typename std::decay<
            decltype(*std::begin(std::declval<
                Container const &
            >()))
        >::type type;
    };

    template <typename Container>
    using value_type = typename value_type_i<Container>::type;
}

template <typename MatrixType>
MatrixType transpose(MatrixType const & matrix)
{
    auto const nrows = matrix.size();
    auto const ncols = nrows > 0 ? matrix[0].size() : 0;

    MatrixType transposed(ncols, value_type<MatrixType>(nrows));
    for(auto k = 0; k < nrows; ++k)
        for(auto j = 0; j < ncols; ++j)
            transposed[j][k] = matrix[k][j];

    return transposed;
}

您可以在此处看到它:http://ideone.com/cCAyFD

相关问题