1. 程式人生 > >C++筆記(十一)函式高階應用

C++筆記(十一)函式高階應用

//learanc11.cpp 函式的高階應用
#include<iostream>
#include<string>

using namespace std;

struct user {
    int age;
};


/*
行內函數,需要在前面加上inline.
行內函數的優點就是效率高,省去了函式呼叫的步驟。缺點是如果函式被多個地方呼叫則比較佔記憶體。
*/
inline int square(int n);
void swapv(int a, int b);
void swapr(int& a, int& b);//引用變數引數
void swapp(int
* a,int* b); void userr(user& u);//引用結構 string & stringr(string& s); void defaultp(int a = 1, string b = "aaa");//預設引數 //函式過載,函式名相同,引數列表不同的函式叫做函式過載。 void hello(char* str,int n); void hello(int n); //函式模版 template <typename T> void swapt(T& a, T& b); int main() { //行內函數的呼叫和普通函式式一樣的。
int n = square(10); cout << n << endl; //引用變數 int num = 100;//常規變數 int& numref = num;//引用變數,這裡的&不是地址運算子。int&表示int型別的引用。必須在宣告是進行初始化。相當於const指標 const int* pn = &num; cout << "num=" << num << endl; cout << "numref=" << numref << endl; numref++;//num和numref的值和地址都相同,改變一個另一個也會跟著改變。
cout << "num=" << num << endl; cout << "numref=" << numref << endl; //將引用變數作為函式引數和指標作為引數的對比 int a = 111; int b = 222; cout << "a=" << a << " b=" << b << endl; swapv(a, b);//不會交換變數的值 cout << "a=" << a << " b=" << b << endl; swapr(a,b);//會交換變數的值 cout << "a=" << a << " b=" << b << endl; swapp(&a, &b);//會交換變數的值 cout << "a=" << a << " b=" << b << endl; //結構的引用變數 user u = { 10 }; userr(u); cout << "age=" << u.age << endl; //將類物件傳遞給引數的使用,c++的通常做法是使用引用。 string str = "hello"; string& str1 = stringr(str); cout << str1 << endl; /* 函式傳遞的時候,應該按值傳遞,還是引用傳遞,還是指標傳遞呢? 第一種情況不對資料進行修改: 1:如果資料物件很小,如內建型別或小型結構,則按值傳遞。 2:如果資料物件是陣列,則使用const指標。 3:如果資料物件是較大的結構,使用const指標或者const引用。這樣可以提高程式的效率,節省複製結構所需要的時間和空間。 4:如果資料物件是類物件,使用const引用。傳遞類物件引數的標準方式是按引用傳遞。 第二種情況對資料進行修改: 1:如果資料物件是內建型別,使用指標。 2:如果資料物件是資料,使用指標。 3:如果資料物件是結構,使用引用或指標。 4:如果資料物件是類物件,使用引用。 */ //預設引數 defaultp(); defaultp(5); defaultp(10,"bbbb"); //函式過載 char str2[] = "hello world"; hello(str2,11); hello(10); //函式模版 int aa = 100; int bb = 200; swapt(aa, bb); cout << "aa=" << aa <<" bb=" << bb << endl; string s1 = "aaaa"; string s2 = "bbbb"; swapt(s1, s2); cout << "s1=" << s1 <<" s2=" << s2 << endl; return 0; } inline int square(int n) { return n * n; } void swapv(int a, int b) { int temp = a; a = b; b = temp; } void swapr(int& a,int& b) { int temp = a; a = b; b = temp; } void swapp(int* a, int* b) { int temp = *a; *a = *b; *b = temp; } void userr(user& u) { u.age = 100; } string& stringr(string& s) { cout << s << endl; s = "hello world"; return s; } void defaultp(int a, string b) { cout << "a=" << a << " b=" << b << endl; } void hello(char* str,int n) { cout << "hello(char* str,int n) " ; char* pc = str; char* pcend = str + n; for (pc = str;pc != pcend;pc++) { cout << *pc; } cout << endl; } void hello(int n) { cout << "hello(int n) " << n << endl; } template <typename T> void swapt(T& a, T& b) { T temp = a; a = b; b = temp; }

輸出結果:

100
num=100
numref=100
num=101
numref=101
a=111 b=222
a=111 b=222
a=222 b=111
a=111 b=222
age=100
hello
hello world
a=1 b=aaa
a=5 b=aaa
a=10 b=bbbb
hello(char* str,int n) hello world
hello(int n) 10
aa=200 bb=100
s1=bbbb s2=aaaa