如果你想走这条路,你将需要以下构造:
ifstream::read(&vec[0],nFreq * sizeof(complex<float>))
std::complex
在预分配的内存上放置新的构造
vector<complex<float>> vec;
vec.reserve(nFreq); // remember to catch exception if allocation fails due to huge files
为了进一步“证明”(无法访问标准)您的放置和文件内容的有效性,请引用 cppreference.com:
For any complex number z,
reinterpret_cast<T(&)[2]>(z)[0] is the real part of z and
reinterpret_cast<T(&)[2]>(z)[1] is the imaginary part of z.
For any pointer to an element of an array of complex numbers p and
any valid array index i,
reinterpret_cast<T*>(p)[2*i] is the real part of the complex number p[i], and
reinterpret_cast<T*>(p)[2*i + 1] is the imaginary part of the complex number p[i]
这些要求实质上将 std::complex 的三个特化中的每一个的实现限制为声明两个且仅两个非静态数据成员,类型为 value_type,具有相同的成员访问权限,分别保存实部和虚部。