# CPP Beginner's Guilde ![meme](https://hackmd.io/_uploads/B1-31EYoJe.png) <!-- ![image](https://hackmd.io/_uploads/ByWyRQKjyx.png) ![image](https://hackmd.io/_uploads/H1i10mKiyg.png) --> :::info [TOC] ::: <br/> ## 1. C++ 簡介與環境設定 - C++ 是什麼? - C++ 是由 Bjarne Stroustrup 於 1980 年代開發的程式語言,基於 C 語言並加入物件導向 (OOP) 的特性。它廣泛應用於系統開發、遊戲開發、嵌入式系統、高效能運算等領域,並提供強大的標準函式庫 (STL) 來支援各種資料結構與演算法。 - C++ 在效能與靈活性方面仍是許多關鍵領域的首選,但相較於 Python 或 Java,學習曲線較陡,需要較多經驗來掌握最佳實踐與資源管理技巧。 - C++ 與其他語言的比較 | 語言 | 主要特點 | 適用範圍 | 優勢 | 劣勢 | |------|---------|----------|------|------| | **C++** | 物件導向 + 高效能 | 系統開發、遊戲開發、高效能計算 | 高執行效能、控制記憶體管理 | 語法較複雜,學習曲線較陡 | | **C** | 低階、高效能 | 嵌入式系統、作業系統 | 控制權高、效能優異 | 缺少 OOP,開發效率較低 | | **Java** | 跨平台、JVM執行 | 企業應用、Android 開發 | 自動記憶體管理、跨平台 | 執行效能不如 C++ | | **Python** | 直譯式、易學易讀 | 資料分析、AI/ML、Web開發 | 易上手、語法簡潔 | 執行速度較慢 | - 選擇 C++ 編譯器 - GCC - MinGW - 常用開發工具 1. VS Code 2. Dev-C++ 3. CodeBlocks - 第一個 C++ 程式:「Hello, World!」 ```cpp[] #include <iostream> int main() { std::cout << "Hello World\n"; return 0; } ``` <br/> ## 2. 基本語法與輸入輸出 - **變數與資料型態** (int, double, char, bool, string) - **輸入輸出** (`cin` / `cout` / `getline`) - **基本運算子** (+, -, *, /, %, ++, --, +=, -=...) - **型別轉換** (隱式 / 顯式轉換, `static_cast<T>`) #### 範例 ```cpp[] #include <iostream> using namespace std; int main() { int a, b; cout << "請輸入兩個整數: "; cin >> a >> b; cout << "它們的和為: " << a + b << endl; return 0; } ``` <br/> ## 3. 控制結構 - **條件判斷 (if / else if / else / switch)** - **迴圈 (for / while / do-while)** - **跳躍語句 (break / continue / return)** #### 範例 ```cpp[] #include <iostream> using namespace std; int main() { for (int i = 1; i <= 10; i++) { if (i == 5) continue; // 略過 5 cout << i << " "; } return 0; } ``` <br/> ## 4. 陣列與字串 - **陣列 (Array) 的宣告與操作** - **字串 (String) 與 `string` 類別** - **字串處理 (`substr`, `find`, `replace`, `append`)** #### 範例 ```cpp[] #include <iostream> using namespace std; int main() { string s = "Hello"; s.append(" World!"); cout << s << endl; // Hello World! return 0; } ``` <br/> ## 5. 函式與遞迴 - **函式的宣告與定義** - **函式的參數傳遞 (值傳遞 / 參考傳遞 / 指標傳遞)** - **遞迴函式 (Recursion) 概念與範例** #### 範例 ```cpp[] #include <iostream> using namespace std; int factorial(int n) { if (n == 1) return 1; return n * factorial(n - 1); } int main() { cout << factorial(5) << endl; // 120 return 0; } ``` <br/> ## 6. 指標與動態記憶體管理 - **指標 (Pointer) 的基本概念** - **指標與陣列** (`int* p = arr;`) - **`new` / `delete` (動態記憶體配置)** - **`nullptr` 與野指標問題** #### 範例 ```cpp[] #include <iostream> using namespace std; int main() { int* p = new int(10); cout << *p << endl; // 10 delete p; // 釋放記憶體 return 0; } ``` <br/> ## 7. 結構體與物件導向 (OOP 基礎) - **`struct` 與 `class` 的區別** - **封裝 (Encapsulation) / 建構子 (Constructor) / 解構子 (Destructor)** - **繼承 (Inheritance) / 多型 (Polymorphism) / 虛擬函式 (Virtual Function)** #### 範例 ```cpp[] #include <iostream> using namespace std; class Animal { public: void speak() { cout << "I am an animal!" << endl; } }; class Dog : public Animal { public: void speak() { cout << "Woof Woof!" << endl; } }; int main() { Dog myDog; myDog.speak(); // Woof Woof! return 0; } ``` <br/> ## 8. STL (標準模板庫) - **容器 (`vector`, `map`, `set`, `queue`, `stack`)** - **迭代器 (`begin()`, `end()`, `rbegin()`, `rend()`)** - **演算法 (`sort()`, `binary_search()`, `count()`)** #### 範例 ```cpp[] #include <iostream> #include <vector> # STL for vector using namespace std; int main() { vector<int> v = {3, 1, 4, 1, 5}; sort(v.begin(), v.end()); for (int x : v) cout << x << " "; // 3 1 4 1 5 cout << endl; return 0; } ``` <br/> ## 9. APCS/CPE 必備 C++ 技巧 <!-- - **快速讀取輸入 (`ios::sync_with_stdio(false); cin.tie(0);`)** --> - **使用 `unordered_map` 加速查找** - **位運算 (`bitwise operators` 加速計算)** - **暴力解法 vs. 優化解法 (時間複雜度分析)** #### 範例 ```cpp[] #include <iostream> #include <unordered_map> using namespace std; int main() { unordered_map<int, string> myMap; myMap[1] = "Apple"; myMap[2] = "Banana"; myMap[3] = "Cherry"; // 快速查找 if (myMap.find(2) != myMap.end()) { cout << "Found: " << myMap[2] << endl; } else { cout << "Not found" << endl; } // 輸出=> Found: Banana return 0; } ``` <br/> ## 10. 小測驗與 APCS/CPE 模擬題 - **題目 1:基本輸入輸出** - **題目 2:條件判斷與迴圈** - **題目 3:字串處理** - **題目 4:STL 與演算法應用** - **題目 5:模擬應用題** ([a040. 阿姆斯壯數](https://zerojudge.tw/ShowProblem?problemid=a040)) :::spoiler __解答__ ```cpp[] #include <iostream> #include <cmath> using namespace std; bool isArmstrong(int num) { int sum = 0, temp = num, digits = log10(num) + 1; while (temp) { sum += pow(temp % 10, digits); temp /= 10; } return sum == num; } int main() { int n, m; cin >> n >> m; bool found = false; for (int i = n; i <= m; i++) { if (isArmstrong(i)) { cout << (found ? " " : "") << i; found = true; } } if (!found) cout << "none"; } ``` ::: <br/> --- :::spoiler Relevant - [大學程式先修檢測](https://apcs.csie.ntnu.edu.tw/) - [ZeroJudge 平台](https://zerojudge.tw/) - [大學程式能力檢定](https://cpe.mcu.edu.tw/) - [LeetCOde 平台](https://leetcode.com/) :::