结构体

c++不再需要typedef的方式定义一个struct,而且在struct里除了可以有变量(称为成员变量)之外还可以有函数(成员函数),其用法与C++中支持的class差不多。事实上,在C++中struct和class最主要的区别是默认的访问权限和继承方式不同,而其它方面差异很小。

这里主要讲与算法竞赛可能用上的结构体初始化和运算符重载(大佬请直接忽略)

结构体初始化

类的构造函数是类的一种特殊的成员函数,它会在每次创建类的新对象时自动执行,但是它不能被直接调用构造函数的名称与类名一样,并且不会有任何返回类型,也不会返回void。

析构函数类似,也是一种特殊的成员函数,它会在每次删除所创建的对象时自动执行,析构函数的名称与类的名称是完全相同的,只是在前面加了个波浪号(~)作为前缀,它不会返回任何值,也不能带有任何参数。析构函数有助于在跳出程序(比如关闭文件、释放内存等)前释放资源。

初始化是通过构造函数来实现的,以下4种方法

1、在结构体中声明,在外面定义

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 struct Point
 6 {
 7     int x, y, w;
 8     Point();                //构造函数声明
 9     Point(int, int, int);    //也是构造函数声明
10 };
11 
12 Point::Point()                //不带参数
13 {
14     printf("Create Success
");
15 }
16 
17 Point::Point(int _x,int _y,int _w)        //带参数
18 {
19     x = _x; y = _y; w = _w;
20 }
21 
22 
23 int main()
24 {
25     Point a;
26     Point b(2, 3, 4);
27     
28     return 0;
29 }

2、也可以直接在结构体中写出定义

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 struct Point
 6 {
 7     int x, y, w;
 8     Point(){};            //之前Point();是函数声明,Point(){}这个是函数定义
 9     Point(int _x, int _y, int _w)
10     {
11         x = _x; y = _y; w = _w;
12     }
13 };
14 
15 int main()
16 {
17     Point a;                //实际使用了第一种初始化
18     Point b(1, 2, 3);        //实际使用了第二种初始化
19     
20     return 0;
21 }

3、定义一个成员函数

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 struct Point
 6 {
 7     int x, y, w;
 8     void  init(int _x,int _y,int _w)
 9     {
10         x = _x; y = _y; w = _w;
11     }
12 };
13 
14 int main()
15 {
16     Point a;
17     a.init(1, 2, 3);
18     
19     return 0;
20 }

4、成员初始化列表,即定义一个有参的构造函数,用成员变量的初始化表对成员变量进行初始化

 1 #include<cstdio>
 2 #include<algorithm>
 3 using namespace std;
 4 
 5 struct Point
 6 {
 7     int x, y, w;
 8     //Point(){};            //下面构造函数中每个参数都设了初始值,就不必Point(){}这个构造函数了
 9     Point(int _x = 0, int _y = 0, int _w = 0) :x(_x), y(_y), w(_w) {};
10 };
11 
12 int main()
13 {
14     Point a;                //a为(0,0,0)
15     Point b(1, 2, 3);        //b为(1,2,3)
16     
17     return 0;
18 }

acm中一般用方法二和方法四

运算符重载

重载的运算符有特殊的函数名,由关键字operator加要重载的运算符构成。与其它函数一样,重载运算符有一个返回类型和参数列表。重载运算符可以被定义成普通的非成员函数或类成员函数。

如果参数不会被改变,一般用const引用来传递,函数也用const。

一般情况下,建议一元运算符使用成员函数,二元运算符使用非成员的普通函数。但是如果二元运算符中,第一个操作数为非对象时,必须使用非成员的普通函数。如输入输出运算符<<和>>

1、使用成员函数

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 struct Point
 7 {
 8     int x, y, w;
 9     Point(int _x = 0, int _y = 0, int _w = 0) :x(_x), y(_y), w(_w) {};
10     Point operator + (const Point& A)
11     {
12         return Point(this->x + A.x, this->y + A.y, this->w + A.w);
13     }
14 };
15 
16 
17 
18 int main()
19 {
20     Point a(1,1,1);                //a为(1,1,1)
21     Point b(1, 2, 3);            //b为(1,2,3)
22     Point c;
23     c = a + b;                    //c为(2,3,4)
24     
25     return 0;
26 }

2、使用普通的非成员函数

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 struct Point
 7 {
 8     int x, y, w;
 9     Point(int _x = 0, int _y = 0, int _w = 0) :x(_x), y(_y), w(_w) {};
10 };
11 
12 Point operator + (const Point& A,const Point& B)    //成员函数只有一个参数(另一个是this),这里有两个
13 {
14     return Point(A.x + B.x, A.y + B.y, A.w + B.w);
15 }
16 
17 
18 int main()
19 {
20     Point a(1,1,1);                //a为(1,1,1)
21     Point b(1, 2, 3);            //b为(1,2,3)
22     Point c;
23     c = a + b;                    //c为(2,3,4)
24     
25     return 0;
26 }

重载"<<"或“>>”这种参数含有非成员变量,必须使用非成员的普通函数。

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 struct Point
 7 {
 8     int x, y, w;
 9     Point(int _x = 0, int _y = 0, int _w = 0) :x(_x), y(_y), w(_w) {};
10     const Point operator + (const Point& p)
11     {
12         return Point(this->x + p.x, this->y + p.y, this->w + p.w);
13     }
14 };
15 
16 ostream& operator << (ostream& out, const Point& a)
17 {
18     out << a.x << " " << a.y << " " << a.w;
19     return out;
20 }
21 
22 
23 int main()
24 {
25     Point a(1,1,1);                //a为(1,1,1)
26     Point b(1, 2, 3);            //b为(1,2,3)
27     Point c;
28     c = a + b;                    //c为(2,3,4)
29     cout << c;                    //打印出2 3 4
30     
31     return 0;
32 }

 如果是输入,参数值会改变,参数前面不能加const限定词

1 istream& operator >> (istream& in, Point& a)   
2 {
3     in >> a.x >> a.y >> a.w;
4     return in;
5 }

参考链接:

1、https://blog.csdn.net/andrewgithub/article/details/59477757

2、https://blog.csdn.net/qq_26822029/article/details/81022227

3、http://www.runoob.com/cplusplus/cpp-constructor-destructor.html

4https://www.cnblogs.com/sexybear/p/4551742.html

5、http://www.runoob.com/cplusplus/cpp-overloading.html

原文地址:https://www.cnblogs.com/lfri/p/10087565.html