模板与重载运算符

c++的模板和重载运算符可以大大减少代码量,而且速度较快。实例:

#include<iostream>
#include<algorithm>
using namespace std;
struct point{
	int x,y;
	point(int x=0,int y=0):x(x),y(y){}
};
point operator +(const point &a,const point&b)
{
	return point(a.x+b.x,a.y+b.y);
}
ostream& operator << (ostream &out,const point&p)
{
	out<<"("<<p.x<<","<<p.y<<")";
	return out;
}
template<typename T>
T sum(T *begin,T *end){
	T *p=begin;
	T ans=0;
	for(T *p=begin;p!=end;p++)
	{
		ans=ans+*p;
	}
	return ans;
}
int main()
{
	double a[10]={1,2,3,4,5,5,6,7,8,9};
	point b[]={point(0,1),point(2,3),point(3,4)};
	cout<<sum(a+2,a+5)<<endl;
	cout<<sum(b,b+3)<<endl;
}
一个c++小测试,也可以试着把point也变成int和double通用的模板

尝试:

#include<iostream>
#include<algorithm>
using namespace std;
template<typename T>
struct point{
	T x,y;
	point(T x=0,T y=0):x(x),y(y){}
};
point operator +(const point &a,const point&b)
{
	return point(a.x+b.x,a.y+b.y);
}
ostream& operator << (ostream &out,const point&p)
{
	out<<"("<<p.x<<","<<p.y<<")";
	return out;
}
template<typename T>
T sum(T *begin,T *end){
	T *p=begin;
	T ans=0;
	for(T *p=begin;p!=end;p++)
	{
		ans=ans+*p;
	}
	return ans;
}
int main()
{
	point<int> a(1,2),b(2,3);
	point<double> c(2.333,6.666);
	cout<<a+b<<endl;
	cout<<a+c<<endl;
	cout<<b+c<<endl;
}
然后发现报错了2333,原来是定义过模板的point使用不能像原来那么随意了

#include<iostream>
#include<algorithm>
using namespace std;
template<typename T>
struct point{
	T x,y;
	point(T x=0,T y=0):x(x),y(y){}
};
template<typename T>
point<T> operator +(const point<T> &a,const point<T> &b)
{
	return point<T>(a.x+b.x,a.y+b.y);
}
template<typename T>
ostream& operator << (ostream &out,const point<T>&p)
{
	out<<"("<<p.x<<","<<p.y<<")";
	return out;
}
template<typename T>
T sum(T *begin,T *end){
	T *p=begin;
	T ans=0;
	for(T *p=begin;p!=end;p++)
	{
		ans=ans+*p;
	}
	return ans;
}
int main()
{
	point<int> a(1,2),b(2,3);
	point<double> c(2.333,6.666),d(1.23,2.22);
	cout<<a+b<<endl;
	//cout<<a+c<<endl;
	cout<<d+c<<endl;
}
注释那个地方是运行不了的,模板的特性可以说是同类无敌,跨类较难吧,比如int型和double型,脑洞大了倒是补了一下

#include<iostream>
#include<algorithm>
using namespace std;
template<typename T>
struct point{
	T x,y;
	point(T x=0,T y=0):x(x),y(y){}
};
template<typename T,typename K>
point<K> operator +(const point<T> &a,const point<K> &b)
{
	return point<K>(a.x+b.x,a.y+b.y);
}
template<typename T>
ostream& operator << (ostream &out,const point<T>&p)
{
	out<<"("<<p.x<<","<<p.y<<")";
	return out;
}
template<typename T>
T sum(T *begin,T *end){
	T *p=begin;
	T ans=0;
	for(T *p=begin;p!=end;p++)
	{
		ans=ans+*p;
	}
	return ans;
}
int main()
{
	point<int> a(1,2),b(2,3);
	point<double> c(2.333,6.666),d(1.23,2.22);
	cout<<a+b<<endl;
	cout<<a+c<<endl;
	cout<<d+c<<endl;
}

这回倒是可以正常求解了,可是这是人为的2333.。。

### C++ 类模板运算符重载的实现方法 在C++中,类模板可以用于创建通用的数据结构或算法框架。当需要对这些类模板中的对象执行特定操作时,可以通过运算符重载来简化语法并提高可读性。以下是关于如何在类模板中实现运算符重载的具体方法。 #### 1. 友元函数方式 如果需要访问类的私有成员,则可以选择通过友元函数的方式来实现运算符重载。这种方式适用于一些特殊的二元运算符(如`<<`, `>>`),它们通常不会作为类的成员函数存在。以下是一个基于引用的例子: ```cpp #include <iostream> using namespace std; template<class T> class Queue { private: T *base; int front, rear, Size; public: Queue(int n = 10); ~Queue(); bool enQueue(T elem); bool deQueue(T &elem); template<class U> friend ostream& operator<<(ostream &out, const Queue<U> &q); // 声明友元函数 }; // 定义友元函数 template<class T> ostream& operator<<(ostream &out, const Queue<T> &q) { if(q.front == q.rear && !q.Size){ out << "Empty queue." << endl; return out; } int index = q.front; while(index != q.rear){ out << q.base[index] << " "; index = (index + 1) % q.Size; } out << endl; return out; } // 构造函数和其他功能省略... ``` 此代码片段展示了如何利用友元函数完成队列类模板的流插入运算符重载[^1]。 #### 2. 成员函数方式 对于一元运算符或者那些自然地作用于单个对象上的二元运算符来说,将其定义为类的成员函数更为合适。下面展示了一个向量类模板重载下标运算符(`[]`)的例子: ```cpp #pragma once #include <iostream> template<typename T> class Vector { public: explicit Vector(int size = 128):m_len(size), m_base(new T[size]){} ~Vector(){ delete [] m_base;} T& operator[](int index)const{ if(index >= m_len || index <0 ){ throw std::out_of_range("Index Out Of Range"); } return m_base[index]; } private: int m_len; T* m_base; }; ``` 在这个例子中,我们看到`operator[]`被定义为了一个常量成员函数,并且它返回的是指向内部存储位置的一个引用,允许修改所获取到的内容[^2]。 #### 3. 复杂类型的乘法运算符重载 有时候我们需要处理更复杂的逻辑,比如两个复数相乘的情况。这时我们可以借助友元函数再次实现这一目标: ```cpp #include <iostream> using namespace std; template <class T> class Complex { T re, im; public: Complex(T r=0,T i=0):re(r),im(i){} template <class T1> friend Complex<T1> operator*(const Complex<T1>& lhs,const Complex<T1>& rhs); void display(){ cout<<"Complex Number:"<<re<<"+i"<<im<<"\n"; } }; template <class T1> Complex<T1> operator*(const Complex<T1>& lhs,const Complex<T1>& rhs){ return Complex<T1>(lhs.re*rhs.re-lhs.im*rhs.im,lhs.re*rhs.im+lhs.im*rhs.re); } int main(){ Complex<int>c1(3,4),c2(5,-6); auto result=c1*c2; result.display(); } ``` 这里实现了两个整型复数之间的乘积计算过程[^3]。 #### 总结 以上分别介绍了三种不同场景下的C++类模板运算符重载技术——通过友元函数支持外部调用;作为成员函数提供直观语义表达能力;以及针对复杂业务需求设计自定义行为模式。每种方式都有其适用范围,在实际开发过程中应根据具体情况进行选择应用。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值