#include #include #include #include using namespace std; int main(int argc, char** argv) { //1.容器Container有兩種: //(1)Sequential Container序列容器:Vector,List,deque // *使用 index // 優點:方便新增,刪除item //(2)Associative Container 關聯容器:Set, Map, // *使用 key-value paris // 優點:方便搜尋 //2.Vector = 動態陣列,很像array,但可以push back動態在最後尾部新增,刪除資料 //優點:適合在『尾部』新增,刪除一筆資料 //缺點:在『頭部,中間』新增,刪除資料比較沒有效率(修改一個,其它後面的都要修正index) //缺點:搜尋資料是逐筆比對,效率差 //3.List = 是很多獨立的記憶體空間(空間內有2個值,data,index), //特色:靠index指向另一個記憶體空間,所以list是一種雙向鏈表串接 //優點:適合在『頭部,中間,尾部』的新增,刪除效率 // insert一筆資料,只要修正兩個空間的鏈接index即可,其它都不要修正 //缺點:搜尋資料要逐筆比對,而且還要把記憶體空間值抓到cache,所以搜尋效率最差 // 4.宣告a1是list資料鏈接 list a1; //5.尾部加入item: push_back a1.push_back(90); a1.push_back(85); a1.push_back(95); a1.push_back(100); a1.push_back(70); //6.頭部加入item: push_front a1.push_front(60); a1.push_front(65); a1.push_front(91); //6.顯示全部list //錯誤寫法:list不支援取得因素的陣列寫法:a1[i] //原因:list不是連續的記憶體空間,無法用a1[0],a1[1]... //for(int i=0;i::iterator itr; for(itr=a1.begin(); itr!=a1.end(); itr++) { cout<<(*itr)<<'\t'; } //7.刪除尾部1筆: pop_back a1.pop_back(); //8.刪除頭部1筆: pop_back a1.pop_front(); cout< a2(10, 0); cout<