首页 > 解决方案 > 可变元组实现

问题描述

我一直在尝试在通用模板和结构上使用可变元组。这个想法是获得一个可变元组的最大值和总和。到目前为止,我能够获得所需的结果,但必须使用全局静态变量。我已经在下面发布了实现的代码。

    #include <iomanip>
    #include <complex>
    #include <cmath>
    #include <tuple>
    #include <string>
    #include <iostream>

    using namespace std;
    using namespace std::complex_literals;

    template<typename T>
    static T max;

    template<typename T>
    static T sum;

    template<typename T>
    static T average;

    template <typename T, typename Tuple, std::size_t N>
    struct Calculator
    {
    static void maximum(const Tuple& pack)
    {
    Calculator<T, Tuple, N - 1>::maximum(pack);
    T packValue = get<N - 1>(pack);
    if (packValue > max<T>)
    {
    //T max = get<0>(pack);
    //Try the above instead of the below code to calculate max
    max<T> = get<N - 1>(pack);
    }
    }

    static void summation(const Tuple& pack)
    {
    Calculator<T, Tuple, N - 1>::summation(pack);
    T packValue = get<N - 1>(pack);
    sum<T> += packValue;
    }

    static void averager(const Tuple& pack)
    {
    average<T> = sum<T> / N;
    }


    };

    template<typename T, typename Tuple>
    struct Calculator<T, Tuple, 1>
    {
    static void maximum(const Tuple& pack)
    {
    //T max = get<0>(pack);
    //Try the above instead of the below code to calculate max
    max<T> = get<0>(pack);
    }

    static void summation(const Tuple& pack)
    {
    sum<T> = get<0>(pack);
    }
    };




    int main()
    {
    tuple<double, double, double, double, double, double, double> t1 = make_tuple(16565.256, 45.539,     0.25, 1000.25, 1.25036, 35.66, 210.20);

    Calculator<double, tuple<double, double, double, double, double, double, double>, 7>::maximum(t1);

    cout << "Maximum is: " << max<double> << endl;

    Calculator<double, tuple<double, double, double, double, double, double, double>, 7>::summation(t1);
    cout << "Total Sum is: " << sum<double> << endl;

    Calculator<double, tuple<double, double, double, double, double, double, double>, 7>::averager(t1);
    cout << "Average is: " << average<double> << endl;



    std::complex<int> c2(22, 3);
    std::complex<int> ci(15, 41);
    tuple<std::complex<int>, std::complex<int> > ct1 = make_tuple(ci, c2);

    Calculator< std::complex<int>, tuple<std::complex<int>, std::complex<int> >, 2>::summation(ct1);

    cout << "Summation of complex numbers is: " << sum<std::complex<int>> << endl;

    }

我的问题是,是否有可能实现一个不使用全局静态变量来保存 sum 和 max 值的版本,但如果可能的话,可以使用 Variadic Templates 和 Structs 的替代实现?

标签: c++11tuples

解决方案


你使用变量 template,所以你至少有 C++14。在 C++14 中,您可以使用index_sequence简单的方法来提取所有元组项并将递归函数重写为非递归:

template <typename T, typename Tuple, std::size_t N>
struct Calculator
{
template<size_t ... Indices>
static T maxHelper(const Tuple& pack, std::index_sequence<Indices...>)  {
    return std::max( {std::get<Indices>(pack)...} );
} // [1]

static T maximum(const Tuple& pack)  {
    return maxHelper(pack, std::make_index_sequence<N>{}); 
}

template<size_t ... Indices>
static T sumHelper(const Tuple& t, std::index_sequence<Indices...>)  {
    T arr[] = { std::get<Indices>(t)... }; // [2]
    return std::accumulate(arr,arr+N,T{}); // [3]
}

static T summation(const Tuple& pack) {
    return sumHelper(pack,std::make_index_sequence<N>{});
}
};

在 [1] 元组中的项目被提取到 中std::initializer_list,然后std::max调用该列表的重载。

[2] 和 [3] 中的元组项被提取到数组中,并通过std::accumulate.

演示


从 C++17 开始使用std::apply(以及求和的折叠表达式):

template<class ... Args>
auto maxValue(std::tuple<Args...> t) {
    return std::apply( [](auto&&... args) { return std::max({args...}); },t);
}

template<class ... Args>
auto sumValue(std::tuple<Args...> t) {
    return std::apply( [](auto&&... args){ return (args + ...);} , t);
}

演示


推荐阅读