繁体   English   中英

整数的向量…的向量的向量之和

[英]Sum of vector of vectors of vectors … of integers

我正在尝试将向量的向量的所有元素相加...整数的向量:类似于std::vector<std::vector<std::vector<std::vector<int>>>>不需要每一层都有相同的大小。

我想使用模板来完成它,所以我做到了:

namespace nn
{
    template < class T >
    int sumAllElements(std::vector<T> v)
    {
        int size = v.size();
        int output = 0;

        for ( int i = 0 ; i < size ; i++ )
        {
                                                //should call the function below 
            output += sumAllElements( v[ i ] ); //or this function, depending in 
                                                //which "layer" we are
        }

        return output;
    }

    int sumAllElements(std::vector<int> v)
    {
        int size = v.size();
        int output = 0;

        for ( int i = 0 ; i < size ; i++ )
        {
            output += v[ i ]; //we've reached the bottomest layer,
                              //so just sum everybory
        }

        return output;
    }
}

但是,这正在发生:

CMakeFiles\test.dir/objects.a(main.cpp.obj): In function `main':
D:/test/main.cpp:49: undefined reference to `int nn::sumAllElements<std::vector<int, std::allocator<int> > >(std::vector<std::vector<int, std::allocator<int> >, std::allocator<std::vector<int, std::allocator<int> > > >)'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\test\build.make:141: test.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:67: CMakeFiles/test.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:79: CMakeFiles/test.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: test] Error 2

我真的不知道为什么

提前致谢。

您不能调用尚未声明的函数。 模板有时可以使该问题消失,但并非总是如此。 这是在template < class T > int sumAllElements(std::vector<T> v)之前简单需要int sumAllElements(std::vector<int> v)声明的情况之一int sumAllElements(std::vector<int> v) template < class T > int sumAllElements(std::vector<T> v)

阅读您的错误消息。 看起来您的函数与main.cpp位于单独的编译单元中。 如果您的函数在.h文件中,请在main.cpp中#include头文件。

我建议使用模板专门化声明:

template<>
int sumAllElements(std::vector<int> v)
{
 ...   
}

另一个不相关的建议是通过const引用传递向量。 当前,您正在按值传递它们,如果向量很大,则传递代价可能很高。

您可以使用SFINAE启用/禁用所需的专业化:

template <class T, std::enable_if_t<std::is_arithmetic<T>::value, int> = 0>
auto sum_all(const std::vector<T>& v)
{
    T sum = 0;

    for (auto& e : v)
    {
        sum += e;
    }

    return sum;
}

template <class T, std::enable_if_t<!std::is_arithmetic<T>::value, int> = 0>
auto sum_all(const std::vector<T>& nested_v)
{
    decltype(sum_all(nested_v[0])) sum = 0;

    for (auto& e : nested_v)
    {
        sum += sum_all(e);
    }

    return sum;
}

在coliru上看到


使用C ++ 17,您只能拥有一个功能(简洁!):

template <class T>
auto sum_all(const std::vector<T>& nested_v)
{
    innermost_type_t<T> sum = 0;

    for (auto& e : nested_v)
    {
        if constexpr(std::is_arithmetic<T>::value)
            sum += e;
        else
            sum += sum_all(e);
    }

    return sum;
}

innermost_type_t定义为:

template <class T> struct innermost_type
{
    using type = T;
};

template <class T> struct innermost_type<std::vector<T>>
{
    using type = typename innermost_type<T>::type;
};

template <class T>
using innermost_type_t = typename innermost_type<T>::type;

暂无
暂无

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

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