初學STL 中list(轉載)

轉載出自:http://blog.sina.com.cn/weiweizaidongda

什麼是STL呢?

STL : standard template library,“標準模版庫”的意思。基本上可以這麼說,STL是一些“容器”的集合,是算法和一些組件的集合。這些算法和容器的集合是精英中的精英的傑作。它的目的是標準化組件,避免重複開發。

學習STL,我覺得應該從list學起,沒別的,就因爲它簡單,容易上手。

首先,我們要明白一些基本概念。

模板(Template):類、結構等的宏(macro)。正規的名字:範型(generic)。

                 一個類的模板叫:範型類(generic class)。

                一個函數的模板叫:範型函數(generic function)。

容器(Container):可容納一些數據的模版類。STL中有vector、set、map、multimap等

向量(Vector):基本數組模板,此乃一容器。

遊標(Iterator):是一指針,用來指向STL容器中的元素,也可指向其它元素。

本篇主要內容:定義和初始化一個list,計算它的元素的數量,從一個list裏查找、刪除元素等。

一、定義list。我們可以這樣定義一個list。

//........注意:這樣是錯誤的!!!..............這是我犯的第一個錯誤

#include   "string.h"

#include   "list.h"                     //這兒錯誤!!

using namespace std;

int main(void)

{

list<string>   strlist;

return 0;

}

編譯會出現如下錯誤:

include/list.h(37) : error C2146: syntax error : missing ';' before identifier 'Length'
include/list.h(37) : error C2501: 'DWORD' : missing storage-class or type specifiers
include/list.h(37) : error C2501: 'Length' : missing storage-class or type specifiers
include/list.h(53) : error C2146: syntax error : missing ';' before identifier 'GetPrevLink'
include/list.h(53) : error C2433: 'WINAPI' : 'inline' not permitted on data declarations
include/list.h(53) : fatal error C1004: unexpected end of file found

原因其實很簡單:"list"和"list.h" 是完全不同的,“list.h” 是c形式的雙向鏈表,而“list”是標準的c++的容器。

正確的應該這樣:

#include "string"

#include "list"

using namespace std;

int main(void)

{

list<string>   strlist;

return 0;

}

編譯你會發現一大隊錯誤不見了,取而代之的是一大堆警告。不同緊張,這是正常現象,有時候編譯器對標準c++ stl 支持不是太好,就會出現這種現象,完全可以不理會。

到目前爲止,我們只是定義了一個list,並沒有做任何事情。

現在我們來把一個字符串裝進這個list。再作之前,有一個東東需要我們瞭解,那就是“值類型”。

”值類型“ 就是list 中對象的類型,這兒是string 字符串。

我們可以使用list的成員函數push_back()、push_font()插入元素到list中。是這麼調用的:

#include "string.h"
#include "list"
using namespace std;


int main(void)
{
list<string> strlist;
strlist.push_back("waring1:");
strlist.push_back("exception");
strlist.push_front("有錯誤:");

return 0;
}

list的成員函數push_back()把一個對象放到一個list的後面,而 push_front()把對象放到前面。

我通常把一些錯誤信息push_back()到一個list中去,然後push_front()一個標題到list中,這樣它就會在這個錯誤消息以前打印它了。

可是我們怎麼使用我們放進去的元素呢?我們可以通過Iterator來訪問他們,( ^_^ 還記不記得Iterator是什麼?一指針而已!)

在具體操作中,我還有了額外的收穫。我本想輸出一個字符串,此字符串是string型,結果出現string 沒有重載<<的錯誤,查了一下才知道應該包含”string“   而不是”string.h“。由於C++是從C擴展而來,所以一般"XXX.h"是C頭文件,而“XXX”纔是C++的。這兒要注意了,不要跟我犯一樣的錯誤。

好,下面遍歷該list

#include "string"
#include "list"
#include "iostream"
using namespace std;


int main(void)
{
list<string> strlist;
strlist.push_back("waring1:");
strlist.push_back("exception");
strlist.push_front("有錯誤:");

list<string>::iterator   pstrlist;
pstrlist=strlist.begin();
for(;pstrlist!=strlist.end();pstrlist++)
{
   cout<<*pstrlist;
}

return 0;
}

需要注意的是:Iterator這個對象沒有重載 大於 >   或 小於 <       , 只有 !=

還有就是:在 list 容器中,我們不能用strlist.begin()+2來指向list中的第三個對象,因爲STL的list是以雙鏈的list來實現的,它不支持隨機存取。vector和deque(向量和雙端隊列)和一些其他的STL的容器可以支持隨機存取。

至此,我們對STL已經有了一個初步的認識,這僅僅是一個開端,只是皮毛。後面的更精彩。

STL 還爲我們設計了通用算法,這樣我們連循環都不用做了。哈哈,爽!

比如 上面的遍歷,就可以用 for_each(),他是STL爲我們做的,及其方便。

一定要記得包含頭文件“algorithm“

#pragma warning(disable:4786)

#include "string"
#include "list"
#include "iostream"
#include "algorithm"

using namespace std;

void print(string &str)
{
cout<<str;
}

int main(void)
{
list<string> strlist;
strlist.push_back("waring1:");
strlist.push_back("exception");
strlist.push_front("有錯誤:");


for_each(strlist.begin(),strlist.end(),print);

return 0;

}

這是for_each()的源碼,可以研究一下

template<class    _init,    class    _fn1>    inline   
   _fn1    for_each(_init    _first,    _init    _last,    _fn1    _func)   
   {   
                   //    perform    function    for    each    element   
                   for    (;    _first    !=    _last;    ++_first)   
                                   _func(*_first);   
                   return    (_func);   
   }   

這裏一個小技巧,如果編譯過程中,討厭warning4786,可以添加參數將其屏蔽掉。

#pragma    warning(    disable    :    xxxx)   
    
   xxxx是warning的編號   

其它的同理操作就行了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章