stl -...
TRANSCRIPT
![Page 1: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/1.jpg)
STLDoraemon 6/6
![Page 2: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/2.jpg)
STL ? 能吃嗎:D?
![Page 3: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/3.jpg)
來回想⼀一下list …
• 加⼊入⼀一個結點 ....
• Node *new_node = new Node();
• new_node->next = head->next;
• head->next = new_node;
![Page 4: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/4.jpg)
想想動態記憶體配置...
• int *new_array;
• new_array = new int[100];
• delete [] new_array;
![Page 5: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/5.jpg)
嘿~排序~• for(int i = 0; i < n; ++i)
• for(int j = 1; j < n; ++j)
• if (s[j] < s[j-1]){
• tmp = s[i];
• s[i] = s[j];
• s[j] = tmp;
• }
![Page 6: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/6.jpg)
~⽂文字轉轉轉~• char select[100], word[100];
• bool visit[100];
• void rotate(int step){
• if(step == length) { … }
• else { .. code 好⻑⾧長省略啦~ .. }
• }
![Page 7: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/7.jpg)
難道不能偷懶嗎orz...
![Page 8: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/8.jpg)
偷懶技術概論• Standard Template Library
• Container
• Vector, list, string
• Iterator
• Algorithm
• sort, next_permutation, lower_bound
![Page 9: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/9.jpg)
警告
• 使⽤用此技巧容易使⼈人變得懶散
• 勤勞者請勿過度使⽤用
• 懶散者使⽤用後會變成懶惰之神
• 使⽤用前請三思
![Page 10: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/10.jpg)
使⽤用前須知
• 要記得 #include ⼀一些東⻄西噢~~~
• STL 屬於 namespace std;
• 跟cin, cout ⼀一樣,在前⾯面加上std ::
• 或者加上using namespace std;
![Page 11: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/11.jpg)
C++ string
#include <string>
![Page 12: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/12.jpg)
string• 宣告!
#include <string>!using namespace std;!!
string str1;! ! //空字串!string str2("bird");!//string str2 = "bird"; !string str3(str2);
![Page 13: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/13.jpg)
string• 指定: =! str1 = str2;!• ⽐比較: ==, !=, <, <=, >, >=! if (str1 == str2)!! cout << “str1 == str2";!!
if (str2 > str3)!! cout << “str2 > str3” ;
![Page 14: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/14.jpg)
string
• 串接: +!
• str1 = str1 + str2;!
• str1 = str1 + " yo”;!
• string str4 = "I'm" + " happy.”!
• str4 = str4 + " " + "Yo.";
![Page 15: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/15.jpg)
string
• 取值 string str1("tree");
cout << str1[0] << " ";
cout << str1[1] << " ";
cout << str1[2] << " ";
cout << str1[3] << " ";
![Page 16: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/16.jpg)
string的各種操作• size(), length()!
• string str1(“tree");!
• cout << str1.size() << endl;!
• cout << str1.length() << endl;
![Page 17: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/17.jpg)
string的各種操作• 取得⼦子字串!
substr(pos, n)!
substr(pos)!
substr()
![Page 18: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/18.jpg)
string的各種操作EX : !
string str("tree");!
cout << str.substr(0, 2) << endl;!
cout << str.substr(1) << endl;!
cout << str.substr() << endl;
![Page 19: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/19.jpg)
string的各種操作• 尋找! find(c)!
find(str)!
find(c, pos)!
find(str, pos)!
會回傳找到的第⼀一個位置,! 若找不到會回傳string::npos,! npos為"絕對⽐比任何有效索引還⼤大"的值
![Page 20: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/20.jpg)
string的各種操作Ex: !
string str("I love the tree.");!
cout << str.find(“ee") < <endl;!
cout << str.find('t', 8) << endl;!
cout << str.find("love", 6) << endl;
![Page 21: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/21.jpg)
伸縮⾃自如的陣列 Vector
#include <vector>
![Page 22: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/22.jpg)
Vector
• 動態陣列
• 不⽤用⾃自⼰己⼿手動 new / delete
• ⽀支援隨機插⼊入,但是很慢,後⾯面會講到list
• ⽀支援隨機存取,可以當陣列使⽤用:D
![Page 23: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/23.jpg)
Vector 宣告• std::vector <資料形態> 變數名稱;
• 資料形態:int, double, string, ⾃自訂形態..
• EX.
• std::vector < int > V;
• std::vector < string > V;
• std::vector < Node > V;
![Page 24: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/24.jpg)
Vector 使⽤用⽅方法• size() : 回傳 vector 的⼤大⼩小
• clear() : 把陣列裡⾯面的東⻄西清光
• [] —> 中括號 : 跟陣列⼀一樣
• Ex. V[0], V[1], V[2] . . . .
• push_back(東⻄西) : 在尾巴加⼊入⼀一個東⻄西
• pop_back() : 從尾巴拔掉⼀一個元素
![Page 25: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/25.jpg)
展⽰示 Vector
![Page 26: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/26.jpg)
鏈表 list
#include <list>
![Page 27: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/27.jpg)
list
• 就是你知道的那個寫起來很痛苦的linked-list
• 可以隨機插⼊入:D
• 不能隨機存取,不能當陣列⽤用:(
![Page 28: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/28.jpg)
list 宣告• std::list <資料形態> 變數名稱;
• 資料形態:int, double, string, ⾃自訂形態..
• EX.
• std::list < int > L;
• std::list < string > L;
• std::list < Node > L;
![Page 29: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/29.jpg)
list ⽤用法• size() : 回傳裡⾯面有幾個東⻄西(回傳⼤大⼩小)
• clear() : 清空
• push_front(東⻄西) : 把東⻄西塞在前⾯面
• push_back(東⻄西) : 把東⻄西塞在後⾯面
• pop_front() : 從前⾯面拔掉⼀一個東⻄西
• pop_back() : 從後⾯面拔掉⼀一個東⻄西
![Page 30: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/30.jpg)
展⽰示 list
![Page 31: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/31.jpg)
迭代器 Iterator
![Page 32: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/32.jpg)
Iterator
• 他看起來就是很像指標
• 可以指到容器(container)的某個元素
• 不⽤用管container到底是怎麼做的
![Page 33: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/33.jpg)
Iterator 宣告
• container <type>::iterator 變數;
• container : vector, list, string ……
• type : int, char, double, ⾃自訂形態...
• 要和被指到的container的type⼀一樣
![Page 34: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/34.jpg)
Iterator ⽤用法(1)• EX. std::string str;
• std::string::iterator iter = str.begin();
• *iter == str[0];
• iter++;
• *iter == str[1];
![Page 35: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/35.jpg)
Iterator ⽤用法(2)
• std::vector <int> V;
• std::vector <int>::iterator iter;
• for(iter = V.begin(); iter != V.end(); ++iter)
• std::cout << *iter << std::endl;
![Page 36: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/36.jpg)
Iterator ⽤用法注意
• 如果你指到的那個位置被刪掉(pop_back(), 之類的
• Iterator 不保證他指到的地⽅方是對的喔
• 參考資料在這裡:
• http://stackoverflow.com/questions/6438086/iterator-invalidation-rules
![Page 37: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/37.jpg)
Iterator Demo
![Page 38: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/38.jpg)
課堂練習 190
![Page 39: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/39.jpg)
Algorithm#include <algorithm>
![Page 40: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/40.jpg)
Algorithm
• 裡⾯面有超多好⽤用的⼯工具
• 你只要⽤用過就會上癮(?)
![Page 41: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/41.jpg)
sort
• 對 int arr[100];
• sort(arr, arr + 100);
• 對 vector<int> V;
• sort(V.begin(), V.end());
![Page 42: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/42.jpg)
lower_bound• 找到某個數字的下界(( 還記得⼆二分搜嗎XD“~
• 他回傳的是container某個元素的位置!!!
• 對 int arr[100], x = 100;
• int *ptr = lower_bound(arr, arr+100, value);
• 對 vector<int> V;
• vector<int>::iterator lb;
• lb = lower_bound(V.begin(), V.end(), x);
• int distance = lb - V.begin(); //距離頭多遠
![Page 43: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/43.jpg)
next_permutation• 下⼀一個排列
• ⼀一開始請記得把它排好,否則 … 嘿嘿
• next_permutation(begin, end);
• 要⽤用 do { . . . . . . . . .} while(next_permutation(…));
![Page 44: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/44.jpg)
Algorithm Demo
![Page 45: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/45.jpg)
好⽤用的⼯工具很多:D
想要知道更多…
請⾃自⼰己Google (X)
![Page 46: STL - tw-csie-sprout.github.iotw-csie-sprout.github.io/.../pages/uploads/presentations/week13/STL.pdf• STL 屬於 namespace std; ... • std::vector < Node > V; Vector 使法](https://reader030.vdocuments.mx/reader030/viewer/2022040106/5e03d9f080d83f417f4e2679/html5/thumbnails/46.jpg)
課堂練習 261