How do I convert a std::vector<double>
to a double array[]
?
如何将std::vector
9 个解决方案
#1
431
There's a fairly simple trick to do so, since the spec now guarantees vectors store their elements contiguously:
这里有一个相当简单的技巧,因为规范现在保证向量连续地存储它们的元素:
std::vector<double> v;
double* a = &v[0];
#2
105
What for? You need to clarify: Do you need a pointer to the first element of an array, or an array?
对什么?您需要澄清:需要指向数组的第一个元素的指针还是数组的指针?
If you're calling an API function that expects the former, you can do do_something(&v[0], v.size())
, where v
is a vector of double
s. The elements of a vector are contiguous.
如果您正在调用一个期望使用前者的API函数,您可以使用do_something(&v[0], v.size()),其中v是一个双精度向量。向量的元素是连续的。
Otherwise, you just have to copy each element:
否则,您只需复制每个元素:
double arr[100];
std::copy(v.begin(), v.end(), arr);
Ensure not only thar arr
is big enough, but that arr
gets filled up, or you have uninitialized values.
确保不仅thar arr足够大,而且arr被填满,或者您有未初始化的值。
#4
15
vector<double> thevector;
//...
double *thearray = &thevector[0];
This is guaranteed to work by the standard, however there are some caveats: in particular take care to only use thearray
while thevector
is in scope.
这是标准的保证,但是有一些注意事项:特别要注意的是,在vector在范围内时,只使用thearray。
#5
13
Vectors effectively are arrays under the skin. If you have a function:
有效的向量是皮肤下的阵列。如果你有一个函数:
void f( double a[]);
you can call it like this:
你可以这样称呼它:
vector <double> v;
v.push_back( 1.23 )
f( &v[0] );
You should not ever need to convert a vector into an actual array instance.
您不应该需要将向量转换为实际的数组实例。
#6
1
If you have a function, then you probably need this:foo(&array[0], array.size());
. If you managed to get into a situation where you need an array then you need to refactor, vectors are basically extended arrays, you should always use them.
如果您有一个函数,那么您可能需要这个:foo(&array[0], array.size();如果你想要进入一个需要一个数组的情况,那么你需要重构,向量基本上是扩展数组,你应该经常使用它们。
#7
1
We can do this using data() method. C++11 provides this method.
我们可以使用data()方法来实现这一点。c++ 11提供这种方法。
Code Snippet
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
vector<int>v = {7, 8, 9, 10, 11};
int *arr = v.data();
for(int i=0; i<v.size(); i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
#8
1
std::vector<double> vec;
double* arr = vec.data();
#9
0
You can do some what like this
你可以这样做
vector <int> id;
vector <double> v;
if(id.size() > 0)
{
for(int i = 0; i < id.size(); i++)
{
for(int j = 0; j < id.size(); j++)
{
double x = v[i][j];
cout << x << endl;
}
}
}
#1
431
There's a fairly simple trick to do so, since the spec now guarantees vectors store their elements contiguously:
这里有一个相当简单的技巧,因为规范现在保证向量连续地存储它们的元素:
std::vector<double> v;
double* a = &v[0];
#2
105
What for? You need to clarify: Do you need a pointer to the first element of an array, or an array?
对什么?您需要澄清:需要指向数组的第一个元素的指针还是数组的指针?
If you're calling an API function that expects the former, you can do do_something(&v[0], v.size())
, where v
is a vector of double
s. The elements of a vector are contiguous.
如果您正在调用一个期望使用前者的API函数,您可以使用do_something(&v[0], v.size()),其中v是一个双精度向量。向量的元素是连续的。
Otherwise, you just have to copy each element:
否则,您只需复制每个元素:
double arr[100];
std::copy(v.begin(), v.end(), arr);
Ensure not only thar arr
is big enough, but that arr
gets filled up, or you have uninitialized values.
确保不仅thar arr足够大,而且arr被填满,或者您有未初始化的值。
#3
#4
15
vector<double> thevector;
//...
double *thearray = &thevector[0];
This is guaranteed to work by the standard, however there are some caveats: in particular take care to only use thearray
while thevector
is in scope.
这是标准的保证,但是有一些注意事项:特别要注意的是,在vector在范围内时,只使用thearray。
#5
13
Vectors effectively are arrays under the skin. If you have a function:
有效的向量是皮肤下的阵列。如果你有一个函数:
void f( double a[]);
you can call it like this:
你可以这样称呼它:
vector <double> v;
v.push_back( 1.23 )
f( &v[0] );
You should not ever need to convert a vector into an actual array instance.
您不应该需要将向量转换为实际的数组实例。
#6
1
If you have a function, then you probably need this:foo(&array[0], array.size());
. If you managed to get into a situation where you need an array then you need to refactor, vectors are basically extended arrays, you should always use them.
如果您有一个函数,那么您可能需要这个:foo(&array[0], array.size();如果你想要进入一个需要一个数组的情况,那么你需要重构,向量基本上是扩展数组,你应该经常使用它们。
#7
1
We can do this using data() method. C++11 provides this method.
我们可以使用data()方法来实现这一点。c++ 11提供这种方法。
Code Snippet
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
vector<int>v = {7, 8, 9, 10, 11};
int *arr = v.data();
for(int i=0; i<v.size(); i++)
{
cout<<arr[i]<<" ";
}
return 0;
}
#8
1
std::vector<double> vec;
double* arr = vec.data();
#9
0
You can do some what like this
你可以这样做
vector <int> id;
vector <double> v;
if(id.size() > 0)
{
for(int i = 0; i < id.size(); i++)
{
for(int j = 0; j < id.size(); j++)
{
double x = v[i][j];
cout << x << endl;
}
}
}