1. 程式人生 > >C++ STL學習筆記四 list雙向連結串列容器

C++ STL學習筆記四 list雙向連結串列容器

/*
 *
 ********************************************
 *   list雙向連結串列容器的基礎說明:
 ********************************************
 *
 * list雙向連結串列容器採用雙向連結串列的資料結構來儲存元素資料,可以高效查詢、插入、刪除容器元素
 *
 * Reversibe Container  Back Insertion Sequence  Front Insertion Sequence
 * 不同於vector,list查詢、插入、刪除元素的時間複雜度均為O(1)
 * 
 * 使用list必須使用巨集語句#include <list>
 *
 **************************************************************************************
 *
 * 建立list物件:
 * 1.list<int> a;
 * 2.list<int> a(10);      //具有10個元素的物件a,每個元素預設值為0
 * 3.list<char> a(5,'k');
 * 4.list<char> b(a);      //list<char> c(a.begin(),a.end())
 *
 **************************************************************************************
 *
 * 初始化賦值
 * void push_back(const T& value)
 *
 **************************************************************************************
 *
 * 遍歷訪問
 * iterator begin()   //只能使用迭代器的方式進行遍歷
 * iterator end()
 *  iterator rbegin();   //反向遍歷
 *  iterator rbegin();
 * 
 **************************************************************************************
 *
 * 常用函式
 *
 * bool empty();
 * 
 * void pop_front(); void pop_back();
 * 
 * void push_front(const T&); void push_back(const T&);
 * iterator insert(iterator pos,const T&x); //注意它是插入到pos前
 *
 * iterator erase(iterator pos);
 * iterator erase(iterator first,iterator last);
 * void clear();
 * void remove(const T& value);
 *
 * void swap()         //通過交換頭指標來實現元素的交換
 * //list內部提供的一個遷移操作
 * void transfer();       //transfer(iterator position,iterator first,iterator last)在A連結串列position位置前插入
 *            //B連結串列迭代器區間[first,last)的元素,並將這部分元素從B連結串列中抹去
 * void splice(iterator pos,list& X);   //呼叫transfer函式將一個連結串列的所有元素全部歸併到當前連結串列,
 *            //並將連結串列物件X清空,
 * void splice(iterator pos,list& X,iterator i); //將X中迭代器i所指的元素歸併到當前連結串列pos前,並將X中i所指元素抹去
 * 
 * void merge(list&X); //對兩個連結串列進行歸併,要求先排序,否則merge沒有太大意義
 *
 * void unique();  //可將連續重複的元素刪除,只保留一個
 *
 *
 *
 ********************************************
 * Author: cumirror   
 * Email: 

[email protected]
 ********************************************
 *
 */

#include <list>
#include <iostream>   
#include <string>   
using namespace std;

struct student{
 char* name;
 int age;
 char* city;
 char* phone;
};

class studentNew{
public:
 char name[10];
 int age;
 char city[10];
 char phone[11];
 studentNew(){}
 studentNew(char* a,int b,char* c,char* d){
  strcpy(name,a);
  age=b;
  strcpy(city,c);
  strcpy(phone,d);
 }

//為何友元函式在類外定義,出現錯誤error C2593: 'operator >' is ambiguous
//friend bool operator < (studentNew&a,studentNew& b);
//friend bool operator > (studentNew&a,studentNew& b);
//friend bool operator == (studentNew&a,studentNew& b);

friend bool operator< (studentNew& a,studentNew& b){
 return strcmp(a.name,b.name)<0;
}
friend bool operator> (studentNew& a,studentNew& b){
 return strcmp(a.name,b.name)>0;
}
friend bool operator== (studentNew& a,studentNew& b){
 return strcmp(a.name,b.name)==0;
}

bool operator() (studentNew& a,studentNew& b){
 return strcmp(a.name,b.name)==-1;
}
};
/*
bool operator < (studentNew& a,studentNew& b){
 return strcmp(a.name,b.name)<0?true:false;
}
bool operator > (studentNew& a,studentNew& b){
 return strcmp(a.name,b.name)>0?true:false;
}
bool operator == (studentNew& a,studentNew& b){
 return strcmp(a.name,b.name)==0?true:false;
}
*/

int main(){
/* list<int> a;
 a.push_back(4);
 a.push_back(3);
 a.push_back(2);
 a.push_back(8);
 a.push_back(7);
 a.push_back(5);
 a.push_back(6);
 a.push_back(9);
 a.push_back(10);
 a.push_back(1);

// list的sort函式實現方法,如下

 list<int> carry;
 list<int> counter[64];
 int fill=0;
 while(!a.empty()){
 carry.splice(carry.begin(),a,a.begin());
 int i=0;
 while(i<fill&&!counter[i].empty()){
  counter[i].merge(carry);
  carry.swap(counter[i++]);
 }
 carry.swap(counter[i]);
 if(i==fill)++fill;
 }
 for(int i=1;i<fill;++i){
  counter[i].merge(counter[i-1]);
  a.swap(counter[fill-1]);
 }
 for(list<int>::iterator j=a.begin();j!=a.end();j++){
  cout<<*j<<endl;
 }
*/
// 其它函式使用示例,如下:

 student s[]={
  {"童進",23,"武漢","XXX"},
  {"老大",23,"武漢","XXX"},
  {"餃子",23,"武漢","XXX"}
 };
 list<student> classA;
 classA.push_back(s[0]);
 classA.insert(classA.begin(),s[1]);
 classA.push_back(s[3]);
 cout<<classA.begin()->name<<endl;
// classA.sort();                //對於自定義結構體,沒有過載</>/==這些操作符,故無法排序

// 自己建立一個新類studentNew過載操作符再進行判斷
 studentNew m1("童進",23,"武漢","XXX");
 studentNew m2("老大",23,"武漢","XXX");
 studentNew m3("餃子",23,"武漢","XXX");
 list<studentNew> classNew;
 classNew.push_back(m1);
 classNew.push_back(m2);
 classNew.push_back(m3);
// 判斷友元函式是否起作用
 if( m1>m2 ){
  cout<<"新類中操作已經過載成功"<<endl;
 }
// 運用SGI STL庫時
// 用函式物件studentNew,替換了greater<T>
 classNew.sort(studentNew());
// 若用VC自帶的STL庫時,下面這樣書寫即可實現.至於為何有這樣的差異,目前我還不知
// classNew.sort(); 
 for(list<studentNew>::iterator m=classNew.begin();m!=classNew.end();m++){ //通過結果可以看出
  cout<<m->name<<endl;             //classNew已經進行了重新排列
 }                              

 list<string> classB;
 classB.push_back("童");
 classB.push_back("蘭");
 classB.push_front("張");
 classB.push_back("童");
 classB.sort();                //對於string型別,有預設型別的greater<T>
 classB.unique();               //剔除重複資料
 for(list<string>::iterator k=classB.begin();k!=classB.end();k++){
  cout<<*k<<endl;
 }
 return 0;
}

相關推薦

C++ STL學習筆記 list雙向連結串列容器

/* * ******************************************** *   list雙向連結串列容器的基礎說明: ******************************************** * * list雙向連結串列容器採用雙向

C++ STLlist雙向連結串列容器

不同於採用線性表順序儲存結構的vector和deque容器,list雙向連結串列中任一位置的元素查詢、插入和刪除,都具有高效的常數階演算法時間複雜度O(1)。 list技術原理 為了支援前向和反向訪問list容器的元素,list採用雙向迴圈的連結串列結構組織資

C++STLlist雙向連結串列容器

list容器實現了雙向連結串列的資料結構,資料元素是通過連結串列指標串成邏輯意義上的線性表,選擇對連結串列的任一位置的元素進行插入,刪除和查詢都是非常高效的。 list的每個節點有三個域:前驅元素指標域,資料域,後繼元素指標域,前驅元素的指標域儲存了前驅元

C++ STL 容器技術 之 list雙向連結串列容器

簡介: list是雙向連結串列的一個泛化容器,它的資料元素可通過連結串列指標串接成邏輯意義上的線性表。不同於採用線性表順序儲存結構的vector和deque容器,list雙向連結串列中任一位置的元素查詢、插入和刪除,都具有高效的常數階演算法時間複雜度O(1)。 lis

[C++ 從入門到放棄-07]C++STLlist雙向連結串列容器

學過資料結構都知道,其中有一章專門講線性表,其中有兩塊,一是順序表(也就是我們平時用的比較多的陣列,結構陣列),二是連結串列(有指標在,想想都複雜)。而C++ STL中給我們封裝好了一個list容器,

list雙向連結串列容器

list容器實現了雙向連結串列的資料結構,資料元素是通過連結串列指標串聯成邏輯意義上的線性表,因此對連結串列的任一位置的元素進行插入、刪除和查詢都是極快的。 list每個節點有三個域:前驅元素指標域、資料域、後繼元素指標域,list的頭結點的前驅元素指標域儲存的是連結串列中

C++|STL學習筆記-對STL中關聯容器map的進一步認識

關聯容器map key + value 的值 關聯容器 = 有序容器(紅黑樹) + 無序容器(散列表) + hash_map 有序容器中: map的鍵值是不允許重複的 multimap的鍵值是允許重複的 set是一個集合,鍵值=實值,就是隻包含一個值,既是鍵值也是實值,不允許重複 mul

C++|STL學習筆記-map的基本操作(插入,刪除,遍歷,大到小輸出)【仿大佬寫法】

首先的程式碼是插入,刪除,遍歷 執行截圖如下: 原始碼如下: #include <map> #include <iostream> #include <algorithm> using namespace std; typedef pair

C++|STL學習筆記-map的屬性(大小以及是否存在)

目錄 1.size()的用法 map的property map屬性 1.沒有容量; 2.得到元素的個數size() 這裡給出呼叫他size()的例子,原始碼如下: /*****

C++STL隨手筆記(二)List容器的splice及std::partition用法

list容器內建splice這個函式,可以將容器依指定範圍切割, 以及std的partition可以對容器中的元素進行條件篩選並排續, 稱著記憶猶新趕快記錄下來用法。 #include <lis

c++ STL 學習筆記(map )

關於map 在STL的標頭檔案中<map>中定義了模版類map和multimap,用有序二叉樹表儲存型別為 pair<const Key, T>的元素對序列。序列中的元素以const Key部分作為標識,map中所有元素的Key 值必須是唯一的,multimap則

c++ STL 學習筆記(pair 和 set)

STL pair (1)pair 的定義 標頭檔案 <utility> STL的標頭檔案中描述了一個看上去非常簡單的模版類pair,用來表示一個二元組或元素對,並提供了按照字典序對元素對進行大小比較運算子模版函式。 定義一個pair物件表示一個平面座標點: 例:

C#回顧學習筆記十:三層架構

1)三層框架是什麼? 按照書籍和部落格文章裡千篇一律的解釋就是:UI層、BLL層、DAL層。這樣的解釋通常無法讓人一時半會理解含義,總而言之在這裡先大概說明一下三層架構。正如其他文章提到的一樣,三層架構就是:表示層(與使用者直接進行互動)、業務邏輯層(在表示層和資料訪問層中

嵌入式linux c 學習筆記9---雜湊連結串列

/*  * =====================================================================================  *  *       Filename:  hash.c  *  *    Descri

大話資料結構()——雙向連結串列的java實現

在實現了單向連結串列後,我們在使用單向連結串列中會發現一個問題:在單向連結串列中查詢某一個結點的下一個結點的時間複雜度是O(1),但是查詢這個結點的上一個結點的時候,時間複雜度的最大值就變成了O(n),因為在查詢這個指定結點的上一個結點時又需要從頭開始遍歷。 那麼該如何解決這個困難呢?

(未完成)STL學習筆記(3)序列式容器 (Sequence Containers)

1. vector vector與陣列array十分相似,但array是靜態空間,而vector是動態空間,可以通過內部機制自行擴充空間,具有很好的靈活性。 其實現的關鍵在於對大小的控制和重新配置時的資料移動效率。 vector的型別定義如下: templat

Glib學習(2) 雙向連結串列 Doubly-Linked Lists

首先貼出來glib庫的幫助文件網址 http://web.mit.edu/barnowl/share/gtk-doc/html/glib/glib-Doubly-Linked-Lists.html#g-list-find 由於雙向連結串列與單向連結串列的很多函式功能和名稱

STL中的(list連結串列

1 定義一個list #include <iostream> #include <string> #include <list> using namespace std; int main (void) { list<str

C/C++】【資料結構】雙向連結串列操作

目錄 標頭檔案定義 測試檔案 雙向連結串列操作 像雙向連結串列的求長,判空,遍歷,查詢,檢索,之類的操作都和單鏈表一樣的。不過我還是在了文中。 標頭檔案定義 #ifndef _DOUBLELINKLIST_H_ #def

學習筆記 5)靜態連結串列

靜態連結串列(static linked list):用陣列來描述的連結串列,用陣列元素的下標來模擬單鏈表的指標。這種描述方法叫做遊標實現法。 遊標 5 2 3 4 5 6 7 … 1 資料 A