天天看點

内聯函數_引用_預設參數_模版_函數重載

1:内聯函數

内聯函數_引用_預設參數_模版_函數重載

普通函數是直接到函數記憶體位址去執行,但是内聯函數是把函數拷貝一份到main函數裡面,不要再去指定位置查找了。

函數代碼執行時間很短,調用時間相對長此時用内聯。但當代碼執行時間長,将函數調入到記憶體裡面的成本大于函數調用的時間此時不建議使用内聯函數。

内聯函數一般要比普通函數效率高,缺點是記憶體消耗大。

但一般在函數定義上加内聯

關于内聯的知識可以參考這篇部落格

1 #include <bits/stdc++.h>
 2 using namespace std;
 3 inline int S(int num)
 4 {
 5     return num * num;
 6 }
 7 #define S1(num) num * num//以後在所有使用S(num)的地方,被自動替換為 num * num
 8 int main()
 9 {
10     cout << S(5) << endl;//25
11     cout << S(5+10) << endl;//225
12     cout << S1(5) << endl;//25
13     cout << S1(5+10) << endl;//65
14     //很明顯,最後一個宏定義的結果出錯,他的計算思路是5+10*5+10,是以c++推薦使用内聯,不推薦使用宏定義
15     return 0;
16 }      

2:引用 

内聯函數_引用_預設參數_模版_函數重載
内聯函數_引用_預設參數_模版_函數重載

const int& refValue = 12//可以跟常量

内聯函數_引用_預設參數_模版_函數重載

使用引用的時候習慣加上const(除非你明确要對引用變量進行修改),可以防止對變量的随意修改。

1 #include <bits/stdc++.h>
 2 using namespace std;
 3 void Swap1(int num1, int num2)//傳參
 4 {
 5     int temp;
 6     temp = num1;
 7     num1 = num2;
 8     num2 = temp;
 9 }
10 void Swap2(int* num1, int* num2)//傳指針
11 {
12     int temp;
13     temp = *num1;
14     *num1 = *num2;
15     *num2 = temp;
16 }
17 void Swap3(int& num1, int& num2)//引用
18 {
19     int temp;
20     temp = num1;
21     num1 = num2;
22     num2 = temp;
23 }
24 int main()
25 {
26     int num1,num2;
27     cout << "輸入要交換的兩個參數" << endl;
28     cin >> num1 >> num2;
29     Swap1(num1, num2);//沒有實作交換,因為傳進的是副本
30     cout << "傳參交換結果為:" << num1 << "\t" << num2 << endl;
31     Swap2(&num1, &num2);//實作了交換,因為是位址裡面的内容被交換
32     cout << "傳指針交換結果為:" << num1 << "\t" << num2 << endl;
33     Swap3(num1, num2);//實作了交換,引用其實就是變量的别名
34     cout << "引用換結果為:" << num1 << "\t" << num2 << endl;
35     return 0;
36 }      
内聯函數_引用_預設參數_模版_函數重載
1 #include <bits/stdc++.h>
 2 using namespace std;
 3 int& sum()//傳回值類型是引用類型的時候,不能傳回局部變量
 4 {
 5     int num = 10;
 6     int& rNum = num;
 7     return rNum;//不能傳回局部變量
 8 }
 9 void test()
10 {
11     int x = 1, y = 2, z = 3;
12 }
13 int main()
14 {
15     int& result = sum();//因為rNum是局部變量,函數結束後會被系統回收
16     test();//result所指的位置因為被回收,是以其他變量可以申請占用并修改資料
17     cout << "result = " << result << endl;//result = 2,說明空間資料被修改,是以引用類型不能傳回局部變量
18     return 0;
19 }      

傳回類型是引用類型的隻能傳回傳入的引用

内聯函數_引用_預設參數_模版_函數重載

 上述代碼傳回值隻能傳回一個,切改傳回值是傳入的參數的一個,即可以為num1或者num2.

上述代碼因為result和num所指空間相同,是以result被修改,num也被修改,是以為防止引用被誤修改,一般加上const。

内聯函數_引用_預設參數_模版_函數重載

基本類型數組啊不鼓勵使用引用傳遞,結構體對象啊多使用引用

這個部落客總結的引用很詳細,可以作為參考

3:預設參數

内聯函數_引用_預設參數_模版_函數重載
1 #include <bits/stdc++.h>
 2 using namespace std;
 3 void test(int x = 10)//預設值是10
 4 {
 5     cout << x << endl;
 6 }
 7 void test1(int x, int y = 10)//從右到左按順序聲明
 8 {
 9 
10 }
11 void test2(int x = 10, int y){//必須先預設右邊的
12 
13 }
14 int main()
15 {
16     test();//不寫參數預設傳10
17     test(100);//指明了參數
18 
19     return 0;
20 }      

4:  模版

内聯函數_引用_預設參數_模版_函數重載
1 #include <bits/stdc++.h>
 2 using namespace std;
 3 template<typename T>
 4 void Swap(T& a,T& b)//可以傳入任何類型,系統根絕傳入的資料判别類型,大大提高代碼的通用性
 5 {
 6     T temp;
 7     temp = a;
 8     a = b;
 9     b = temp;
10 }
11 int main()
12 {
13    cout << "請輸入兩個整數:" <<endl;
14    int a,b;
15    cin >> a >> b;
16    Swap(a, b);
17    cout << "交換的結果為: ";
18    cout << a << "\t"<< b << endl;
19    cout << "請輸入兩個字元串:" <<endl;
20    string a1, b1;
21    cin >> a1 >> b1;
22    Swap(a1, b1);
23    cout << "交換的結果為: ";
24    cout <<a1 << "\t" << b1;
25     return 0;
26 }      

5:函數重載

内聯函數_引用_預設參數_模版_函數重載
1 #include <bits/stdc++.h>
 2 using namespace std;
 3 void test(int x)//預設值是10
 4 {
 5     cout << x << endl;
 6 }
 7 void test()
 8 {
 9     cout << "test()" << endl;
10 }
11 int main()
12 {
13     //名字相同,參數清單不同,是以認為重載
14     test();//不帶參數的
15     test(100);//帶參數的,
16 
17     return 0;
18 }      

繼續閱讀