作者khoguan (Khoguan Phuann)
看板C_and_CPP
标题Re: [问题] C++复数的初始值设定
时间Fri Sep 23 01:07:30 2005
※ 引述《cation234 (猫离子)》之铭言:
: 在下找到了这个 说明了为什麽会有这种问题
:
: http://www-h.eng.cam.ac.uk/help/tpl/languages/C++/complex.html
那里面有几句话不对。
"In fact, c2=(3,7) is equivalent to c2=3,7. C++'s "," operator
suppresses the value of the first operand in favour of the second,
so c2=3,7 is equivalent to c2=7."
当写成 c2=(3,7); 时, c2 会变成 7, 而 3 是多余的,没效果。
但写成 c2=3,7; 时, c2 会变成 3, 而 7 是多余的,没效果。
请自行实验看看。
至於其道理,则是 comma operator 比 assignment operator 的优先权
还要低。 c2=3,7; 等同於 (c2=3), 7;
: : : 以下是小弟的code, 简单的测试C++ STL的复数资料型态:
: : : #include <iostream>
: : : #include <complex>
: : : using namespace std;
: : : int main()
: : : {
: : : complex <double> x = (0.1, 0.2);
: : ~~~~~~~~~~~这里的结果是0.2
: : : complex <double> mu = (0.2, 0.6);
: : ~~~~~~~~~~~这个是0.6
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 220.130.208.168