How to convert vector to array

后端 未结 10 1540
野的像风
野的像风 2020-11-22 09:20

How do I convert a std::vector to a double array[]?

10条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 09:44

    What for? You need to clarify: Do you need a pointer to the first element of an array, or an array?

    If you're calling an API function that expects the former, you can do do_something(&v[0], v.size()), where v is a vector of doubles. The elements of a vector are contiguous.

    Otherwise, you just have to copy each element:

    double arr[100];
    std::copy(v.begin(), v.end(), arr);
    

    Ensure not only thar arr is big enough, but that arr gets filled up, or you have uninitialized values.

提交回复
热议问题