• 支离破碎的C++学习——std::complex


     刚刚在看《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

  • 相关阅读:
    centos 7 安装zabbix 4.0
    django Middleware
    初探paramiko
    python中的反射
    python异常处理
    双绞线
    简易的CMDB服务端
    4-初识Django Admin
    数据资产管理实践纲要
    matplotlib 散点图,为不同区域的点添加不同颜色
  • 原文地址:https://www.cnblogs.com/GodZhuan/p/14749286.html
Copyright © 2020-2023  润新知