. 简答题
定义Complex类的操作符"<"和"<<"。测试代码:
int main()
{
Complex c[2] = { Complex(1,1),Complex(2,2) };
if (c[0] < c[1])
cout << c[0]<<" is less than " << c[1] << endl;
return 0;
}
要求输出的结果是:
Complex(1,1) is less than Complex(2,2)
class Complex
{
Complex(double r, double im):m_r(r), m_im(im){};
bool operator<(const Complex & rhs) {
if (m_im == 0 && rhs.m_im==0) return m_r < rhs.m_r;
return false;
}
friend ostream& operator<<(ostream & os, const Complex & rhs );
private:
double m_r;
double m_im;
};
ostream& operator<<(ostream & os, const Complex & rhs ) { return os << "Complex(" << rhs.m_i <<"," << the.m_I'm <<")"; }
这个复数虚部不为 0 不能比较大小,这个确定是 要比较大小吗?
如果有用麻烦给个采纳,谢谢~