struct的一些函数以及其他用法(析构、友元、构造、成员等)

文章目的:好多博客都在写类(class)可是没人写struct,想学还要自己扒(幸好我学过一点)

一,正文

1.构造函数

构造函数是以结构体的名字来命名的

构造函数可以初始化结构体变量的值,它既可以默认,也可以自己定义

#include<bits/stdc++.h>
using namespace std;
struct node{
	int a,b,c;
	node(){//默认的初始化
		a=1,b=2,c=3;
	}
	node(int o,int p,int q){//自己定义的初始化
		a=o,b=p,c=q;
	}
};
int main(){
	cout<<"X:\n";
	node x;//结构体默认初始值为 1 2 3
	cout<<x.a<<" "<<x.b<<" "<<x.c<<" \n";
	
	cout<<"Y:\n";
	node y(4,5,6);//把初始值设为 4 5 6
	cout<<y.a<<" "<<y.b<<" "<<y.c;
	return 0;
}

输出

X:
1 2 3
Y:
4 5 6

2.析构函数

析构函数是以“~结构体的名字”来命名的

它在当前这个变量被摧毁时运行(一般是在return 0时)

#include<bits/stdc++.h>
using namespace std;
struct node{
	int a,b,c;
	node(){//构造函数 
		a=1,b=2,c=3;
	}
	~node(){//析构函数 
		cout<<"Hello World!"; //在程序结束后运行(这个变量被摧毁时) 
	}
};
int main(){
	cout<<"---------程序开始---------\n";
	node x;
	cout<<x.a+x.b+x.c<<"\n";
	cout<<"---------程序结束---------\n";
	return 0;
}

输出

---------程序开始---------
6
---------程序结束---------
Hello World!

3.成员函数

成员函数和自定义函数用法几乎相同,但不过独属于当前这个结构体

#include<bits/stdc++.h>
using namespace std;
struct node{
	int a,b,c;
	node(){//构造函数 
		a=1,b=2,c=3;
	}
	int getsum(){//成员函数 
		return a+b+c;//返回a b c相加
	}
	void output(){
		cout<<a<<" "<<b<<" "<<c<<"\n";//输出a b c
	}
};
int main(){
	node x;
	x.output();
	cout<<x.getsum()<<"\n";
	return 0;
}

输出

1 2 3
6

4.重载运算符

重载运算符的函数可以写在结构体内也可以写在结构体外

但用法几乎都相同

注意:重载运算符后,输出要在结构内写单独的输出函数或用printf,直接使用cout会报错

#include<bits/stdc++.h>
using namespace std;
struct node{
	int a,b,c;//成员变量
	node(){
		int a=0,b=0,c=0;
	} 
	node(int o,int p,int q){
		a=o,b=p,c=q;
	} 
	node operator+(node t){//普通的 
		return node(a+t.a,b+t.b,c+t.c);
	}
	void output(){//输出 
		cout<<a<<" "<<b<<" "<<c<<"\n";
	}
	friend node operator-(node t1,node t2){//友元函数
		return node(t1.a-t2.a,t1.b-t2.b,t1.c-t2.c);
	}
};
node operator*(node t1,node t2){//函数外 
	return node(t1.a*t2.a,t1.b*t2.b,t1.c*t2.c);
}
int main(){
	node t1(1,2,3),t2(4,5,6);
	node t3;
	
	cout<<"+:";
	t3=t1+t2;
	t3.output();//输出要用成员函数 
	
	cout<<"-:";
	t3=t2-t1;
	t3.output();
	
	cout<<"*:";
	t3=t1*t2;
	printf("%d %d %d\n",t3.a,t3.b,t3.c);//输出也可以使用printf 
	return 0;
}

输出

+:5 7 9
-:3 3 3
*:4 10 18

用重载运算符排序:

#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=3e5+5;
struct node{
	int a,b,c;
	node(int x=1,int y=2,int z=3){a=x,b=y,c=z;}
	void output(){cout<<a<<" "<<b<<" "<<c<<endl;}
	bool operator<(const node T)const{
		if(a==T.a){
			if(b==T.b) return c<T.c;
			return b<T.b; 
		}
		return a<T.a; 
	}
};

priority_queue<node> q;
int main(){
	q.push(node(1,2,3));
	q.push(node(4,5,6));
	node tmp=q.top();
	tmp.output();
	return 0;
}

输出:

4 5 6

二、其他的一些技巧

1.template

这我认为是个比较实用的东西,它可以根据输入变量的类型不同来使用

直接说可能有点难懂,先看一下这个用了template的比大小函数

#include<bits/stdc++.h>
using namespace std;
template<typename T>
T getMax(T a,T b){
	if(a>b) return a;
	return b;
}	
int main(){
	cout<<"getMax(1,2)="<<getMax(1,2);//整数类型
	cout<<"\n";
	cout<<"getMax(1.1,2.5)="<<getMax(1.1,2.5);小数类型
	cout<<"\n";
	cout<<"getMax(a,b)="<<getMax("a","b");字符类型
	cout<<"\n\n";
	return 0;
}

输出:

getMax(1,2)=2
getMax(1.1,2.5)=2.5
getMax(a,b)=a

可以看出,它可以自动更改类型,使用时在要使用的地方前面加上一句template<typename T>就可以

注意:一定是紧挨着要使用的地方,两者之间不能插入任何东西

例如这样,它就会报错

template<typename T>
struct node{
	int a,b,c;
};
T getMax(T a,T b){
	if(a>b) return a;
	return b;
}

它还可以配合结构体使用,在定义结构体变量时写上要使用的类型就可以

#include<bits/stdc++.h>
using namespace std;
template<typename T> 
struct Node{
	T a,b,c;
};	
int main(){
	Node<char> y;
	y.a='A',y.b='B',y.c='C';
	cout<<y.a<<" "<<y.b<<" "<<y.c<<"\n\n";
	return 0;
}

输出

A B C

你甚至可以使用多个template

#include<bits/stdc++.h>
using namespace std;
template<typename T,typename T2> 
struct Node{
	T a,b,c;
	T2 d;
};	
int main(){
	Node<char,int> y;
	y.a='A',y.b='B',y.c='C',y.d=2;
	cout<<y.a<<" "<<y.b<<" "<<y.c<<" "<<y.d<<"\n\n";
	return 0;
}

输出:

A B C 2

2.函数重载

没什么重点也没什么用,看看就行

#include<bits/stdc++.h>
using namespace std;
void fun(int a=3,int b=7){
	cout<<a<<" "<<b<<"\n";
}
//函数重载 
void fun1(){
	cout<<"114514\n";
}
void fun1(int a){
	cout<<"a is "<<a<<"\n";
}
int main(){
	cout<<"fun()=";
	fun();
	cout<<"fun(1,2)=";
	fun(1,2);
	cout<<"fun(1)=";
	fun(1);
	cout<<"\n";
	
	cout<<"fun1()=";
	fun1();
	cout<<"\n";
	cout<<"fun1(1)=";
	fun1(1);
	cout<<"\n";
	return 0;
}

输出:

fun()=3 7
fun(1,2)=1 2
fun(1)=1 7

fun1()=114514

fun1(1)=a is 1

3.指针

比较基础,稍微看看

#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;
int f[10][10][10][10][10];
void fun(int &a){//引用
	a=1;
}
void fun2(int a){
	a=1;
}
int main(){
	int a=7;
	fun2(a);
	cout<<a<<' ';
	fun(a);
	cout<<a<<"\n";
	int &p=f[1][2][3][4][5];
	p=7;
	cout<<f[1][2][3][4][5];
	return 0;
}

输出

7 1
7

三、最后

最后放一个包含以上几乎所有东西的代码

#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;
template<typename T,typename T2> 
struct Node{
	T a,b,c;
	T2 d;
};
struct node{
	int a,b,c;//成员变量 
	node(){//构造函数 
		a=1,b=2,c=3;
	}
	node(int aa,int bb,int cc){
		a=aa,b=bb,c=cc;
	}
	int getsum(){//成员函数 
		return a+b+c;
	}
	~node(){//析构函数(return 0后运行,每个变量后都会运行) 
		cout<<"Hello world!\n";
	}
	node operator+(node t){
		return node(a+t.a,b+t.b,c+t.c);
	}
	void output(){
		cout<<a<<" "<<b<<" "<<c<<"\n";
	}
	//友元函数 
	friend node operator-(node t1,node t2){
		return node(t1.a-t2.a,t1.b-t2.b,t1.c-t2.c);
	}
};
node operator*(node t1,node t2){
	return node(t1.a*t2.a,t1.b*t2.b,t1.c*t2.c);
}
template<typename T>
T getMax(T a,T b){
	if(a>b) return a;
	return b;
}
void fun(int a=3,int b=7){
	cout<<a<<" "<<b<<"\n";
}
//函数重载 
void fun1(){
	cout<<"114514\n";
}
void fun1(int a){
	cout<<"a is "<<a<<"\n";
}
//运算符+-*/%[],特殊的函数! 
int main(){
	node t;
	cout<<"t.getsum()="<<t.getsum()<<"\n";
	node x(2,3,4);
	cout<<"x.getsum()="<<x.getsum()<<"\n\n";
	
	Node<char,int> y;
	y.a='A',y.b='B',y.c='C',y.d=2;
	cout<<y.a<<" "<<y.b<<" "<<y.c<<" "<<y.d<<"\n\n";
	
	cout<<"getMax(1,2)="<<getMax(1,2);
	cout<<"\n";
	cout<<"getMax(1.1,2.5)="<<getMax(1.1,2.5);
	cout<<"\n";
	cout<<"getMax(a,b)="<<getMax("a","b");
	cout<<"\n\n";
	
	cout<<"fun()=";
	fun();
	cout<<"fun(1,2)=";
	fun(1,2);
	cout<<"fun(1)=";
	fun(1);
	cout<<"\n";
	
	cout<<"fun1()=";
	fun1();
	cout<<"\n";
	
	
	node t1(1,2,3),t2(4,5,6);
	t1=t1+t2;
	cout<<"t1+t2=\n";
	t1.output();
	cout<<"t1*t2=\n";
	t1=t1*t2;
	t1.output();
	cout<<"t1-t2=\n";
	t1=t1-t2;
	t1.output();
	return 0;
}

输出

getMax(a,b)=a

fun()=3 7
fun(1,2)=1 2
fun(1)=1 7

fun1()=114514

Hello world!
Hello world!
t1+t2=
5 7 9
t1*t2=
Hello world!
Hello world!
Hello world!
20 35 54
t1-t2=
Hello world!
Hello world!
Hello world!
16 30 48
Hello world!
Hello world!
Hello world!
Hello world!

谢谢观看

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值