向量向量向量的总和..的整数
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;
}
}
但是,这种情况正在发生:
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;
相关文章:
- C++:如何循环通过向量中的整数元素
- 添加存储在向量中的大整数的函数出现问题
- C ++中的StringStream有助于使用向量从字符串中提取逗号分隔的整数,而不是空格分隔的整数,为什么?
- 使用英特尔内联函数将打包的 8 位整数乘以浮点数向量
- C++ 如何比较n个排序的整数向量以找到互元素?
- 必须首先打印向量 v1 中最接近整数 x 的数字<int>
- 如何将整数字符串转换为整数的二维向量?
- 如何将整数向量值插入字符向量
- 如何使用 STL 算法将整数向量转换为字符串向量?
- 使用192/256位整数求和无符号64位整数向量的点积的最快方法
- 查找多个向量之间的公共元素(无整数元素)
- 如何将整数向量插入到键中,标准::映射的值
- 整数对的向量转化为整数向量的向量
- 如何组合两个整数向量
- C++中整数向量的序列化/反序列化
- 如何从C++中的值向量构造整数<bool>值
- 使用整数初始化列表初始化长双精度的向量
- 将向量整数拆分为这些数字的整数
- 如何访问映射的向量(整数和字符串的向量)
- 将带有时间戳的向量整数连接为C++中的字符串