定義一個list
我們可以象這樣來定義一個STL的list:
#include <string> #include <list> int main (void) { list<string> Milkshakes; return 0; } |
這就行了,你已經定義了一個list。簡單嗎?list<string> Milkshakes這句是你聲明了list<string>模板類 的一個實例,然后就是實例化該類的一個對象。但是我們別急著做這個。在這一步其實你只需要知道你定義了 一個字符串的list。你需要包含提供STL list類的頭文件。我用gcc 2.7.2在我的Linux上編譯這個測試程序,例如:
g++ test1.cpp -o test1 注意iostream.h這個頭文件已經被STL的頭文件放棄了。這就是為什么這個例子中沒有它的原因。
現在我們有了一個list,我們可以看實使用它來裝東西了。我們將把一個字符串加到這個list里。有一個非常 重要的東西叫做list的值類型。值類型就是list中的對象的類型。在這個例子中,這個list的值類型就是字符串,string , 這是因為這個list用來放字符串。
使用list的成員函數push_back和push_front插入一個元素到list中:
#include <string> #include <list>
int main (void) { list<string> Milkshakes; Milkshakes.push_back("Chocolate"); Milkshakes.push_back("Strawberry"); Milkshakes.push_front("Lime"); Milkshakes.push_front("Vanilla"); return 0; } |
我們現在有個4個字符串在list中。list的成員函數push_back()把一個對象放到一個list的后面,而 push_front()把對象放到前面。我通常把一些錯誤信息push_back()到一個list中去,然后push_front()一個標題到list中, 這樣它就會在這個錯誤消息以前打印它了。
The list member function empty()list的成員函數empty()
知道一個list是否為空很重要。如果list為空,empty()這個成員函數返回真。 我通常會這樣使用它。通篇程序我都用push_back()來把錯誤消息放到list中去。然后,通過調用empty() 我就可以說出這個程序是否報告了錯誤。如果我定義了一個list來放信息,一個放警告,一個放嚴重錯誤, 我就可以通過使用empty()輕易的說出到底有那種類型的錯誤發生了。
我可以整理這些list,然后在打印它們之前,用標題來整理它們,或者把它們排序成類。
/* || Using a list to track and report program messages and status */ #include <iostream.h> #include <string> #include <list> int main (void) { #define OK 0 #define INFO 1 #define WARNING 2 int return_code; list<string> InfoMessages; list<string> WarningMessages;
// during a program these messages are loaded at various points InfoMessages.push_back("Info: Program started"); // do work... WarningMessages.push_back("Warning: No Customer records have been found"); // do work...
return_code = OK;
if (!InfoMessages.empty()) { // there were info messages InfoMessages.push_front("Informational Messages:"); // ... print the info messages list, we'll see how later return_code = INFO; }
if (!WarningMessages.empty()) { // there were warning messages WarningMessages.push_front("Warning Messages:"); // ... print the warning messages list, we'll see how later return_code = WARNING; }
// If there were no messages say so. if (InfoMessages.empty() && WarningMessages.empty()) { cout << "There were no messages " << endl; }
return return_code; } |
用for循環來處理list中的元素 我們想要遍歷一個list,比如打印一個中的所有對象來看看list上不同操作的結果。要一個元素一個元素的遍歷一個list, 我們可以這樣做:
/* || How to print the contents of a simple STL list. Whew! */ #include <iostream.h> #include <string> #include <list>
int main (void) { list<string> Milkshakes; list<string>::iterator MilkshakeIterator;
Milkshakes.push_back("Chocolate"); Milkshakes.push_back("Strawberry"); Milkshakes.push_front("Lime"); Milkshakes.push_front("Vanilla");
// print the milkshakes Milkshakes.push_front("The Milkshake Menu"); Milkshakes.push_back("*** Thats the end ***"); for (MilkshakeIterator=Milkshakes.begin(); MilkshakeIterator!=Milkshakes.end(); ++MilkshakeIterator) { // dereference the iterator to get the element cout << *MilkshakeIterator << endl; } } |
這個程序定義了一個iterator,MilkshakeIterator。我們把它指向了這個list的第一個元素。 這可以調用Milkshakes.begin()來作到,它會返回一個指向list開頭的iterator。然后我們把它和Milkshakes.end()的 返回值來做比較,當我們到了那兒的時候就停下來。
容器的end()函數會返回一個指向容器的最后一個位置的iterator。當我們到了那里,就停止操作。 我們不能不理容器的end()函數的返回值。我們僅知道它意味著已經處理到了這個容器的末尾,應該停止處理了。 所有的STL容器都要這樣做。
在上面的例子中,每一次執行for循環,我們就重復引用iterator來得到我們打印的字符串。
在STL編程中,我們在每個算法中都使用一個或多個iterator。我們使用它們來存取容器中的對象。 要存取一個給定的對象,我們把一個iterator指向它,然后間接引用這個iterator。
這個list容器,就象你所想的,它不支持在iterator加一個數來指向隔一個的對象。 就是說,我們不能用Milkshakes.begin()+2來指向list中的第三個對象,因為STL的list是以雙鏈的list來實現的, 它不支持隨機存取。vector和deque(向量和雙端隊列)和一些其他的STL的容器可以支持隨機存取。
上面的程序打印出了list中的內容。任何人讀了它都能馬上明白它是怎麼工作的。它使用標準的iterator和標準 的list容器。沒有多少程序員依賴它里面裝的東西, 僅僅是標準的C++。這是一個向前的重要步驟。這個例子使用STL使我們的軟件更加標準。
用STL的通用算法for_each來處理list中的元素 使用STL list和 iterator,我們要初始化、比較和給iterator增量來遍歷這個容器。STL通用的for_each 算法能夠減輕我們的工作。
/* || How to print a simple STL list MkII */ #include <iostream.h> #include <string> #include <list> #include <algorithm>
PrintIt (string& StringToPrint) { cout << StringToPrint << endl; }
int main (void) { list<string> FruitAndVegetables; FruitAndVegetables.push_back("carrot"); FruitAndVegetables.push_back("pumpkin"); FruitAndVegetables.push_back("potato"); FruitAndVegetables.push_front("apple"); FruitAndVegetables.push_front("pineapple");
for_each (FruitAndVegetables.begin(), FruitAndVegetables.end(), PrintIt); } |
在這個程序中我們使用STL的通用算法for_each()來遍歷一個iterator的范圍,然后調用PrintIt()來處理每個對象。 我們不需要初始化、比較和給iterator增量。for_each()為我們漂亮的完成了這些工作。我們執行于對象上的 操作被很好的打包在這個函數以外了,我們不用再做那樣的循環了,我們的代碼更加清晰了。
for_each算法引用了iterator范圍的概念,這是一個由起始iterator和一個末尾iterator指出的范圍。 起始iterator指出操作由哪里開始,末尾iterator指明到哪結束,但是它不包括在這個范圍內。
用STL的通用算法count()來統計list中的元素個數
STL的通用算法count()和count_it()用來給容器中的對象記數。就象for_each()一樣,count()和count_if() 算法也是在iterator范圍內來做的。
讓我們在一個學生測驗成績的list中來數一數滿分的個數。這是一個整型的List。
/* || How to count objects in an STL list */ #include <list> #include <algorithm> # int main (void) { list<int> Scores; # Scores.push_back(100); Scores.push_back(80); Scores.push_back(45); Scores.push_back(75); Scores.push_back(99); Scores.push_back(100); # int NumberOf100Scores(0); count (Scores.begin(), Scores.end(), 100, NumberOf100Scores); # cout << "There were " << NumberOf100Scores << " scores of 100" << endl; } |
count()算法統計等于某個值的對象的個數。上面的例子它檢查list中的每個整型對象是不是100。每次容器中的對象等于100,它就給NumberOf100Scores加1。這是程序的輸出:
There were 2 scores of 100 |
用STL的通用算法count_if()來統計list中的元素個數 count_if()是count()的一個更有趣的版本。他采用了STL的一個新組件,函數對象。count_if() 帶一個函數對象的參數。函數對象是一個至少帶有一個operator()方法的類。有些STL算法作為參數接收 函數對象并調用這個函數對象的operator()方法。
函數對象被約定為STL算法調用operator時返回true或false。它們根據這個來判定這個函數。舉個例子會 說的更清楚些。count_if()通過傳遞一個函數對象來作出比count()更加復雜的評估以確定一個對象是否應該被 記數。在這個例子里我們將數一數牙刷的銷售數量。我們將提交包含四個字符的銷售碼和產品說明的銷售記錄。
/* || Using a function object to help count things */ #include <string> #include <list> #include <algorithm>
const string ToothbrushCode("0003");
class IsAToothbrush { public: bool operator() ( string& SalesRecord ) { return SalesRecord.substr(0,4)==ToothbrushCode; } };
int main (void) { list<string> SalesRecords;
SalesRecords.push_back("0001 Soap"); SalesRecords.push_back("0002 Shampoo"); SalesRecords.push_back("0003 Toothbrush"); SalesRecords.push_back("0004 Toothpaste"); SalesRecords.push_back("0003 Toothbrush");
int NumberOfToothbrushes(0); count_if (SalesRecords.begin(), SalesRecords.end(), IsAToothbrush(), NumberOfToothbrushes);
cout << "There were " << NumberOfToothbrushes << " toothbrushes sold" << endl; } |
這是這個程序的輸出:
There were 2 toothbrushes sold 這個程序是這樣工作的:定義一個函數對象類IsAToothbrush,這個類的對象能判斷出賣出的是否是牙刷 。如果這個記錄是賣出牙刷的記錄的話,函數調用operator()返回一個true,否則返回false。
count_if()算法由第一和第二兩個iterator參數指出的范圍來處理容器對象。它將對每個 IsAToothbrush()返回true的容器中的對象增加NumberOfToothbrushes的值。
最后的結果是NumberOfToothbrushes這個變量保存了產品代碼域為"0003"的記錄的個數,也就是牙刷的個數。
注意count_if()的第三個參數IsAToothbrush(),它是由它的構造函數臨時構造的一個對象。你可以把IsAToothbrush類的一個臨時對象 傳遞給count_if()函數。count_if()將對該容器的每個對象調用這個函數。
使用count_if()的一個更加復雜的函數對象 我們可以更進一步的研究一下函數對象。假設我們需要傳遞更多的信息給一個函數對象。我們不能通過 調用operator來作到這點,因為必須定義為一個list的中的對象的類型。 然而我們通過為IsAToothbrush指出一個非缺省的構造函數就可以用任何我們所需要的信息來初始化它了。 例如,我們可能需要每個牙刷有一個不定的代碼。我們可以把這個信息加到下面的函數對象中:
/* || Using a more complex function object */ #include <iostream.h> #include <string> #include <list> #include <algorithm>
class IsAToothbrush { public: IsAToothbrush(string& InToothbrushCode) : ToothbrushCode(InToothbrushCode) {} bool operator() (string& SalesRecord) { return SalesRecord.substr(0,4)==ToothbrushCode; } private: string ToothbrushCode; };
int main (void) { list<string> SalesRecords;
SalesRecords.push_back("0001 Soap"); SalesRecords.push_back("0002 Shampoo"); SalesRecords.push_back("0003 Toothbrush"); SalesRecords.push_back("0004 Toothpaste"); SalesRecords.push_back("0003 Toothbrush");
string VariableToothbrushCode("0003");
int NumberOfToothbrushes(0); count_if (SalesRecords.begin(), SalesRecords.end(), IsAToothbrush(VariableToothbrushCode), NumberOfToothbrushes); cout << "There were " << NumberOfToothbrushes << " toothbrushes matching code " << VariableToothbrushCode << " sold" << endl; } |
程序的輸出是:
There were 2 toothbrushes matching code 0003 sold 這個例子演示了如何向函數對象傳遞信息。你可以定義任意你想要的構造函數,你可以再函數對象中做任何你 想做的處理,都可以合法編譯通過。
你可以看到函數對象真的擴展了基本記數算法。