如何从两个向量(实数和图像)获取复数向量
我有两个浮点数向量,我希望它们成为一个复数向量。 我被困住了。 我不介意使用迭代器,但我确信它会重新发现我不知道的轮子。 我的代码是否引导我走向正确的方向?
typedef std::vector<float> CVFloat;
CVFloat vA, vB;
//fil vectors
typedef std::complex<CVFloat> myComplexVector;
myComplexVector* vA_Complex = new myComplexVector(vA, vB);
上面的代码正确地通过了编译器,但是当我想使用迭代器从 myComplexVector 获取单个数字时,我收到错误“未定义符号'const_iterator'”(Borland C++)
myComplexVector::const_iterator it = vA_Complex->begin();
I have two vectors of floats and i want them to become one vector of Complex numbers. I'm stuck. I don't mind using iterators, but i am sure it'd be rediscovering the wheel i'm not informed about. Is my code leading me in the right direction?
typedef std::vector<float> CVFloat;
CVFloat vA, vB;
//fil vectors
typedef std::complex<CVFloat> myComplexVector;
myComplexVector* vA_Complex = new myComplexVector(vA, vB);
The code above is going through the compiler correctly, but when i want to get single numbers from myComplexVector using iterator i get error "Undefined symbol 'const_iterator'" (Borland C++)
myComplexVector::const_iterator it = vA_Complex->begin();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在这里,您正在创建一个“复杂”对象,其实部和虚部是浮点向量。
也许您真正想做的是创建一个复杂对象的向量,其实部和虚部都是浮点数?
编辑: myComplexVector 不是向量,而是复数。 这就是为什么没有定义它的
const_iterator
的原因。Here you are creating a "complex" object whose real and imaginary parts are vectors of floats.
Maybe what you actually want to do is creating a vector of complex objects whose real and imaginary parts are floats?
EDIT: myComplexVector is not a vector, is a complex. That's why a
const_iterator
for it is not defined.为什么不做得更容易呢?
Whay not do it much much easier?
最简单的方法就是编写循环
Edit: ... 并按照 Neil 的建议正确声明 myComplexVector 类型。
The easiest way is just write the loop
Edit: ... and follow Neil's advice to declare myComplexVector type properly.
您可以创建一个通用的“zip”函数,将迭代器带到两个向量、转换器函子和输出迭代器:
我希望 STL 中有这样一个函数......
You can create a general "zip" function taking iterators to both vectors, and a convertor functor and an output iterator:
And I wish there were such a function in the STL...
这没有任何意义:
你的意思肯定是
或者类似的东西吗?
一旦你拥有它,你可以简单地迭代浮点向量(假设它们包含匹配的值)并使用push_back()添加到你的复杂向量中:
This doesn't make any sense:
surely you mean
or something similar?
Once ou have it you can simply iterate over the float vectors (assuming they contain matching values) and add to your complex vector using push_back():
复数只是一对两个实数
a
和b
,它们表示复数a+bi
。 你到底想用这两个向量做什么?A complex number is simply a pair of two real numbers
a
andb
which denote the complex numbera+bi
. What exactly are you trying to do with the two vectors?我理解你的问题,你想将实部向量与虚部向量组合成复数向量。
std::complex
有一个模板参数,可让您选择复数各部分的数字表示(即,如果您想要基于double
或float 的复数值
甚至一些自定义数字类型...)。 然后,复杂类型根据基础类型定义基本复杂代数。在您的代码中,您试图基于浮点数向量构造一个复数类型(即实部和虚部为向量的单个复数值),这显然是错误的。 相反,您需要一个 float 类型的复数向量,
您必须执行以下操作:
备注:
在大多数情况下,无需创建容器例如堆上的向量(即使用
new
) 尽量避免这种情况。不幸的是,C++ 标准库不包含组合迭代器(即“自动”组合两个序列的迭代器),这将允许更优雅的解决方案(请参阅 Boost Zip 迭代器 的总体思路)。
I understand your question that you want to combine a vector of real parts with a vector of imaginary parts into a vector of complex numbers.
std::complex
has one template parameter which lets you chose the numerical represenation of the parts of the complex (i.e. if you want complex values based ondouble
orfloat
or even some custom number type...). The complex type then defines basic complex algebra in terms of the underlying type.In your code you are trying to construct a complex type based on a vector of floats (i.e. a single complex value having a real and imaginary part being a vector), which is obviously wrong. Instead you want a vector of complex numbers of type float
You'd have to do something like:
Remarks:
In most cases it isn't necessary to create containers such as vectors on the heap (i.e. using
new
) Try to avoid this.Unfortunately the C++ standard library doesnt contain a combining iterator (i.e. one that "automatically" combines two sequences) which would allow a more elegant solution (see Boost Zip iterator for a general idea).