IMPORTANT
本筆記由 Perplexity 協助生成架構與內容,由 PGpenguin72 進行簡易內容修改。
目錄
一. C++ 基本規則
1. 程式架構與萬用頭檔 ⭐⭐⭐
1 2 3 4 5 6 7 8 9 10 11 12
| #include <bits/stdc++.h> using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); return 0; }
|
為什麼要加這兩行?
1 2
| ios::sync_with_stdio(false); // 關閉 C++ 和 C 輸入輸出的同步(提速5倍) cin.tie(nullptr); // 解除 cin 和 cout 的綁定(再提速)
|
萬用頭檔包含:
2. 變數與資料型態
基本型態(APCS必背)
1 2 3 4 5 6
| int 整數 ±2×10^9 考試90%用這個 long long 大整數 ±9×10^18 超過10^9必用! double 小數 15位精度 char 單字元 'A'(65) string 字串 "Hello" bool 布林 true/false
|
宣告語法
1 2 3 4 5 6 7 8 9
| int x = 42; long long big = 1e18; double pi = 3.14159; char ch = 'A'; string s = "Hello"; bool ok = true;
int a = 1, b = 2, c = 3;
|
型態轉換(超重要!)
1 2 3 4 5 6 7 8 9 10 11 12
| int x = stoi("123"); long long n = stoll("999999999999"); double f = stod("3.14");
string s1 = to_string(42); string s2 = to_string(3.14);
double d = 7.9; int i = (int)d;
|
3. 運算子
數學運算(注意整數除法!)
1 2 3
| + - * / % (沒有 **,用 pow()) 7 / 2 = 3 整數除法直接截斷!(Python要用 //) 7.0 / 2 = 3.5 只要一方是double就保留小數
|
1 2
| #include <cmath> int pow_result = (int)pow(2, 10);
|
比較與邏輯
1 2
| == != > < >= <= &&(且) ||(或) !(非)
|
複合指定 + 自增自減
1 2 3 4 5 6 7 8
| int x = 10; x += 5; x -= 3; x *= 2; x /= 4; x %= 4; x++; x--;
|
4. 條件判斷
1 2 3 4 5 6 7
| if (x > 0) { cout << "正數"; } else if (x < 0) { cout << "負數"; } else { cout << "零"; }
|
三元運算子(超方便)
1 2
| string result = (x > 0) ? "正數" : "非正數"; int max_val = (a > b) ? a : b;
|
5. 迴圈
for迴圈(對應Python range)
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| for (int i = 0; i < 5; i++) { cout << i << endl; }
for (int i = 2; i < 5; i++) { cout << i << endl; }
for (int i = 0; i < 10; i += 2) { cout << i << endl; }
|
Range-based for(走訪容器,超讚!)
1 2 3 4 5 6 7 8 9
| vector<int> arr = {10, 20, 30}; for (int item : arr) { cout << item << endl; }
string s = "Hello"; for (char ch : s) { cout << ch << endl; }
|
while + 控制
1 2 3 4 5
| while (條件) { if (...) break; if (...) continue; }
|
6. 函式
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| int add(int a, int b) { return a + b; }
void print_hello(string name) { cout << "Hello " << name << endl; }
int main() { int result = add(3, 4); print_hello("企鵝"); return 0; }
|
7. 輸入輸出
基本輸入
1 2 3 4
| int n; cin >> n; double f; cin >> f; string s; cin >> s; getline(cin, s);
|
輸出技巧
1 2 3 4 5 6 7 8
| cout << "Hello" << endl; cout << x << " " << y << endl; cout << x;
printf("%d\n", x); printf("%.2f\n", 3.14159); printf("%lld\n", 1e18);
|
cin與getline混用陷阱
1 2 3 4 5
| int n; cin >> n; cin.ignore(); string line; getline(cin, line);
|
8. 常用內建函式
1 2 3 4 5 6 7 8 9 10
| #include <algorithm> #include <cmath> #include <numeric>
arr.size() max(a, b), min(a, b) accumulate(arr.begin(), arr.end(), 0LL) abs(-5), fabs(-5.5) pow(2, 10), sqrt(16) swap(a, b)
|
9. 常見錯誤與Debug
1 2 3 4 5
| CE - 編譯錯誤:語法錯、少;、型態錯 RE - 執行錯誤:除0、陣列越界、指標錯 WA - 答案錯 TLE - 超時 MLE - 記憶體超限
|
常見Bug實例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| int a = 1e9, b = 1e9; long long c = 1LL * a * b;
int arr[100]; int arr2[100] = {};
vector<int> v(5); cout << v [oreateai](http:
cin >> n; getline(cin, s);
|
10. 重要觀念與陷阱
1 2 3 4 5 6 7 8 9
| ✅ 索引從0開始 ✅ for(i=0; i<n; i++) 而非 i<=n ✅ 整數/整數=整數,7/2=3 ✅ 超過10^9用long long ✅ vector沒有負索引,用back() ✅ 陣列越界不會報錯,自己小心! ✅ 每個變數都要宣告型態 ✅ 每行結尾要加; ✅ 用{}包程式區塊
|
二. C++ 基本工具
1. vector (動態陣列) ⭐⭐⭐
1 2 3 4 5 6 7 8 9 10 11 12 13
| vector<int> arr = {1, 2, 3, 4, 5}; cout << arr[0] << endl; cout << arr.back() << endl;
arr.push_back(6); arr.pop_back(); arr.insert(arr.begin(), 0); arr.erase(find(arr.begin(), arr.end(), 3));
vector<int> v(10, 0); vector<int> v2(n);
|
走訪方式:
1 2 3 4 5 6 7 8 9
| for(int i = 0; i < arr.size(); i++) { cout << arr[i] << " "; }
for(int x : arr) { cout << x << " "; }
|
2. string (字串)
1 2 3 4 5 6 7 8 9 10 11
| string s = "Hello World"; cout << s.length() << endl; cout << s[0] << endl; cout << s.substr(0, 5) << endl;
s += "!!"; s.find("World") != string::npos
reverse(s.begin(), s.end());
|
字串分割:
1 2 3 4 5 6
| #include <sstream> string line = "1 2 3"; istringstream iss(line); vector<int> nums; int x; while(iss >> x) nums.push_back(x);
|
3. 輸入輸出進階工具 ⭐⭐⭐
一行多數:
1 2 3 4 5 6 7 8 9 10 11 12
| int n; cin >> n; vector<int> arr(n); for(auto& x : arr) cin >> x;
string line; getline(cin, line); istringstream iss(line); vector<int> arr2; int x; while(iss >> x) arr2.push_back(x);
|
印陣列:
1 2 3 4 5
| for(size_t i = 0; i < arr.size(); i++) { if(i) cout << " "; cout << arr[i]; } cout << endl;
|
4. sorted() 排序技巧 ⭐⭐⭐⭐
1 2 3 4 5 6
| vector<int> arr = {3, 1, 4, 2}; sort(arr.begin(), arr.end()); sort(arr.rbegin(), arr.rend());
sort(arr.begin(), arr.end(), greater<int>());
|
5. enumerate/zip 等便利工具
enumerate(索引+值):
1 2 3 4
| vector<int> arr = {10, 20, 30}; for(int i = 0; i < arr.size(); i++) { cout << i << ": " << arr[i] << endl; }
|
zip(多陣列對應):
1 2 3 4 5
| vector<int> a = {1,2,3}; vector<int> b = {10,20,30}; for(size_t i = 0; i < a.size(); i++) { cout << a[i] << " " << b[i] << endl; }
|
6. 二維陣列/矩陣
1 2 3 4 5 6 7 8
| vector<vector<int>> mat(3, vector<int>(3, 0)); mat[0][0] = 1;
int n, m; cin >> n >> m; vector<vector<int>> grid(n, vector<int>(m));
|
7. map (字典) - 超重要! ⭐⭐⭐⭐⭐
1 2 3 4 5 6 7 8 9 10 11 12 13
| map<string, int> score; score["Alice"] = 95; score["Bob"] = 87;
if(score.count("Alice")) { cout << score["Alice"] << endl; }
for(auto& [name, sc] : score) { cout << name << ": " << sc << endl; }
|
8. 字元與數字轉換
1 2 3 4 5 6 7 8
| char ch = 'A'; int asc = ch;
int num = '7' - '0'; char digit = 5 + '0';
tolower('A'), toupper('a')
|
9. Lambda 函式與自訂排序 ⭐⭐⭐⭐
1 2 3 4 5 6 7 8 9
| auto f = [](int x){ return x * 2; }; cout << f(5) << endl;
vector<pair<string,int>> stu = {{"Alice",85},{"Bob",92}}; sort(stu.begin(), stu.end(), [](auto& a, auto& b){ return a.second > b.second; });
|
三. APCS 競程實作模板
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| #include <bits/stdc++.h> using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while(T--) { int n; cin >> n; vector<int> arr(n); for(auto& x : arr) cin >> x; for(size_t i = 0; i < arr.size(); i++) { if(i) cout << " "; cout << arr[i]; } cout << endl; } return 0; }
|
四. 題目實作
初級
中級