重载运算符的实现与应用
来源:互联网 发布:mysql数据库用户名 编辑:程序博客网 时间:2024/06/16 21:10
重载运算符
一.背景
在进行整数运算时,我们经常会看到整数溢出的情况。如果运算结果很大,就会用到高精度。但是高精度能否像int一样简便地使用呢?
二.一些应用
重载运算符是借助于结构体来实现的。一般情况下为了对我们自定义的类型用sort函数进行排序,会对‘<’进行重载。然而在进行高精度是涉及到的就不仅仅是比较运算符‘<’还会涉及到赋值运算符‘>>’,‘=’,算术运算符‘+’,‘-’等。
struct Bignum
{
static const int mod=100000000;//这里定义了一个常量,实际上是用来做万进制优化的
static const int length=8;
vector<int> s;//用来节省内存
.......
};
三.具体实现
1.赋值运算符:
① ‘=’的重载(一般在结构体内部)
一般格式: 结构体名 operator = (const int num(可以任意))
或者用string赋值operator = (const string&num(可以任意))
需要注意的是:初始化和赋值是两种完全不同的概念,所以我们需要先在结构体内加上:Bignum(int num=0){*this=num;}//*this是指向当前对象的指针。
然后就是重载赋值符号:
Bignum operator = (int num)
{
s.clear();
do{
s.push_back(num%mod);
num /= mod;
}while(num>0);
return *this;
}
Bignum operator = (const string& str)
{
s.clear();
int x,len=(str.length()-1)/l+1;//加1为了防止有不足l位的情况。
for (int i=0;i<len;i++)
{
int t=str.length()-i*l;
int st=max(0,t-l);//防止到前面不足8位
sscanf(str.substr(st,t-st).c_str(),"%d",&x);//从后向前8位
s.push_back(x);
}
return *this;
}
‚‘<<’和‘>>’的重载(定义在结构体的外部)
格式:
ostream& operator << (ostream &out, const Bignum &x)
{
for (int i=x.s.size()-1;i>=0;i--)
{
cout<<x.s[i];
}
return out;
}
istream& operator >> (istream &in,Bignum &x)
{
string s;
in>>s;
x=s;
return in;
}
2.算术运算符(一般在结构体内部)
+:
Bignum operator + (const Bignum& x) const
{
Bignum c;
c.s.clear();//用来存放结果的变量。
int p=0;
for (int i=0; ;i++)
{
if (p==0 && i>=s.size()&&i>=x.s.size()) break;//停止相加的判断。
int l=p;
if (i<s.size()) l+=s[i];//避免了位数不足的情况。
if (i<x.s.size()) l+=x.s[i];
c.s.push_back(l % mod);
p=l/mod;
}
return c;
}
3.比较运算符(一般在结构体内部)
<:
bool operator < (const Bignum& x) const
{
if (s.size()>x.s.size()) return 0;
if (s.size()<x.s.size()) return 0;
for (int i=s.size()-1;i>=0;i--)//从后向前比较
if (s[i]!=x.s[i]) return s[i]<x.s[i];
return false;
}
可以用>号实现>,>=等其它运算符的重载。
四.总结
重载运算符不只应用于高精度,还广泛应用于自定义类型的排序中(比较运算符的重载)。希望灵活使用。
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<vector> using namespace std;int n,m;struct Bignum {static const int mod=100000000; static const int l=8; vector<int> s; Bignum(int num=0){*this=num;}Bignum operator = (int num){ s.clear(); do{ s.push_back(num%mod); num /= mod; }while(num>0); return *this;}Bignum operator = (const string& str){s.clear();int x,len=(str.length()-1)/l+1;for (int i=0;i<len;i++){int t=str.length()-i*l;int st=max(0,t-l);sscanf(str.substr(st,t-st).c_str(),"%d",&x);s.push_back(x);}return *this;}Bignum operator + (const Bignum& x) const {Bignum c;c.s.clear();int p=0;for (int i=0; ;i++){if (p==0 && i>=s.size()&&i>=x.s.size()) break;int l=p;if (i<s.size()) l+=s[i];if (i<x.s.size()) l+=x.s[i];c.s.push_back(l % mod);p=l/mod;}return c;}bool operator < (const Bignum& x) const{if (s.size()>x.s.size()) return 0;if (s.size()<x.s.size()) return 0;for (int i=s.size()-1;i>=0;i--) if (s[i]!=x.s[i]) return s[i]<x.s[i]; return false; }}a,b; ostream& operator << (ostream &out, const Bignum &x){for (int i=x.s.size()-1;i>=0;i--) { cout<<x.s[i]; } return out;} istream& operator >> (istream &in,Bignum &x){string s;in>>s;x=s;return in;}long long s,s1;string sss,sss1;int main(){cin>>a;cin>>b; if (a<b) cout<<"Y";return 0;}
- 重载运算符的实现与应用
- 十九、运算符重载(三)String类的改进实现、[]运算符重载、+运算符重载、+=运算符重载、<<运算符重载、>>运算符重载
- 重载运算符的应用1
- 运算符重载的应用:日期类
- ++与--运算符重载
- 矩阵类的实现,运算符重载
- C++实现的复数运算符重载
- 实现复数类的运算符重载
- 实现负数中的运算符的重载
- 实现Time类的运算符重载
- 实现复数类的运算符重载
- 实现TIME类的运算符重载
- 【c++】实现运算符的重载
- C++运算符重载的实现
- 运算符重载的实现代码
- ++运算符的重载前置与后置
- 继承与派生、运算符的重载、虚函数的应用
- C++之运算符的重载实现大数的运算
- linux下QT+MYSQL开发
- EDAS系统投稿
- linux上anaconda的卸载
- Java关键字final的理解
- c3p0-config.xml配置
- 重载运算符的实现与应用
- 手机锁屏时点亮屏幕(如闹钟和来电显示)
- map操作
- 蓝桥杯:历届试题 城市建设
- 小型电热锅电路分析
- 《第四次工业革命-转型的力量》
- 1602液晶按键输入显示
- HTTP协议详解
- 解决tomcat在eclipse中启动正常,但在登录主页时出现404错误的问题