Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ float* array as reference to std::vector<float>

I want to create a std::vector<float> vpd which will be a reference to float*.

float * old = new float[6];
for (int i = 0; i < 6; i++)
{
    old[i] = i;
}
vector<float> vpd(6);
auto refasd = &*vpd.begin();
*refasd = *old;
vpd[0] = 23;
cout << old[0] << endl;

How should I modify the code, if I want get 23 from cout?

like image 432
Artur Domasik Avatar asked Dec 03 '22 19:12

Artur Domasik


1 Answers

You can't. std::vector is not designed to take ownership of a raw pointer.
Maybe you can make do with std::unique_ptr<float[]>, but the better solution is to directly use std::vector.

like image 56
Quentin Avatar answered Dec 22 '22 23:12

Quentin