开发者

How can I convert a std::vector<float> to a float array? [duplicate]

This question already has an开发者_JAVA技巧swers here: Closed 12 years ago.

Possible Duplicate:

Getting array from std:vector

I've been reading the answers to this question and found them useful, but how can I do the same with a float type?


The same thing.

std::vector<float> v(10);
float *p = &v[0];


In exactly the way econoclast demonstrated on the answer you quoted.

std::vector<float> v;
v.push_back(1.2);
v.push_back(3.4);

// &v[0] is a pointer to the first element of the vector.

float* array_sort_of = &v[0];
for (size_t i = 0; i < 2; i++) {
    std::cout << array_sort_of[i] << " ";
}

// Output: 1.2 3.4
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜