<h2 class="text-center">CPE 檢定加強計畫(三)</h2> <h3 class="text-center">字串與STL</h3> <style> :root{ --r-main-font-size: 30px; } .reveal .slides { text-align: left; } /* .ul-left { display: block !important; margin:0 0 0 0.5em !important; } */ .hint { color: #191919; } details[open] { margin-top: 1.5em; } details[open] .hint { color: inherit; } details[open] .hint::after { content: ":"; } </style> ---- ### Table of Contents - string - vector - map - pair - sort - 基本題型講解 --- <h2 class="text-center">string</h2> ---- ## string 基本操作 ```cpp string s; // 宣告字串 cin >> s; // 輸入字串 cout << s << '\n'; // 輸出字串 cout << "The first letter is: " << s[0] << '\n'; cout << "length: " << s.length() << '\n'; // size() 也可以 ``` ---- ## 實用的字串功能 ```cpp string s = "abc", a = "cba"; s.push_back(1); // "abc1" cout << s.empty() << '\n'; // 確認 s 是否為空字串 cout << s.substr(1, 2) << '\n'; // 輸出第一位後長度 2 的子字串 s += a; // s 跟 a 合在一起,儲存在 s cout << s << '\n'; // "abc1cba" cout << (s == a) << '\n'; // 判斷是否相同 ``` ---- ## getline 基本用法 這東東可以把整行吃下去 包含空格 ```cpp string s; getline(cin, s); // 輸入整行 cout << s << '\n'; // 輸出 ``` ---- ## getline 與 cin 混用 要注意 getline 是吃到 `'\n'` 就結束, 而 cin 是吃掉需要的東西就結束, 不包含 `'\n'`,是故混著用時要注意 ```cpp string s; int n; cin >> n; getline(cin, s); // 先吃掉 '\n' getline(cin, s); // 輸入整行 ``` --- <h2 class="text-center">map</h2> ---- ## map 基本用法 map 就是個對照表 ```cpp map<int, int> mp1; // int 對 int map<int, char> mp2; // int 對 char map<string, int> mp3; // string 對 int mp3["foobar"] = 87; ``` --- <h2 class="text-center">vector</h2> ---- ### vector 基本用法 我都把他當 array 來用 ```cpp vector<int> foo; // 空 vector vector<bool> bar(10); // 10 個空間的 vector foo.push_back(5); foo.push_back(4); foo.push_back(8); foo.push_back(7); for(int i = 0; i < 4; i++) cout << vec[i] << ' '; // 輸出 5 4 8 7 ``` ---- ## vector 常見用法 ```cpp vector<vector<int>> vec; // 二維 vector vector<int> foobar[10000]; // 這也是二維 vector cout << vec.size() << '\n'; // 查詢大小 cout << vec.empty() << '\n'; // 判斷是否為空 ``` --- <h2 class="text-center">pair</h2> ---- ## pair 基本用法 ```cpp pair<int, int> pii = {8, 7}; cout << pii.first << ' ' << pii.second << '\n'; ``` ---- ## pair 進階用法 ```cpp #define pii pair<int, int> #define x first #define y second vector<pii> vec; // 這在存座標時很好用 vec.push_back({8, 7}); cout << vec[0].x << ' ' << vec[0].y << '\n'; ``` --- <h2 class="text-center">sort</h2> ---- ## sort 基礎用法 ```cpp vector<int> vec = {5, 4, 8, 7}; sort(vec.begin(), vec.end()); // 預設升冪 for(auto i : vec) // 4 5 7 8 cout << i << ' '; ``` ---- ## sort 自定義排序 ```cpp bool cmp1(int a, int b){ // 傳入的容器是什麼資料型態就寫什麼 return a > b; // a < b 升冪, a > b 降冪 } int main(){ vector<int> vec = {5, 4, 8, 7}; sort(vec.begin(), vec.end(), cmp1); } ``` --- <h2 class="text-center"> 基本題型講解 </h2> ---- <h3 class="text-left"><a href="https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=54"> 00118 - Mutant Flatworld Expolrers </a></h3> 照題目模擬就好了 :::spoiler <font color="#191919">提示</font> ```cpp int dir[4][2] = { // 方向可以這樣寫 {0, 1 }, {1, 0 }, {0, -1}, {-1, 0 } }; ``` ::: ---- <h3 class="text-left"><a href="https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=998"> 10057 - A mid-summer nights dream </a></h3> 找完中位數之後 去陣列裡找答案 <a href="https://www.youtube.com/watch?v=lYBUbBu4W08" style="color: #191919;">提示</a> ---- <h3 class="text-left"><a href="https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=14&page=show_problem&problem=1163"> 10222 - Decode the Mad man </a></h3> 給你一個加密方式,要你解密 但其實是鍵盤位置移兩位 :::spoiler <font color="#191919">提示</font> 可以用 `string` 與 `map` 建表 ::: ---- <h3 class="text-left"><a href="https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1193"> 10252 - Common Permutation </a></h3> 找出兩字串中,都出現過的字元 :::spoiler <font color="#191919">提示</font> 這題測資有空白字元 ::: ---- <h3 class="text-left"><a href="https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=6&page=show_problem&problem=431"> 00490 - Rotating Sentences </a></h3> 轉置字串 ---- <h3 class="text-left"><a href="https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=4&page=show_problem&problem=208"> 00272 - TEX Quotes </a></h3> 把符號替換並輸出 ---- <h3 class="text-left"><a href="https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=1003"> 10062 - Tell me the frequencies! </a></h3> 照 ASCII 順序輸出每個字母出現的次數 :::spoiler <font color="#191919">提示</font> 用 `map` ::: ---- <h3 class="text-left"><a href="https://onlinejudge.org/index.php?option=onlinejudge&Itemid=8&page=show_problem&problem=1167"> 10226 - Hardwood Species </a></h3> 計算樹種出現的比例 :::spoiler <font color="#191919">提示</font> 用 `map` ::: --- 以上是本章內容 <a href="https://github.com/ShanCisgood/cpp_for_uva" style="color: #191919">答案在這裡</a>
{"title":"字串與STL","description":"string","slideOptions":"{\"transition\":\"fade\"}","contributors":"[{\"id\":\"4f67a8cd-06ae-45dc-a8e3-62c6a41e5a37\",\"add\":8296,\"del\":2860},{\"id\":\"dbf3352a-4a61-4739-9e70-396b6f72e250\",\"add\":179,\"del\":451}]"}
    121 views
   Owned this note