c 容器(list學習總結)

2022-07-08 13:39:16 字數 3628 閱讀 7192

list是乙個線性雙向鍊錶結構,它的資料由若干個節點構成,每乙個節點都包括乙個資訊塊(即實際儲存的資料)、乙個前驅指標和乙個後驅指標。它無需分配指定的記憶體大小且可以任意伸縮,這是因為它儲存在非連續的記憶體空間中,並且由指標將有序的元素鏈結起來。由於其結構的原因,list 隨機檢索的效能非常的不好,因為它不像vector 那樣直接找到元素的位址,而是要從頭乙個乙個的順序查詢,這樣目標元素越靠後,它的檢索時間就越長。檢索時間與目標元素的位置成正比。雖然隨機檢索的速度不夠快,但是它可以迅速地在任何節點進行插入和刪除操作。因為list 的每個節點儲存著它在鍊錶中的位置,插入或刪除乙個元素僅對最多三個元素有所影響,不像vector 會對操作點之後的所有元素的儲存位址都有所影響,這一點是vector 不可比擬的。

list 的特點:

(1) 不使用連續的記憶體空間這樣可以隨意地進行動態操作;

(2) 可以在內部任何位置快速地插入或刪除,當然也可以在兩端進行push 和pop 。

(3) 不能進行內部的隨機訪問,即不支援[ ] 操作符和vector.at() ;

(4) 相對於verctor 占用更多的記憶體。

初學list:需要掌握的知識:

(1)定義乙個list 

(2)向list中加入元素 

(3)如何知道list是否為空 

(4)如何使用for迴圈來遍歷乙個list 

(5)如何使用stl的通用演算法for_each來遍歷list 

(6)list成員函式begin() 和 end() 以及它們的意義 

(7)iterator範圍的概念和乙個範圍的最後乙個位置實際上並不被處理這一事實 

第一:定義,插入,遍歷列印。

**實現如下:

#include

#include

#include

#include

#include

using namespace std;

void printit(string& stringtopoint)

int main()

listtest;

list::iterator testiterator;

test.push_back("no");

test.push_back("march");

test.push_front("ok");

test.push_front("loleina");

test.push_front("begin");

test.push_back("end");

for (testiterator = test.begin(); testiterator != test.end(); ++testiterator)

cout << "-------------" << endl;

for_each(test.begin(), test.end(), printit);

cout << "-------------" << endl;

system("pause");

return 0;

}定義了乙個字串型別的list。需要包含提供stl的 list類的標頭檔案#include <list>即可;list的成員函式push_back()把乙個物件放到乙個list的後面,而 push_front()把物件放到前面。

我們想要遍歷乙個list,比如列印乙個list中的所有物件來看看list上不同操作的結果。要乙個元素乙個元素的遍歷乙個list, 可以這樣做:

a. 這個程式定義了乙個iterator(類似指標),testiterator。它指向了這個list的第乙個元素。 這可以呼叫testiterator.begin()來做到,它會返回乙個指向list開頭的iterator。然後把它和testiterator.end()的 返回值來做比較,到了那兒的時候就停下來。 容器的end()函式會返回乙個指向容器的最後乙個位置的iterator。 在上面的例子中,每一次執行for迴圈,我們就重複引用iterator來得到我們列印的字串。

注意:不能用testiterator.begin()+2來指向list中的第三個物件,因為stl的list是以雙鏈的list來實現的,所有的資料存放不一定是連續存放的。 它不支援隨機訪問。

b.使用stl的通用演算法for_each()來遍歷乙個iterator的範圍,然後呼叫printit()來處理每個物件。 不需要初始化、比較和給iterator增量。for_each()完成了這些工作。執行於物件上的操作被很好的 打包在這個函式以外了,不用再做那樣的迴圈了,**更加清晰了。 

第二:count()和count_if() 的基本使用

stl的通用演算法count()和count_it()用來給容器中的物件記數。就象for_each()一樣,count()和count_if() 演算法也是在iterator範圍內來做的。

#include

#include

#include

#include

#include

using namespace std;

class isloleina

};int main()

{listtest;

listscore;

list::iterator testiterator;

test.push_back("no");

test.push_back("march");

test.push_front("ok");

test.push_front("loleina");

test.push_front("begin");

test.push_back("end");

score.push_back(100);

score.push_back(90);

score.push_back(80);

score.push_back(70);

score.push_back(100);

score.push_back(20);

int countnum(0);

countnum= count(score.begin(), score.end(), 100);

cout << "there are " << countnum << " scores of 100" << endl;

cout << "-------------" << endl;

int countloleina(0);

countloleina=count_if(test.begin(), test.end(), isloleina());

cout << "there are " << countloleina << " loleina" << endl;

system("pause");

return 0;

count()演算法統計等於某個值的物件的個數。count_if() 帶乙個函式物件的引數。函式物件是乙個至少帶有乙個operator()方法的類。有些stl演算法作為引數接收函式物件並呼叫這個函式物件的operator()方法。函式物件被約定為stl演算法呼叫operator時返回true或false。它們根據這個來判定這個函式。舉個例子會 說的更清楚些。count_if()通過傳遞乙個函式物件來作出比count()更加複雜的評估以確定乙個物件是否應該被記數。

list容器總結

1.關於list容器 list是一種序列式容器。list容器完成的功能實際上和資料結構中的雙向鍊錶是極其相似的,list中的資料元素是通過鍊錶指標串連成邏輯意義上的線性表,也就是list也具有鍊錶的主要優點,即 在鍊錶的任一位置進行元素的插入 刪除操作都是快速的。list的實現大概是這樣的 list...

c 中list容器學習

c list用法 所屬命名空間 using system.collections.generic list類是 arraylist 類的泛型等效類。該類使用大小可按需動態增加的陣列實現 ilist泛型介面。泛型的好處 它為使用 c 語言編寫物件導向程式增加了極大的效力和靈活性。不會強行對值型別進行裝...

C 學習筆記 List容器

1.雙向鍊錶容器 2.不能隨機存放元素,不支援at.pos 函式與 操作符,可以it 但不能it n include using namespace std include void main cout l.size endl list iterator it l.begin while it l....