c 語言不輸出空資料 (全面覆蓋)
阿新 • • 發佈:2022-03-06
#include<iostream> using namespace std; //區域性 靜態物件 size_t count_calls() { static size_t ctr = 0; return ++ctr; } //使用引用避免拷貝 bool isShorter(const string& s1, const string& s2) { return s1.size() < s2.size(); } //使用標記指定陣列的長度 void print(const char* cp) { if (cp) //cp不是一個空指標 while (*cp) //指標所指的字元不是空字元 cout << *cp++; } //陣列引用形參,c++允許將變數定義為陣列的引用,基於同樣的道理,形參也可以是陣列的引用 void print(int(&arr)[10]) { for (auto elem : arr) { cout << elem << endl; } //注意:f(int &arr[10]) 將arr宣告為引用的陣列 f(int (&arr)[10]) arr是具有10個整數的整形陣列的引用 } //傳遞多維陣列 void print(int(*matrix)[10], int rowsize) { //matrix代表指向含有10個整數的陣列指標 return; } //等價於 void print(int matrix[][10], int rowsize) { //matrix代表指向含有10個整數的陣列指標 return; } //含有可變形參的函式 void error_msg(initializer_list<string> il) { for (auto beg = il.begin(); beg != il.end(); ++beg) { cout << *beg << " "; } cout << endl; } string expected = "abc"; string actual = "def"; void test2() { if (expected != actual) { error_msg({ "functionx", expected, actual }); } else { error_msg({ "functionx", "okay" }); } } //省率符形參 //void foo(parm_list, ...); //void foo(...); //不要返回區域性物件的引用或指標 const string& manip() { string ret; if (!ret.empty()) return ret; //錯誤,返回區域性物件的引用 else return "empty"; //錯誤,empty是一個區域性臨時量 } //返回陣列指標 typedef int arr[10]; using arr = int[10]; arr* func(int i); //func返回一個指向含有10個整數的陣列的指標 /* int (*func(int i))[10]; func(int i):代表呼叫func時需要一個int型別的實參 (*func(int i)):我們可以對函式呼叫的結果執行解引用操作 (*func(int i))[10]:解引用func的呼叫將得到一個大小是10的陣列 int (*func(int i))[10] :表示陣列中的元素是int型別 */ //使用尾置返回型別 auto func(int i) ->int(*)[10]; //func接受一個int型別的實參,返回一個指標,該指標指向含有10個整數的陣列 //使用decltype int odd[] = { 1, 3, 4, 7, 9 }; int even[] = { 0, 2, 4 ,6, 8 }; decltype(odd)* arrptr(int i) { return (i % 2) ? &odd : &even; } //const_cast和過載 const string& shoererString(const string& s1, const string& s2) { return s1.size() <= s2.size() ? s1 : s2; } //這個函式的引數和返回型別都是const string的引用,我們可以對兩個非常量的string實參呼叫這個函式,但是返回的結果任然是const string的引用 //因此我們需要引進一個新的shorterString函式,當他的實參不是常量時,得到的結果是一個普通的引用,使用const_cast可以做到 string &shorterString(string& s1, string& s2) { auto &r = shorterString(const_cast<const string&>(s1), const_cast<const string&>(s2)); return const_cast<string&>(r); }//報錯了,不知道為什麼 //過載與作用域 string read(); void print(const string&); void print(double); void fooBar(int ival) { bool read = false; //新的作用域,隱藏了外層的read //string = read(); //錯誤,read是一個布林值,而非函式 void print(int); //print("value"); //print(const string&)被隱藏了 print(3.14); } //行內函數和constexpr函式 inline const string& shorterString(const string& s1, const string& s2) { return s1.size() <= s2.size() ? s1:s2; } //constexpr函式的返回型別及所有形參的型別必須都是字面值型別,而且函式體中必須有且只有一條 rerun語句 constexpr int new_sz(){ return 42; }; constexpr int foo = new_sz(); //返回指向函式的指標 using F = int(int*, int); //F為函式型別,不是指標 using PF = int(*)(int*, int); //PF為指標型別 PF f1(int); //PF是指向函式的指標,F1返回指向函式的指標 int (*f1(int))(int*, int); //f1有形參列表,所以f1是一個函式,f1前面有*,所以f1返回一個指標,指標型別本身也包含形參列表,因此指標指向函式,該函式的返回值為int auto f1(int) -> int(*)(int*, int); void test() { void f1() ; //隱士的定義空引數列表 void f2(void); //顯示的定義空引數列表 //自動物件:存在於塊執行期間的物件稱為自動物件,當塊的執行結束後,塊中建立的自動物件的值就變成未定義的 //const形參與實參 const int ci = 42; int i = ci; int* const p = &i; *p = 0; //下列三種形式是等價的 void print(const int*); void print(const int[]); void print(const int[10]); } int main() { system("pause"); return 0; }