complex::complex
建構具有指定實數與虛數部分或作為其他一些複數複本的複數。
constexpr complex(
const T& RealVal = 0,
const T& ImagVal = 0
);
constexpr complex( const complex& );
template<class Other>
constexpr complex(
const complex<Other>& ComplexNum
);
參數
RealVal
用於初始化建構中複數之實數部分的值。ImagVal
用於初始化建構中複數之虛數部分的值。ComplexNum
其實數及虛數用於初始化建構中之複數的複數。
備註
第一個建構函式將儲存的實數部分初始化至 RealVal 並將儲存的虛數部分初始化至 Imagval。 第二個建構函式將儲存的實數部分初始化至 ComplexNum**.real**() 並將儲存的虛數部分初始化至 ComplexNum**.imag**()。
在此實作中,若轉譯器不支援成員樣板函式,此樣板:
template<class Other>
complex(const complex<Other>& right);
取代為:
complex(const complex& right);
此為複製建構函式。
範例
// complex_complex.cpp
// compile with: /EHsc
#include <complex>
#include <iostream>
int main( )
{
using namespace std;
double pi = 3.14159265359;
// The first constructor specifies real & imaginary parts
complex <double> c1 ( 4.0 , 5.0 );
cout << "Specifying initial real & imaginary parts,"
<< "c1 = " << c1 << endl;
// The second constructor initializes values of the real &
// imaginary parts using those of another complex number
complex <double> c2 ( c1 );
cout << "Initializing with the real and imaginary parts of c1,"
<< " c2 = " << c2 << endl;
// Complex numbers can be initialized in polar form
// but will be stored in Cartesian form
complex <double> c3 ( polar ( sqrt( (double)8 ) , pi / 4 ) );
cout << "c3 = polar ( sqrt ( 8 ) , pi / 4 ) = " << c3 << endl;
// The modulus and argument of a complex number can be recovered
double absc3 = abs ( c3 );
double argc3 = arg ( c3 );
cout << "The modulus of c3 is recovered from c3 using: abs ( c3 ) = "
<< absc3 << endl;
cout << "Argument of c3 is recovered from c3 using:\n arg ( c3 ) = "
<< argc3 << " radians, which is " << argc3 * 180 / pi
<< " degrees." << endl;
}
輸出
Specifying initial real & imaginary parts,c1 = (4,5)
Initializing with the real and imaginary parts of c1, c2 = (4,5)
c3 = polar ( sqrt ( 8 ) , pi / 4 ) = (2,2)
The modulus of c3 is recovered from c3 using: abs ( c3 ) = 2.82843
Argument of c3 is recovered from c3 using:
arg ( c3 ) = 0.785398 radians, which is 45 degrees.
需求
標頭: <complex>
命名空間: std