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

  • A+

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?


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.


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: