直接给个样例:
struct node{
int x,y;
node(int x1,int y1):x(x1),y(y1){}
friend node operator +(node a,node b){
return node(a.x+b.x, a.y+b.y);
}
bool operator <(const node x1)const{
return x1.y>y;
}
};
int main()
{
node a(1,1),b(1,2);
node c = a + b;
cout<<c.x<<" "<<c.y<<endl;
if(a<b)
cout<<"a小"<<endl;
else
cout<<"b小"<<endl;
}
输出结果为
struct node{
int ed,l,cost;
node(int _ed=0,int _l=0,int _cost=0):ed(_ed),l(_l),cost(_cost){}
// node(int _v=0,int _c=0){
// v=_v;
// c=_c;
// }
// bool operator < (const node &a)const{
// return
// }
};
struct cmp{
bool operator ()(const node &a,const node &b){
if(a.l==b.l)
return a.cost>b.cost;
return a.l>b.l;
}
};
struct Edge{//邻接表
int ed,l,cost;
Edge(int _ed=0,int _l=0,int _cost=0):ed(_ed),l(_l),cost(_cost){}
};
首先事结构体的直接引用,初始化赋值:
方便输入:
struct node{
int a,b;
node(int _a=0,int _b=0):a(_a),b(_b){}
//结构体的直接赋值
};
//使用方便:可以直接进行结构体赋值
//例:
vector<node> n;
int main()
{
n.push_back(node(1,2));//直接将a=1,b=2放入结构体
cout<<n[0].a<<" "<<n[0].b;//尝试输出一下试试
//output:1 2
//是不是很方便?
}
struct node{
int a,b;
node(int _a=0,int _b=0){
a=_a;
b=_b;
}
//结构体的初始化赋值,样例大意:a,b初始化赋值0
};
结构体符号的重载:
一般重载符号(<,>,==,<=):
样例1:
struct node{
int a,b;//重载<,含义:结构体中a元素小的结构体优先
bool operator < (const node &x)const{
return x.a<a;
}
};
样例2:
struct node{
int a,b;//a元素相等的时候,b元素小的结构体优先
bool operator < (const node &x)const{
if(x.a==a)
return x.b<b;
return x.a<a;
}
};
结构体运算符的重载(+-*/):
样例一:
struct node{
int x,y;//重载加法运算符
node& operator + (node &a){
x=x+a.x;
y=y+a.y;
return *this;
}
node& operator - (node &a){//重载减法运算符
x=x-a.x;
y=y-a.y;
return *this;
}
};
int main()
{
std::ios::sync_with_stdio(false);
node a,b;
a.x=5,a.y=10;
b.x=1,b.y=2;
node c;
c.x=1,c.y=2;
cout<<c.x<<" "<<c.y<<endl;//output:1 2
c=a+b;
cout<<c.x<<" "<<c.y<<endl;//output:6 12
}
//当然,也可以定义其他类型的变量:
struct node{
double x,y;
node& operator + (node &a){
x=x+a.x;
y=y+a.y;
return *this;
}
};
int main()
{
std::ios::sync_with_stdio(false);
node a,b;
a.x=5.1,a.y=10.3;
b.x=1.2,b.y=2.2;
node c;
c.x=1,c.y=2;
cout<<c.x<<" "<<c.y<<endl;//output:1 2
c=a+b;
cout<<c.x<<" "<<c.y<<endl;//output:6.3 12.5
}
样例二:强制类型转化
注意,部分编译器会报错,类似:
[Warning] extended initializer lists only available with -std=c++11 or -std=gnu++11
但是可以用
struct node{
double x,y;
node friend operator - (node a,node b){
return {a.x-b.x,a.y-b.y};
}
};
int main()
{
std::ios::sync_with_stdio(false);
node a,b;
a.x=5.1,a.y=10.3;
b.x=1.2,b.y=2.2;
node c;
c.x=1,c.y=2;
cout<<c.x<<" "<<c.y<<endl;//output:1 2
c=a-b;
cout<<c.x<<" "<<c.y<<endl;//output:3.9 8.1
}
优先队列的排序问题:
首先要引用优先队列:priority_queue<node> que;
其中node是要引用的数据类型;
首先是优先队列的默认排序:
从大到小,例:
priority_queue<int> que;
//应用一个int类型的优先队列
int main()
{
que.push(1);
que.push(3);
que.push(2);
while(que.size())
{
cout<<que.top()<<" ";每次取出首元素
que.pop();
}
//output:3 2 1
}
然后是系统自带的一些库排序:
less<node>,greater<node>
注意引用头文件<vector>,因为优先队列也是一个动态数组
priority_queue<int,vector<int>,less<int> > que;
//引用的一个less,从大到小,和默认一样
int main()
{
que.push(1);
que.push(3);
que.push(2);
while(que.size())
{
cout<<que.top()<<" ";
que.pop();
}
//optput:3 2 1
}
priority_queue<int,vector<int>,greater<int> > que;
//引用的是greater,从小到大
int main()
{
que.push(1);
que.push(3);
que.push(2);
while(que.size())
{
cout<<que.top()<<" ";
que.pop();
}
//output:1 2 3
}
其实这两个都不是很常用,毕竟我们还是习惯自定义排序规则,用着用着也舒服,安心(据说程序员总是蜜汁自信??)
struct node{
int a,b;
node(int _a=0,int _b=0):a(_a),b(_b){}
};
struct cmp{
//a大的优先
bool operator()(const node &x,const node &y){
return x.a<y.a;
}
};
priority_queue<node,vector<node>,cmp> que;//cmp自定义排序
int main()
{
que.push(node(1,2));
que.push(node(5,8));
que.push(node(2,3));
while(que.size())
{
cout<<que.top().a<<" "<<que.top().b<<endl;;
que.pop();
}
/*output:
5 8
2 3
1 2
*/
}
当然,也可以有第二优先级的排序:
struct node{
int a,b;
node(int _a=0,int _b=0):a(_a),b(_b){}
};
struct cmp{
bool operator()(const node &x,const node &y){
if(x.a==y.a)//a元素相等时,b元素大的优先
return x.b<y.b;
return x.a<y.a;
}
};
priority_queue<node,vector<node>,cmp> que;
int main()
{
que.push(node(1,2));
que.push(node(5,8));
que.push(node(5,9));
que.push(node(2,3));
while(que.size())
{
cout<<que.top().a<<" "<<que.top().b<<endl;;
que.pop();
}
/*output:
5 9
5 8
2 3
1 2
*/
}
好了,总结的差不多了,如果以后还学到东西,就再补充一下,如果有什么错误,请指出,博主表示感谢!