# 競試必勝C++ 附錄一 實用 STD 函式 ## 優化 IO ```c++ #define 幫我撐五秒鐘 ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); ``` ## 輸出格式控制 (cout) ### 四捨五入小數點 輸出 n 位有效數字,包括整數和小數部分,會四捨五入 ```c++ cout << setprecision (n) << [double or float]; ``` 無條件進位捨去 ```c++ cout << ceil([double or float]); // 進位 cout << floor([double or float]); // 捨去 ``` ## 字元、字串相關 ### Ascii Code A => 65 ~ Z => 90, a => 97, z => 122 ### String to Int ```C++ stringstream ss; ss << [int or string] ss >> [儲存變數] (return string or int) // 重復使用 implement 下兩行 ss.str(""); ss.clear(); ``` ### 字元搜尋 ```C++ size_t pos = str.find("live"); ``` ### 字元切割 ```C++ str.substr ([開始位置(從1開始)], [獲取字數]); // 獲取範圍位置 str.substr ([斷頭位置(從0開始,可用 pos)]); // 獲取斷頭位置到最後的位置 ``` ### String.split(" ") von JS ``` string str = "I am fast as fuck boy!", temp = "" istrtingstream iss; while(iss > temp){ cout << temp << "\n"; } // 輸出:[I,am,fast,as,fuck,boy!] ``` ### 字元插入 ```C++ str.append([來源字串]); str.append([來源字串], [獲取字數]); str.append([來源字串], [開始位置(從1開始)], [獲取字數]); ``` ### 字串倒反 ```C++ for (string::reverse_iterator rit=str.rbegin(); rit!=str.rend(); ++rit) cout << *rit; //輸出倒反的結果 ``` ### 陣列移除 ```C++ vector.erase (vector.begin()+[你想移除的index]); vector.erase ([從第幾個index開始移除],[移除到第幾個index]); ``` ## remove_if 查找符合條件的iterator位置 ```c++ std::string a="hello world"; a.erase(remove_if(a.begin(),a.end(),::isspace)); //a="hellowworld ``` ### Find Map's Value ```C++ vector<int> vec; vector<int>::iterator it = find (vec.begin(), vec.end(), [欲搜尋值]); if (it != vec.end()) cout << "Element found in myvector: " << *it << '\n'; else cout << "Element not found in myvector\n"; ``` 還有這個 ```c++ vector<int> v = {1, 2, 3, 4, 6}; find_if(v.begin(), v.end(),[條件]); ``` ## SORT 演算法 ### <Alogrithm.h> ```C++ sort([開始迭代器], [結束迭代器], [](type a, type b){function}) vector<int> vec; sort(vec.begin(), vec.end(), [](int a, int b){ return a > b; }) // 大到小 ``` ### No <Alogrithm.h> ```C++ void bubbleSort(std::vector<int>& vec) { int n = vec.size(); bool swapped; for (int i = 0; i < n - 1; i++) { swapped = false; for (int j = 0; j < n - i - 1; j++) { if (vec[j] < vec[j + 1]) { // Compare adjacent elements // Swap if the element found is smaller than the next element std::swap(vec[j], vec[j + 1]); swapped = true; } } // If no two elements were swapped by inner loop, then break if (!swapped) break; } } ``` ## 下一個排列組合 > 如果要獲得全部排列組合要先進行 SORT ```C++ vector<int> arr; do { for(auto i of arr) cout << arr << " "; cout << "\n"; } while (next_permutation(arr.begin(), arr.end(), [](type a, type b){function})); ``` > No <Alogrithm.h> ```C++ bool nextPermutation(std::vector<int>& vec) { int n = vec.size(), i, j; // Step 1: Find the largest index i such that vec[i - 1] < vec[i] for (i = n - 1; i > 0; i--) { if (vec[i - 1] < vec[i]) { break; } } // If no such index exists, the permutation is the last permutation if (i == 0) { return false; } // Step 2: Find the largest index j greater than i such that vec[i - 1] < vec[j] for (j = n - 1; j >= i; j--) { if (vec[j] > vec[i - 1]) { break; } } // Step 3: Swap elements at i-1 and j (自己寫)swap(vec, i - 1, j); // Step 4: Reverse the sequence from i to end (自己寫)reverse(vec, i); return true; } ```