簡體   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