Standard Template Library,縮寫:STL,包含 algorithms, containers, functions, 和 iterators.
描述、優點
#include <iostream>#include <string>#include <map>using namespace std;int main(){ // map宣告,key, value分別為string型態 map<string, string> mapStudent; // iterator宣告,反向iterator宣告 map<string, string>::iterator iter; map<string, string>::reverse_iterator iter_r; //insert element mapStudent.insert(pair<string, string>("r000", "student_zero")); //insert element by array style mapStudent["r123"] = "student_first"; mapStudent["r456"] = "student_second"; //取得map大小 int nSize = mapStudent.size(); //順向traversal //若iter沒事先宣告可以用 auto it = mapStudent.begin() 代替 for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++) cout<<iter->first<<" "<<iter->second<<endl; //逆向traversal for(iter_r = mapStudent.rbegin(); iter_r != mapStudent.rend(); iter_r++) cout<<iter_r->first<<" "<<iter_r->second<<endl; //順向traversal, array方式 for(int nIndex = 1; nIndex<= nSize; nIndex++) cout<<mapStudent[nIndex]<<endl; // 刪掉 "r456", return 1 if success else return 0 int n = mapStudent.erase("r456"); //找 "r123" 位置, 若找不到回傳 mapStudent.end()位置 iter = mapStudent.find("r123"); if(iter != mapStudent.end()) //印出 "r123" 的內容 "student_first" cout<<"Find, the value is "<<iter->second<<endl; //刪掉 "r123" mapStudent.erase(iter); else cout<<"Do not Find"<<endl; //清掉整個map mapStudent.clear(); //另一種清掉整個map方法,順向traversal刪除 //mapStudent.erase(mapStudent.begain(), mapStudent.end()); return 0;}描述、優點
缺點
#include <iostream> #include <list> using namespace std; typedef list<int> INTLIST; //順向Transversal list void put_list(INTLIST list, char *name) { INTLIST::iterator plist; cout << "The contents of " << name << " : "; for(plist = list.begin(); plist != list.end(); plist++) cout << *plist << " "; cout<<endl; } void main(void) { //宣告一個空的 list1 INTLIST list1; //宣告一個 list2 裡面有10個值為6的element INTLIST list2(10,6); //宣告一個 list3 裡面有3個值為6的element INTLIST list3(list2.begin(),--list2.end()); //iterator宣告 INTLIST::iterator i; //順向transversal list內所有element put_list(list1,"list1"); put_list(list2,"list2"); put_list(list3,"list3"); //在list1後面插入兩個element list1.push_back(2); list1.push_back(4); cout<<"list1.push_back(2) and list1.push_back(4):"<<endl; put_list(list1,"list1"); //在list1前面插入兩個element list1.push_front(5); list1.push_front(7); cout<<"list1.push_front(5) and list1.push_front(7):"<<endl; put_list(list1,"list1"); //在list1的第二個位置插入3個值為9的element list1.insert(++list1.begin(),3,9); cout<<"list1.insert(list1.begin()+1,3,9):"<<endl; put_list(list1,"list1"); //取回list中的第一個值和最後一個值 cout<<"list1.front()="<<list1.front()<<endl; cout<<"list1.back()="<<list1.back()<<endl; //從list1序列的前後各刪掉一個element list1.pop_front(); list1.pop_back(); cout<<"list1.pop_front() and list1.pop_back():"<<endl; put_list(list1,"list1"); //清除list1中的第2个元素 list1.erase(++list1.begin()); cout<<"list1.erase(++list1.begin()):"<<endl; put_list(list1,"list1"); //給list2值並顯示 list2.assign(8,1); cout<<"list2.assign(8,1):"<<endl; put_list(list2,"list2"); //顯示list1的最max_size、size、是否為空 cout<<"list1.max_size(): "<<list1.max_size()<<endl; cout<<"list1.size(): "<<list1.size()<<endl; cout<<"list1.empty(): "<<list1.empty()<<endl; //list容器的運算 put_list(list1,"list1"); put_list(list3,"list3"); cout<<"list1>list3: "<<(list1>list3)<<endl; cout<<"list1<list3: "<<(list1<list3)<<endl; //對list1排序 list1.sort(); put_list(list1,"list1"); //結合處理 list1.splice(++list1.begin(), list3); put_list(list1,"list1"); put_list(list3,"list3"); } The contents of list1 : The contents of list2 : 6 6 6 6 6 6 6 6 6 6 The contents of list3 : 6 6 6 6 6 6 6 6 6 list1.push_back(2) and list1.push_back(4):The contents of list1 : 2 4 list1.push_front(5) and list1.push_front(7):The contents of list1 : 7 5 2 4 list1.insert(list1.begin()+1,3,9):The contents of list1 : 7 9 9 9 5 2 4 list1.front()=7list1.back()=4list1.pop_front() and list1.pop_back():The contents of list1 : 9 9 9 5 2 list1.erase(++list1.begin()):The contents of list1 : 9 9 5 2 list2.assign(8,1):The contents of list2 : 1 1 1 1 1 1 1 1 list1.max_size(): 768614336404564650list1.size(): 4list1.empty(): 0The contents of list1 : 9 9 5 2 The contents of list3 : 6 6 6 6 6 6 6 6 6 list1>list3: 1list1<list3: 0The contents of list1 : 2 5 9 9 The contents of list1 : 2 6 6 6 6 6 6 6 6 6 5 9 9 The contents of list3 : 描述、優點
缺點
示意圖
#include <iostream>#include <vector>using namespace std;int main() { vector<int> number = {10, 20, 30}; //以下提供三種不同取vecotr中值的方法 for(auto n : number) { //使用 for range cout << n << endl; } for(int i = 0; i < number.size(); i++) { //直接取值 //不做邊界檢查,可能會Segmentation Fault, cout << number[i] << endl; } for(int i = 0; i < number.size(); i++) { //用at取值 //at() 會做邊界檢查,越界時會丟出 out_of_range 異常 //比較容易 debug,必要時也可以把這個異常接住來處理。 cout << number.at(i) << endl; } for(vector<int>::iterator it = number.begin(); it != number.end(); it++) { //使用itorator auto n = *it; cout << n << endl; } //底下會顯示 9 到 0 的數字 vector<int> tmp; for(int i = 0; i < 10; i++) { tmp.push_back(i); } while(!tmp.empty()) { int n = tmp.back(); tmp.pop_back(); cout << n << endl; } // push_back()用法 vector <int> a; a.push_back(2); a.push_back(3); a.push_back(1); for(int i = 0; i<3; i++){ cout << a.at(i) << endl; } return 0;}//執行結果:(此處換行部分經過手動縮減)10 20 3010 20 3010 20 309 8 7 6 5 4 3 2 1 02 3 1#include <algorithm> //排序、尋找、反轉#include <iostream> #include <vector>using namespace std; int main() { vector<int> number = {30, 12, 55, 31, 98, 11, 41, 80, 66, 21}; // 排序 sort(number.begin(), number.end()); for(auto n : number) { cout << n << " "; } cout << endl; cout << "輸入搜尋值:"; int search = 0; cin >> search; vector<int>::iterator it = find(number.begin(), number.end(), search); cout << (it != number.end() ? "找到" : "沒有") << "搜尋值" << endl; // 反轉 reverse(number.begin(), number.end()); for(auto n : number) { cout << n << " "; } cout << endl; return 0; }//執行結果:11 12 21 30 31 41 55 66 80 98輸入搜尋值:22沒有搜尋值98 80 66 55 41 31 30 21 12 11#include <vector>#include <iostream>using namespace std;class NODE{ public: char symbol; int count; };int main(){ NODE temp; vector<NODE> gem_list; temp.symbol = 'a'; temp.count = 0; gem_list.push_back(temp); temp.symbol = 'b'; temp.count = 1; gem_list.push_back(temp); // .. 經過幾次push_back for(int i=0; i<gem_list.size(); i++) cout<<gem_list[i].symbol<<" "<<gem_list[i].count<<endl; return 0;}//執行結果:a 0b 1struct monster { DWORD id; int x; int y; int distance; int HP;};std::vector<monster> tmp;DWORD monster = 0xFFFAAA;it = std::find(bot.tmp.begin(), bot.tmp.end(), currentMonster);struct find_monster{ DWORD id; find_monster(DWORD id) : id(id) {} bool operator () ( const monster& m ) const { return m.id == id; }};it = std::find_if( tmp.begin(), tmp.end(), find_monster(monsterID));參考資料
https://mropengate.blogspot.com/2015/12/cc-map-stl.htmlhttps://ithelp.ithome.com.tw/articles/10231350http://www.cplusplus.com/reference/vector/vector/https://mropengate.blogspot.com/2015/07/cc-vector-stl.htmlhttps://openhome.cc/Gossip/CppGossip/vector1.htmlhttps://edisonx.pixnet.net/blog/post/93629406-%5Bstl-note%5D-vector-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A0%85-&-faq