支离破碎的C++学习——std::complex
本文总字数:6419,阅读预计需要:16分钟
刚刚在看《C++语言导学》看到了complex库,还以为是什么新特性去向室友宣传,之后才知道是我学艺不精了。所以特意去看了cppreference和microsoft文档去学习了一下。以下简单记录一下个人想法
先贴一下gcc中的部分源码
template<typename _Tp>
struct complex
{
/// Value typedef.
typedef _Tp value_type;
/// Default constructor. First parameter is x, second parameter is y.
/// Unspecified parameters default to 0.
_GLIBCXX_CONSTEXPR complex(const _Tp& __r = _Tp(), const _Tp& __i = _Tp())
: _M_real(__r), _M_imag(__i) { }
// Let the compiler synthesize the copy constructor
#if __cplusplus >= 201103L
constexpr complex(const complex&) = default;
#endif
/// Converting constructor.
template<typename _Up>
_GLIBCXX_CONSTEXPR complex(const complex<_Up>& __z)
: _M_real(__z.real()), _M_imag(__z.imag()) { }
#if __cplusplus >= 201103L
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// DR 387. std::complex over-encapsulated.
_GLIBCXX_ABI_TAG_CXX11
constexpr _Tp
real() const { return _M_real; }
_GLIBCXX_ABI_TAG_CXX11
constexpr _Tp
imag() const { return _M_imag; }
#else
/// Return real part of complex number.
_Tp&
real() { return _M_real; }
/// Return real part of complex number.
const _Tp&
real() const { return _M_real; }
/// Return imaginary part of complex number.
_Tp&
imag() { return _M_imag; }
/// Return imaginary part of complex number.
const _Tp&
imag() const { return _M_imag; }
#endif
// _GLIBCXX_RESOLVE_LIB_DEFECTS
// DR 387. std::complex over-encapsulated.
void
real(_Tp __val) { _M_real = __val; }
void
imag(_Tp __val) { _M_imag = __val; }
/// Assign a scalar to this complex number.
complex<_Tp>& operator=(const _Tp&);
/// Add a scalar to this complex number.
// 26.2.5/1
complex<_Tp>&
operator+=(const _Tp& __t)
{
_M_real += __t;
return *this;
}
/// Subtract a scalar from this complex number.
// 26.2.5/3
complex<_Tp>&
operator-=(const _Tp& __t)
{
_M_real -= __t;
return *this;
}
/// Multiply this complex number by a scalar.
complex<_Tp>& operator*=(const _Tp&);
/// Divide this complex number by a scalar.
complex<_Tp>& operator/=(const _Tp&);
// Let the compiler synthesize the copy assignment operator
#if __cplusplus >= 201103L
complex& operator=(const complex&) = default;
#endif
/// Assign another complex number to this one.
template<typename _Up>
complex<_Tp>& operator=(const complex<_Up>&);
/// Add another complex number to this one.
template<typename _Up>
complex<_Tp>& operator+=(const complex<_Up>&);
/// Subtract another complex number from this one.
template<typename _Up>
complex<_Tp>& operator-=(const complex<_Up>&);
/// Multiply this complex number by another.
template<typename _Up>
complex<_Tp>& operator*=(const complex<_Up>&);
/// Divide this complex number by another.
template<typename _Up>
complex<_Tp>& operator/=(const complex<_Up>&);
_GLIBCXX_CONSTEXPR complex __rep() const
{ return *this; }
private:
_Tp _M_real;
_Tp _M_imag;
};
可以发现std::complex的value_type类型为_Tp。_M_real存放实数,_M_imag存放虚数。使用公有成员函数real和imag可以访问到。
公有成员函数还包括构造函数,赋值运算符,+=,-=,*=,/=等。
/// Return @a x.
template<typename _Tp>
inline complex<_Tp>
operator+(const complex<_Tp>& __x)
{ return __x; }
/// Return complex negation of @a x.
template<typename _Tp>
inline complex<_Tp>
operator-(const complex<_Tp>& __x)
{ return complex<_Tp>(-__x.real(), -__x.imag()); }
非成员函数有,有返回复数的值的重载运算符+、与取反的重载运算符-
//@{
/// Return new complex value @a x plus @a y.
template<typename _Tp>
inline complex<_Tp>
operator+(const complex<_Tp>& __x, const complex<_Tp>& __y)
{
complex<_Tp> __r = __x;
__r += __y;
return __r;
}
template<typename _Tp>
inline complex<_Tp>
operator+(const complex<_Tp>& __x, const _Tp& __y)
{
complex<_Tp> __r = __x;
__r += __y;
return __r;
}
template<typename _Tp>
inline complex<_Tp>
operator+(const _Tp& __x, const complex<_Tp>& __y)
{
complex<_Tp> __r = __y;
__r += __x;
return __r;
}
//@}
//@{
/// Return new complex value @a x minus @a y.
template<typename _Tp>
inline complex<_Tp>
operator-(const complex<_Tp>& __x, const complex<_Tp>& __y)
{
complex<_Tp> __r = __x;
__r -= __y;
return __r;
}
template<typename _Tp>
inline complex<_Tp>
operator-(const complex<_Tp>& __x, const _Tp& __y)
{
complex<_Tp> __r = __x;
__r -= __y;
return __r;
}
template<typename _Tp>
inline complex<_Tp>
operator-(const _Tp& __x, const complex<_Tp>& __y)
{
complex<_Tp> __r(__x, -__y.imag());
__r -= __y.real();
return __r;
}
//@}
//@{
/// Return new complex value @a x times @a y.
template<typename _Tp>
inline complex<_Tp>
operator*(const complex<_Tp>& __x, const complex<_Tp>& __y)
{
complex<_Tp> __r = __x;
__r *= __y;
return __r;
}
template<typename _Tp>
inline complex<_Tp>
operator*(const complex<_Tp>& __x, const _Tp& __y)
{
complex<_Tp> __r = __x;
__r *= __y;
return __r;
}
template<typename _Tp>
inline complex<_Tp>
operator*(const _Tp& __x, const complex<_Tp>& __y)
{
complex<_Tp> __r = __y;
__r *= __x;
return __r;
}
//@}
//@{
/// Return new complex value @a x divided by @a y.
template<typename _Tp>
inline complex<_Tp>
operator/(const complex<_Tp>& __x, const complex<_Tp>& __y)
{
complex<_Tp> __r = __x;
__r /= __y;
return __r;
}
template<typename _Tp>
inline complex<_Tp>
operator/(const complex<_Tp>& __x, const _Tp& __y)
{
complex<_Tp> __r = __x;
__r /= __y;
return __r;
}
template<typename _Tp>
inline complex<_Tp>
operator/(const _Tp& __x, const complex<_Tp>& __y)
{
complex<_Tp> __r = __x;
__r /= __y;
return __r;
}
//@}
非成员函数存在复数和标量,复数与复数之间进行运算的+、-、*、/的重载运算符。由于复数和标量类型不同,所以有复数和标量交换位置两种情况,加上复数和复数则4个重载运算符有12个重载式。
剩下的函数建议直接看文档或者stl源码std::complex - cppreference.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)