[英]Initialize a struct which contains vector
I'm looking for a method for initializing a complex struct which contains vector in a single row. 我正在寻找一种初始化复杂结构的方法,该结构在一行中包含向量。
For example if I have this structure: 例如,如果我有这个结构:
struct A{
double x;
double y;
};
struct B{
double z;
double W;
};
struct C{
A a;
B b;
};
I can initialize C in this way: C c = {{0.0,0.1},{1.0,1.1}};
我可以用这种方式初始化C:
C c = {{0.0,0.1},{1.0,1.1}};
But what If I have to initialize a struct like this? 但是如果我必须初始化这样的结构呢?
struct A{
double x;
double y;
};
struct B{
vector<double> values;
};
struct C{
A a;
B b;
};
I have to do it in a single row because I want to allow the users of my application to specify in a single field all the initial values. 我必须在一行中完成它,因为我想允许我的应用程序的用户在单个字段中指定所有初始值。 And of course, I would prefer a standard way to do it and not a custom one.
当然,我更愿意采用标准方式来做而不是自定义方式。
Thanks! 谢谢!
您可以在C ++中以非常类似的方式初始化C
到C:
C c = {{0.3, 0.01}, {{14.2, 18.1, 0.0, 3.2}}};
如果你没有使用C ++ 11(所以你不能使用Mankarse的建议),boost可以帮助:
C c = { {0.1, 0.2}, {boost::assign::list_of(1.0)(2.0)(3.0)} };
You could do this: 你可以这样做:
C obj = { { 0.0, 0.1f }, std::vector<double>( 2, 0.0 ) };
But obviously it's not ideal since all the values in your vector need to be the same. 但显然它并不理想,因为矢量中的所有值都必须相同。
The question is how to initialize B in the second example? 问题是如何在第二个例子中初始化B?
You cannot initialize a vector like this: 你无法初始化这样的矢量:
std::vector<int> v = { 1, 2, 3 }; // doesn't work
You can do this 你可以这样做
int data[] = { 1, 2, 3 };
std::vector<int> v( &data[0], &data[0]+sizeof(data)/sizeof(data[0]));
(there are nicer ways to do that). (有更好的方法可以做到这一点)。 That isn't a "one-liner" though.
然而,这不是“一线”。
This works and I have tested it: 这有效,我测试了它:
template< typename T >
struct vector_builder
{
mutable std::vector<T> v;
operator std::vector<T>() const { return v; }
};
template< typename T, typename U >
vector_builder<T>& operator,(vector_builder<T>& vb, U const & u )
{
vb.v.push_back( u );
return vb;
}
struct B
{
vector<double> v;
};
B b = { (vector_builder(),1.1,2.2,3.3,4.4) };
C c = { {1.1, 2.2 }, {(vector_builder(),1.1,2.2,3.3,4.4)} };
You'll just have to use one of the constructors if you don't have the new C++11 initialization syntax. 如果您没有新的C ++ 11初始化语法,则只需使用其中一个构造函数。
C c = {{1.0, 1.0}, {std::vector<double>(100)}};
The vector above has 100 elements. 上面的矢量有100个元素。 There's no way to initialize each element uniquely in a vector's construction.
没有办法在向量的构造中唯一地初始化每个元素。
You could do... 你可以......
double values[] = {1, 2, 3, 4, 5};
C c = {{1.0, 1.0}, {std::vector<double>(values, values+5)}};
or if you always know the number of elements use a std::array which you can initialize the way you want. 或者如果你总是知道元素的数量使用std :: array,你可以初始化你想要的方式。
struct C {
A a;
B b;
C(double ta[2], vector<double> tb) {
a = A(ta[0],ta[1]); // or a.x = ta[0]; a.y = ta[1]
b = tb;
}
};
then you can initialize with something like 那么你可以用类似的东西进行初始化
C c = C( {0.0, 0.1} , vector<double> bb(0.0,5) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.