0
点赞
收藏
分享

微信扫一扫

自定义运算符与仿函数

天悦哥 2023-05-30 阅读 49


如何定义运算符什么是仿函数

#include<iostream>
#include<ostream>
#include<map> 
#include<set>
using namespace std;
struct node{
	int x;
	node(int x=0):x(x){
	};
};
struct S{
	private:
		int a,b;
	public:
		S(int a=0,int b=0):a(a),b(b){
		};
		int operator ^ (int){
			if(a==b)return 0;
			return 1;
		}
};
bool operator == (const node& a,const node& b){
	if(a.x==b.x){
		return true;
	}
	return false;
} 
node operator ^ (const node& a,const node& b){
	return a;
}
node operator + (node &a,node &b){
	if(a==b)return node(0);
	return node(1);
} 
ostream& operator << (ostream& out,const node& c){
	out<<"变量的值是"<<c.x<<endl;
	return out;
}
bool operator < (const node &a,const node& b){
	if(a.x<b.x)return true;
	return false;
}
bool operator > (const node& a,const node& b){
	if(!(a<b||a==b))return true;
	return false;
}
bool operator != (node& a,node& b){
	if(a==b)return false;
	return true;
}
node& operator ++ (node& a){
	a.x=a.x+1;
	return a;
}
struct cmp{
	bool operator () (const node& a,const node& b){
		return a>b;
	}
};
int main(){
	set<node> sc;
	map<node,int,cmp> mp;
	node a(22),b(24);
	cout<<"set中的值:\n";
	sc.insert(a);
	sc.insert(b);
	for(node t:sc){
		cout<<t;
	}
	cout<<"map中的值:\n";
	mp[a]=2;
	mp[b]=2;
	for(auto it:mp){
		cout<<it.first;
	}
	if(a==b)cout<<"a==b\n";
	if(a<b)cout<<"a<b\n";
	if(a>b)cout<<"a>b\n";
	if(a!=b)cout<<"a!=b\n";
	cout<<++++a;
	node c=a+b;
	cout<<(cout<<"a异或b后"<<c)<<endl;
	S xx(23,23);		//23与23异或 
	cout<<(xx.operator ^(1));	//里面的参数随意 
	return 0;
}


举报

相关推荐

运算符运算符

0 条评论