[英]Construct std::vector<double> from a double* array of known size
I have a pointer to the first element of a double array: double* p
. 我有一个指向double数组的第一个元素的指针: double* p
。
I also know the length of the array: n
. 我也知道数组的长度: n
。
Is there a way I can copy this into a std::vector
? 有没有办法可以将它复制到std::vector
? Preferably using a constructor and allocator. 最好使用构造函数和分配器。 I'd like to do something like 我想做点什么
std::vector<double> v = std::vector(p, n);
but can't figure out which constructor is closest to this. 但无法弄清楚哪个构造函数最接近于此。 I'm using C++11. 我正在使用C ++ 11。
std::vector<>
has a constructor that takes a range. std::vector<>
有一个带范围的构造函数。 You can use it like this: 你可以像这样使用它:
std::vector<double> v(p, p + n);
Try 尝试
std::vector<double> v( p, p + n );
Or if the vector was already defined then 或者如果已经定义了矢量那么
v.assign( p, p + n );
If you want to append the vector then 如果你想附加矢量那么
v.insert( v.end(), p, p + n );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.