代語(yǔ)法與內(nèi)存管理示例)
1. 字符串流與格式化輸出C 中的std::stringstream提供了一種靈活的方式來(lái)格式化字符串類(lèi)似于 C 語(yǔ)言的sprintf。#include iostream #include sstream #include cstdio int main() { std::stringstream ss; ss 12 :; ss 12.23 :; ss yhping; std::string str ss.str(); std::cout str std::endl; char stra[128]; sprintf(stra, %d : %lf : %s, 12, 12.23, yhping); std::cout stra std::endl; }2. 基于范圍的 for 循環(huán)C11 引入了基于范圍的 for 循環(huán)可以簡(jiǎn)化數(shù)組和容器的遍歷。#include iostream #define ArSize(x) (sizeof(x)/sizeof(x[0])) int main() { int ar[] { 12, 23, 34 }; double dx[] { 1.2, 2.3, 3.4, 4.5, 5.6, 6.7 }; int* p ar; // 1. 遍歷數(shù)組 for (auto x : ar) // elemtype elemcount { std::cout x ; } std::cout std::endl; // 2. 遍歷初始化列表 for (auto x : { yhping, hello, word }) { std::cout x ; } std::cout std::endl; }3. 引用與值傳遞的遍歷使用引用遍歷可以修改原數(shù)組元素使用值遍歷則不會(huì)影響原數(shù)組。#include iostream int main() { int ar[] { 12, 23, 34 }; double dx[] { 1.2, 2.3, 3.4, 4.5, 5.6, 6.7 }; for (auto x : ar) { std::cout x \t; x 100; } std::cout std::endl; for (auto x : dx) { std::cout x \t; } std::cout std::endl; }4. 傳統(tǒng)數(shù)組遍歷使用宏計(jì)算數(shù)組大小進(jìn)行傳統(tǒng)遍歷。#include iostream #define ArSize(x) (sizeof(x)/sizeof(x[0])) int main() { int ar[] { 12, 23, 34 }; double dx[] { 1.2, 2.3, 3.4, 4.5, 5.6, 6.7 }; for (int i 0; i ArSize(ar); i) { std::cout ar[i] ; } }5. 內(nèi)存管理對(duì)比C 語(yǔ)言使用malloc、calloc、realloc和free而 C 使用new和delete。6. auto 類(lèi)型推導(dǎo)auto定義的變量可以根據(jù)初始化值在編譯時(shí)推導(dǎo)出類(lèi)型但會(huì)自動(dòng)剝離引用和 const 限定。int main() { auto a 10; // a int; auto int auto dx 12.25; // dx double auto double auto p a; // p int*; auto int* auto* s a, b 10; // s int *; auto int ; b int; auto int // auto x; // 錯(cuò)誤auto 變量必須初始化 }7. decltype 類(lèi)型推導(dǎo)decltype會(huì)保持 const 限定可以用于推導(dǎo)表達(dá)式類(lèi)型而不實(shí)際執(zhí)行。#include iostream int main() { int x 10; decltype(x) a x; // int // decltype(變量名) → 取變量本身的類(lèi)型 decltype((x)) rx x; // int // decltype((表達(dá)式)) → 表達(dá)式是左值時(shí)會(huì)推導(dǎo)為引用類(lèi)型 }8. decltype 與函數(shù)調(diào)用decltype中的函數(shù)調(diào)用不會(huì)實(shí)際執(zhí)行只用于推導(dǎo)返回值類(lèi)型。#include iostream int Add(int a, int b) { std::cout Add(int a, int b) std::endl; return a b; } int main() { std::cout sizeof(Add(1, 2)) std::endl; // Add(1, 2) 只是作為類(lèi)型上下文被 sizeof 計(jì)算函數(shù)并不會(huì)真的被調(diào)用 typedef decltype(Add(1, 2)) RetType; // decltype 里的函數(shù)調(diào)用不會(huì)實(shí)際執(zhí)行只是用來(lái)推導(dǎo)返回值類(lèi)型 RetType x; x Add(1, 2); // 只有這個(gè)才會(huì)真正調(diào)用函數(shù)并執(zhí)行里面的 cout return 0; }9. decltype 與結(jié)構(gòu)體#include iostream struct Student { char s_name[20]; int s_age; }; int main() { int x 10; int* p x; Student st{ 20204001, 12 }; typedef decltype(x) y; // int sizeof(x); y a, b; typedef decltype(st) sa; sa z, w; }10. 模板函數(shù)與 auto 返回值使用auto作為函數(shù)返回值類(lèi)型實(shí)現(xiàn)通用最大值函數(shù)。#include iostream #include typeinfo templateclass T, class U auto my_max(T a, U b) { return a b ? a : b; } int main() { std::cout my_max(12, 23) std::endl; std::cout my_max(a, b) std::endl; std::cout my_max(12, 34.23) std::endl; auto x my_max(12, 34.45); std::cout typeid(x).name() std::endl; std::cout x std::endl; // double auto y my_max(100, 3.45); std::cout typeid(y).name() std::endl; std::cout y std::endl; // auto 會(huì)把兩個(gè)操作數(shù)提升為 double }11. auto 的限制auto不能用于非靜態(tài)成員變量也不能用于未初始化的變量。struct Student { // auto age 0; // 錯(cuò)誤C11 不允許非靜態(tài)成員變量使用 auto }; int main() { auto age 0; // 正確 }12. 初始化列表與 auto#include iostream struct Student { char s_id[10]; int s_age; }; struct Emp { char id[10]; int age; }; int main() { auto ax { 2024, 12 }; // 初始化列表 }13. auto 參數(shù)函數(shù)C17 支持使用auto作為函數(shù)參數(shù)類(lèi)型。#include iostream void funa(auto ar) // C17 { std::cout typeid(ar).name() std::endl; std::cout funa(auto ar): sizeof(ar) std::endl; // 4 } void funb(auto ar) { std::cout typeid(ar).name() std::endl; std::cout funa(auto ar): sizeof(ar) std::endl; // 40 } int main() { int ar[10] { 12, 23, 34, 45, 56, 67, 78, 89, 90, 100 }; funa(ar); funb(ar); auto rb ar; // rb int* auto rc ar; // rc int()[10] }14. auto 與 const 引用int main() { int a 10; const int ra a; // const int auto rb ra; // int (剝離了 const 和引用) auto rx a; // int auto crb ra; // crb const int auto* p ra; // const int * }15. 動(dòng)態(tài)內(nèi)存分配展示 C 中new和delete的使用以及 placement new 的用法。#include iostream int main() { char str[10]; int* p new int(10); delete p; p (int*)::operator new(sizeof(int)); // p (int*)malloc(sizeof(int)); ::operator delete(p); p NULL; new (str) int(100); new (str 4) int(20); }16. 數(shù)組的動(dòng)態(tài)分配#include iostream int main() { int n 10; int* p (int*)malloc(sizeof(int) * n); free(p); p NULL; int* s new int[n] {1, 2, 3, 4, 5, 6, 7, 8, 9, 0}; // new int[n] // 1) sizeof(int[n]); 2) 調(diào)用構(gòu)造函數(shù); 3) 返回指針 for (int i 0; i n; i) { std::cout s[i] ; } std::cout std::endl; delete[] s; s NULL; return 0; }