[英]static const array of static const arrays of doubles
I managed to make an array of arrays of double
s, having all of these static
and const
: 我设法制作了一个
double
数组,并拥有所有这些static
和const
:
# myclass.h
class myclass {
static constexpr double I_1[] = {... , ... , ... ...};
static constexpr double I_2[] = {... , ... , ... ...};
static constexpr double I_3[] = {... , ... , ... ...};
...
static constexpr const double* list[] = {I_1,I_2,I_3,...};
}
Note that all arrays can have different sizes. 请注意,所有阵列可以具有不同的大小。
Now, this technique requires the definition of these static variables in the cpp
file: 现在,此技术需要在
cpp
文件中定义以下静态变量:
# myclass.cpp
constexpr const double* myclass::list[];
constexpr double myclass::I_1[], myclass::I_2[], myclass::I_3[],...
and this list can be very long if there are many arrays. 如果有很多数组,此列表可能会很长。 This seems a little bit complicated to me.
对我来说,这似乎有些复杂。
Is there a better way? 有没有更好的办法?
Bonus question: is there a way to do it without requiring C++11? 额外的问题: 有没有一种方法可以不需要C ++ 11?
This will provide what I believe OP is looking for. 这将提供我认为OP正在寻找的东西。
In a header somewhere and no C++11 required 在标头中,不需要C ++ 11
class ConfigDB
{
public:
static const std::vector<std::vector<double>> db;
};
The cpp file allocating ConfigDB::db requires c++11 for the nifty initializer list. 分配ConfigDB :: db的cpp文件需要c ++ 11作为漂亮的初始化程序列表。
const std::vector<std::vector<double>> ConfigDB::db
{
{1,2,3,4},
{5,6,7}
};
Non C++11 alternatives include loading all of the vector<double>
s and then loading those into the vector<vector<double>>
. 非C ++ 11的替代方法包括加载所有
vector<double>
,然后将它们加载到vector<vector<double>>
。 As bad or Worse than OP's solution with the arrays. 比OP的阵列解决方案差或差。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.