stl list使用。

來源:互聯網
上載者:User

什麼是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;
}

 

這時會報 二進位“<<”: 沒有找到接受“std::basic_string<_Elem,_Traits,_Ax>”類型的右運算元的運算子(或沒有可接受的轉換) 的錯誤。

因為string沒有cout輸出重載,應該改為 pstrlist

 

 

需要注意的是: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的編號   

其它的同理操作就行了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.