在c++中获取指向复向量实部和虚部的指针
Getting pointers to the real and imaginary parts of a complex vector in C++
使用标准的c++复数和向量库,我定义了一个复数向量。现在,我想要得到(类型为double *
的)指向包含这个复向量的实部和虚部的向量的指针。下面的解决方案可以工作,但是不美观且浪费,因为内存使用量增加了一倍;
using namespace std;
typedef complex<double> cmp;
.
.
int i,m=10;
vector<cmp> C(m);
//Do something to populate the vector C
vector<double> R(m), I(m);
for(i=0; i<m; i++){
R[i] = C[i].real();
I[i] = C[i].imag();
}
double * r = &R[0];
double * i = &I[0];
根据c++标准
If z is an lvalue expression of type cv std::complex<T> then:
— the expression reinterpret_cast<cv T(&)[2]>(z) shall be well-formed,
— reinterpret_cast<cv T(&)[2]>(z)[0] shall designate the real part of z, and
— reinterpret_cast<cv T(&)[2]>(z)[1] shall designate the imaginary part of z.
Moreover, if a is an expression of type cv std::complex<T>* and the expression a[i] is well-defined
for an integer expression i, then:
— reinterpret_cast<cv T*>(a)[2*i] shall designate the real part of a[i], and
— reinterpret_cast<cv T*>(a)[2*i + 1] shall designate the imaginary part of a[i].
你可以直接写
using namespace std;
typedef complex<double> cmp;
.
.
int i,m=10;
vector<cmp> C(m);
//Do something to populate the vector C
double * r = &reinterpret_cast<double(&)[2]>( C[0] )[0];
double * i = &reinterpret_cast<double(&)[2]>( C[0] )[1];
下面是一个例子
#include <iostream>
#include <complex>
#include <vector>
int main()
{
std::vector<std::complex<double>> v( 1, { 1.1, 2.2 } );
double * r = &reinterpret_cast<double(&)[2]>( v[0] )[0];
double * i = &reinterpret_cast<double(&)[2]>( v[0] )[1];
std::cout << *r << 't' << *i << std::endl;
return 0;
}
输出为
1.1 2.2
(c++ 03)标准没有定义std::complex<double>
的内部是怎样的,但通常它由两个双精度数组成,实部在虚部之前。因此,给定一个std::complex<double>
的数组(或std::vector
),你不可能得到一个指向全实部数组的指针,而另一个指向全虚部数组的指针:实部和虚部是交错的。如果你真的需要拆分它们,你必须复制所有的元素(就像你已经做的那样)。
但是你为什么要把他们分开呢?把它们传递给某个库的一些例程?也许那个库也支持交错格式?在这种情况下,你可以做reinterpret_cast<double*>(&C[0])
。请注意,这是非标准的,但它似乎在大多数情况下都有效。有关更多信息,请参阅广泛使用的fftw-库的文档,其中推荐使用此方法。
如果性能是一个问题,你应该从一开始就把实部和虚部分开,而不是先构造一个std::complex<double>
向量。
相关文章:
- 如何将部分流作为参数传递
- 有没有办法修改'operator->',以便'z->im'返回复数的虚部
- 如何删除部分类?
- 如何只链接到柯南包中的部分库?
- 无法打印用户填充的部分数组的反面
- 使用类型特征的部分类专用化
- 将 CRTP 与部分类专用化结合使用?
- 为什么 C++17 标准没有带来部分类模板参数扣除?
- 是否可以保证输入<Type>实数时复杂对象的虚部设置为零?
- 如何在 Eigen3 库中高效提取复矩阵的实部/虚部?
- FFTW和OpenCV的C++接口,Mat输出中的实部和虚部
- 如何将实部和虚部放入一个变量中,就像在matlab中一样
- 在c++的复杂类中,为实部和虚部指定了多少位
- C++中的复杂数组 - 实部,想象部
- 复数的实部和虚部
- FFTW3:解释fftw_plan_r2c_1d输出并访问输出的虚部
- c++显示虚部为I的复数
- 在c++中获取指向复向量实部和虚部的指针
- 用C++中的[]运算符可以访问复数的实部和虚部吗
- C++复数的最高元素(最高实部)