天天看點

結構體

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\n");
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

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

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

個性簽名:時間會解決一切