How to cheaply assign C-style array to std::vector?

后端 未结 4 1417
[愿得一人]
[愿得一人] 2020-12-01 16:13

Currently I do the following:

// float *c_array = new float[1024];

void Foo::foo(float *c_array, size_t c_array_size) {
  //std::vector cpp_arr         


        
4条回答
  •  臣服心动
    2020-12-01 16:38

    Unlikely it's possible - it's quite dangerous, because std::vector doesn't know how the memory was allocated and how it should be freed.

    If it's possible, you may replace original allocation with creation of std::vector of correct size. It uses contiguous memory area, so it can replace manually allocated buffer.

提交回复
热议问题