我想编写一个 n 维直方图类。它应该是包含其他 bin 等的 bin 的形式,其中每个 bin 包含最小和最大范围,以及指向下一个维度 bin 的指针
bin 的定义如下
template<typename T>
class Bin {
float minRange, maxRange;
vector<Bin<either Bin or ObjectType>> bins;
}
这个定义是递归的。所以在运行时用户定义直方图的维度
so if its just 1-dimension, then
Bin<Obj>
while 3-dimensions
Bin<Bin<Bin<Obj>>>
这可能吗?
问候
最佳答案
当然,C++11 有可变长度的模板参数列表。即使没有 C++11,您也可以使用特化,如果您的所有维度都具有相同的类型:
template <typename T, unsigned nest>
struct Bin {
std::vector<Bin<T, (nest-1)> > bins;
};
template <typename T>
struct Bin<T,0> {
T content;
};
您只能在一定程度上指定运行时的维度。如果它受固定值约束,您甚至可以动态选择合适的类型。但是,请考虑使用一维 vector 而不是多维锯齿状 vector !
关于c++ - 定义一个深度/维度未知的 C++ 模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10963552/