简体   繁体   English

从已知大小的double *数组构造std :: vector <double>

[英]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.

相关问题 错误“没有从 &#39;std::vector 进行的已知转换<double, std::allocator<double> &gt;&#39; 到 &#39;double *&#39; ” 使用 std::count_if() 时? - Error “ no known conversion from 'std::vector<double, std::allocator<double> >' to 'double *' ” when using std::count_if()? 如何获取向量中每个维度的大小<vector<array<double, 2> &gt;&gt; 构造? </vector<array<double,> - How to get the size of each dimension in a vector<vector<array<double, 2>>> construct? 为什么std :: array :: size constexpr具有简单类型(int,double,…),却没有std :: vector(GCC)? - Why is std::array::size constexpr with simple types (int, double, …) but not std::vector (GCC)? 使用与double数组的内存匹配的std :: vector - Work with a std::vector that matches the memory of an array of double 从std :: vector &lt;&gt;转换为双指针? - Converting from std::vector<> to a double pointer? 如何从std :: vector构造Platform :: Array - How to construct a Platform::Array from a std::vector std::vector 的零初始化<std::array<double,10> &gt; </std::array<double,10> - Zero initialization of std::vector<std::array<double,10>> 从double的std :: vector的std :: vector读取线程安全? - Thread safe read from a std::vector of std::tuple of double? 取消引用不适用于std :: vector <std::vector<double> &gt; - Dereferencing not working for std::vector<std::vector<double> > 分配给std :: vector <std::vector<double> &gt;并行 - Assigning to std::vector<std::vector<double>> in parallel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM