c ++的operator的两种用法

operator,它有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换)。

 

1.operator overloading
C++可通过operator 重载操作符,格式如下:类型T operator 操作符 (),如比重载+,如下所示

 

 

2 operator casting

C++可通过operator 隐式转换,格式如下: operator 类型T (),T是要转换到的类型。如下所示

 

 

载入转换运算符函数!
100

 

  • 3
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
FFT(快速傅里叶变换)是一种高效的算法,可以在O(nlogn)时间复杂度下计算离散傅里叶变换(DFT)。虽然使用vector实现FFT是比较常见的方法,但也可以使用数组实现FFT。 下面给出一个使用数组实现FFT的C++代码: ```cpp #include <bits/stdc++.h> using namespace std; const double PI = acos(-1); struct Complex { double real, imag; Complex() {} Complex(double r, double i): real(r), imag(i) {} inline Complex operator+(const Complex &c) const { return Complex(real + c.real, imag + c.imag); } inline Complex operator-(const Complex &c) const { return Complex(real - c.real, imag - c.imag); } inline Complex operator*(const Complex &c) const { return Complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real); } }; void FFT(Complex *a, int n, int flag) { for (int i = 1, j = 0; i < n; i++) { for (int k = n; j ^= k >>= 1, ~j & k;); if (i < j) swap(a[i], a[j]); } for (int m = 2; m <= n; m <<= 1) { Complex wm(cos(2 * PI / m), flag * sin(2 * PI / m)); for (int k = 0; k < n; k += m) { Complex w(1, 0); for (int j = k; j < k + (m >> 1); j++, w = w * wm) { Complex u = a[j], t = w * a[j + (m >> 1)]; a[j] = u + t, a[j + (m >> 1)] = u - t; } } } if (flag == -1) for (int i = 0; i < n; i++) a[i].real /= n; } void Convolution(int *a, int na, int *b, int nb, int *res) { static Complex A[1 << 21], B[1 << 21]; int n = na + nb - 1, m = 1; while (m < n) m <<= 1; for (int i = 0; i < m; i++) { A[i] = i < na ? Complex(a[i], 0) : Complex(0, 0); B[i] = i < nb ? Complex(b[i], 0) : Complex(0, 0); } FFT(A, m, 1), FFT(B, m, 1); for (int i = 0; i < m; i++) A[i] = A[i] * B[i]; FFT(A, m, -1); for (int i = 0; i < n; i++) res[i] = (int)(A[i].real + 0.5); } int main() { int a[] = {1, 2, 3, 4}, b[] = {5, 6, 7}; int n = sizeof(a) / sizeof(int), m = sizeof(b) / sizeof(int); static int res[1 << 21]; Convolution(a, n, b, m, res); for (int i = 0; i < n + m - 1; i++) cout << res[i] << " "; return 0; } ``` 上述代码中,`FFT`函数实现了FFT的核心算法,`Convolution`函数实现了两个多项式的卷积。其中,`Complex`结构体表示复数,`FFT`函数的flag参数为1表示进行DFT,为-1表示进行IDFT。`Convolution`函数中使用`static`关键字定义了A和B数组,避免在函数栈上分配大量空间导致栈溢出。注意这里的n和m分别表示两个多项式的次数(不是系数个数),res数组的长度应该为n+m-1。 需要注意的是,这里的数组实现与vector实现类似,只是使用数组代替了vector,因此在实际应用中,可以根据自己的需要选择vector或数组来实现FFT。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值