iPhone 15 Pro · PGpenguin72
2098 字
10 分鐘
C++
附屬文章
C++
IMPORTANT本筆記由 Perplexity 協助生成架構與內容,由 PGpenguin72 進行簡易內容修改。
目錄
一. C++ 基本規則
1. 程式架構與萬用頭檔 ⭐⭐⭐
#include <bits/stdc++.h> // APCS萬用頭檔,一個包含全部常用函式庫using namespace std;
int main() { // 競程必備:加速輸入輸出 ios::sync_with_stdio(false); cin.tie(nullptr);
// 你的程式碼寫在這裡
return 0;}為什麼要加這兩行?
ios::sync_with_stdio(false); // 關閉 C++ 和 C 輸入輸出的同步(提速5倍)cin.tie(nullptr); // 解除 cin 和 cout 的綁定(再提速)萬用頭檔包含:
// vector, string, map, algorithm(sort/max/min), iostream(cin/cout)// numeric(accumulate), cmath(abs/pow), sstream(字串分割)...2. 變數與資料型態
基本型態(APCS必背)
int 整數 ±2×10^9 考試90%用這個long long 大整數 ±9×10^18 超過10^9必用!double 小數 15位精度char 單字元 'A'(65)string 字串 "Hello"bool 布林 true/false宣告語法
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;型態轉換(超重要!)
// 字串轉數字int x = stoi("123"); // string → intlong long n = stoll("999999999999"); // → long longdouble f = stod("3.14"); // → double
// 數字轉字串string s1 = to_string(42); // "42"string s2 = to_string(3.14); // "3.14000"
// C風格強制轉型(常用)double d = 7.9;int i = (int)d; // 7(直接截斷)3. 運算子
數學運算(注意整數除法!)
+ - * / % (沒有 **,用 pow())7 / 2 = 3 整數除法直接截斷!(Python要用 //)7.0 / 2 = 3.5 只要一方是double就保留小數#include <cmath>int pow_result = (int)pow(2, 10); // 1024(注意回傳double)比較與邏輯
== != > < >= <=&&(且) ||(或) !(非)複合指定 + 自增自減
int x = 10;x += 5; // 15x -= 3; // 12x *= 2; // 24x /= 4; // 6x %= 4; // 2x++; // 3(x += 1)x--; // 2(x -= 1)4. 條件判斷
if (x > 0) { cout << "正數";} else if (x < 0) { cout << "負數";} else { cout << "零";}三元運算子(超方便)
string result = (x > 0) ? "正數" : "非正數";int max_val = (a > b) ? a : b;5. 迴圈
for迴圈(對應Python range)
// range(5): 0,1,2,3,4for (int i = 0; i < 5; i++) { cout << i << endl;}
// range(2,5): 2,3,4for (int i = 2; i < 5; i++) { cout << i << endl;}
// range(0,10,2): 0,2,4,6,8for (int i = 0; i < 10; i += 2) { cout << i << endl;}Range-based for(走訪容器,超讚!)
vector<int> arr = {10, 20, 30};for (int item : arr) { // 直接取元素 cout << item << endl;}
string s = "Hello";for (char ch : s) { // 逐字元 cout << ch << endl;}while + 控制
while (條件) { // ... if (...) break; // 跳出 if (...) continue; // 跳過本次}6. 函式
// 必須指定回傳型態和參數型態!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. 輸入輸出
基本輸入
int n; cin >> n;double f; cin >> f;string s; cin >> s; // 只讀單字(空格停止)getline(cin, s); // 讀整行(含空格)輸出技巧
cout << "Hello" << endl; // 基本輸出cout << x << " " << y << endl; // 多變數cout << x; // 不換行
// printf(C風格,格式化超方便)printf("%d\n", x); // %d=整數printf("%.2f\n", 3.14159); // 小數點後2位:3.14printf("%lld\n", 1e18); // long long用 %lldcin與getline混用陷阱
int n;cin >> n; // 輸入 5<Enter>cin.ignore(); // 消耗換行符!string line;getline(cin, line); // 現在才能正確讀下一行8. 常用內建函式
#include <algorithm> // sort/max/min/swap/reverse#include <cmath> // abs/pow/sqrt#include <numeric> // accumulate
arr.size() // 長度max(a, b), min(a, b) // 最大最小accumulate(arr.begin(), arr.end(), 0LL) // 總和(用long long避免溢位)abs(-5), fabs(-5.5) // 絕對值pow(2, 10), sqrt(16) // 次方/平方根swap(a, b) // 交換9. 常見錯誤與Debug
CE - 編譯錯誤:語法錯、少;、型態錯RE - 執行錯誤:除0、陣列越界、指標錯WA - 答案錯TLE - 超時MLE - 記憶體超限常見Bug實例:
// 1. 整數溢位int a = 1e9, b = 1e9; // a * b 溢位!long long c = 1LL * a * b; // 正確寫法
// 2. 忘記初始化int arr[100]; // 垃圾值!int arr2[100] = {}; // 全0 ✓
// 3. 陣列越界(不會報錯!)vector<int> v(5);cout << v [oreateai](http://oreateai.com/blog/bridging-the-gap-navigating-the-python-to-c-conversion-journey/bf7a787640019e3316e5c2ab9fac9986); // UB!
// 4. cin與getline混用cin >> n; getline(cin, s); // s變成空字串!10. 重要觀念與陷阱
✅ 索引從0開始✅ for(i=0; i<n; i++) 而非 i<=n✅ 整數/整數=整數,7/2=3✅ 超過10^9用long long✅ vector沒有負索引,用back()✅ 陣列越界不會報錯,自己小心!✅ 每個變數都要宣告型態✅ 每行結尾要加;✅ 用{}包程式區塊二. C++ 基本工具
1. vector (動態陣列) ⭐⭐⭐
vector<int> arr = {1, 2, 3, 4, 5};cout << arr[0] << endl; // 1cout << arr.back() << endl; // 5(最後一個)
// 常用操作arr.push_back(6); // 加到尾端arr.pop_back(); // 移除尾端arr.insert(arr.begin(), 0); // 開頭插入arr.erase(find(arr.begin(), arr.end(), 3)); // 移除第一個3
// 大小指定vector<int> v(10, 0); // 10個0vector<int> v2(n); // n個元素(預設0)走訪方式:
// 1. 索引走訪for(int i = 0; i < arr.size(); i++) { cout << arr[i] << " ";}
// 2. 直接走訪元素(推薦)for(int x : arr) { cout << x << " ";}2. string (字串)
string s = "Hello World";cout << s.length() << endl; // 11cout << s[0] << endl; // Hcout << s.substr(0, 5) << endl; // Hello
// 常用操作s += "!!"; // 拼接s.find("World") != string::npos // 找子字串
// 反轉reverse(s.begin(), s.end());字串分割:
#include <sstream>string line = "1 2 3";istringstream iss(line);vector<int> nums;int x;while(iss >> x) nums.push_back(x); // [1,2,3]3. 輸入輸出進階工具 ⭐⭐⭐
一行多數:
// 方法1:已知個數int n; cin >> n;vector<int> arr(n);for(auto& x : arr) cin >> x;
// 方法2:未知個數string line;getline(cin, line);istringstream iss(line);vector<int> arr2;int x;while(iss >> x) arr2.push_back(x);印陣列:
for(size_t i = 0; i < arr.size(); i++) { if(i) cout << " "; // i!=0才印空格 cout << arr[i];}cout << endl;4. sorted() 排序技巧 ⭐⭐⭐⭐
vector<int> arr = {3, 1, 4, 2};sort(arr.begin(), arr.end()); // 升序 [1,2,3,4]sort(arr.rbegin(), arr.rend()); // 降序 [4,3,2,1]
// 自訂排序(超重要!)sort(arr.begin(), arr.end(), greater<int>()); // 降序5. enumerate/zip 等便利工具
enumerate(索引+值):
vector<int> arr = {10, 20, 30};for(int i = 0; i < arr.size(); i++) { cout << i << ": " << arr[i] << endl;}zip(多陣列對應):
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. 二維陣列/矩陣
// 3x3全0矩陣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 (字典) - 超重要! ⭐⭐⭐⭐⭐
map<string, int> score;score["Alice"] = 95;score["Bob"] = 87;
// 檢查存在if(score.count("Alice")) { cout << score["Alice"] << endl;}
// 遍歷for(auto& [name, sc] : score) { // C++17 cout << name << ": " << sc << endl;}8. 字元與數字轉換
char ch = 'A';int asc = ch; // 65(char就是數字!)
int num = '7' - '0'; // 7char digit = 5 + '0'; // '5'
// 大小寫轉換tolower('A'), toupper('a')9. Lambda 函式與自訂排序 ⭐⭐⭐⭐
// Lambda基本auto f = [](int x){ return x * 2; };cout << f(5) << endl; // 10
// 學生排序:(名字,分數)vector<pair<string,int>> stu = {{"Alice",85},{"Bob",92}};sort(stu.begin(), stu.end(), [](auto& a, auto& b){ return a.second > b.second; // 分數降序});三. APCS 競程實作模板
#include <bits/stdc++.h>using namespace std;
int main() { ios::sync_with_stdio(false); cin.tie(nullptr);
int T; cin >> T; // 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;}四. 題目實作
初級
中級
留言區