convert from float* to vector<float >

僤鯓⒐⒋嵵緔 提交于 2019-12-05 19:26:45

Of course you can not generally convert a pointer to a vector, they are different things. If however the pointer holds the address of the first element of a C-style array of known length, you can create a vector with the same contents as the array like:

std::vector<float> my_vector {arr, arr + arr_length};

where arr is said pointer and arr_length is the length of the array. You can then pass the vector to the function expecting std::vector<float>&.

If you look at e.g. this std::vector constructor reference, you will see a constructor which takes two iterators (alternative 4 in the linked reference). This constructor can be used to construct a vector from another container, including arrays.

For example:

float* pf = new float[SOME_SIZE];
// Initialize the newly allocated memory

std::vector<float> vf{pf, pf + SOME_SIZE};
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!