向量向量向量的总和..的整数

Sum of vector of vectors of vectors ... of integers

本文关键字:向量 整数      更新时间:2023-10-16

我正在尝试对向量向量的所有元素求和......整数:类似于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;
    }
}

但是,这种情况正在发生:

CMakeFilestest.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]: *** [CMakeFilestestbuild.make:141: test.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFilesMakefile2:67: CMakeFiles/test.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFilesMakefile2:79: CMakeFiles/test.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: test] Error 2

我真的不知道为什么...

提前谢谢。

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

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

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

另一个不相关的建议是通过常量引用传递向量。目前,您按值传递它们,如果向量很大,这可能会很昂贵。

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

您可以使用

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;
}

在科里鲁上看到它


使用 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;