如何将std :: vector <float>转换为float数组?</float>

时间:2011-01-21 20:42:26

标签: c++ arrays vector

  

可能重复:
  Getting array from std:vector

我一直在阅读this question的答案并发现它们很有用,但我怎么能用浮点型做同样的事情呢?

2 个答案:

答案 0 :(得分:9)

同样的事情。

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

答案 1 :(得分:2)

正如econoclast在你所引用的答案中所展示的那样。

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