STL 简介 标准模板库(2)
这是这个程序的输出: 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
这个例子演示了如何向函数对象传递信息。你可以定义任意你想要的构造函数,你可以再函数对象中做任何你 想做的处理,都可以合法编译通过。
你可以看到函数对象真的扩展了基本记数算法。
到现在为止,我们都学习了:
定义一个list
向list中加入元素
如何知道list是否为空
如何使用for循环来遍历一个list
如何使用STL的通用算法for_each来遍历list
list成员函数begin() 和 end() 以及它们的意义
iterator范围的概念和一个范围的最后一个位置实际上并不被处理这一事实
如何使用STL通用算法count()和count_if()来对一个list中的对象记数
如何定义一个函数对象
我选用这些例子来演示list的一般操作。如果你懂了这些基本原理,你就可以毫无疑问的使用STL了 建议你作一些练习。我们现在用一些更加复杂的操作来扩展我们的知识,包括list成员函数和STL通用算法。
--------------------------------------------------------------------------------
使用STL通用算法find()在list中查找对象
我们如何在list中查找东西呢?STL的通用算法find()和find_if()可以做这些。 就象for_each(), count(), count_if() 一样,这些算法也使用iterator范围,这个范围指出一个list或任意 其他容器中的一部分来处理。通常首iterator指着开始的位置,次iterator指着停止处理的地方。 由次iterator指出的元素不被处理。
这是find()如何工作:
/*
|| How to find things in an STL list
*/
#include <string>
#include <list>
#include <algorithm>
#
int main (void) {
list<string> Fruit;
list<string>::iterator FruitIterator;
#
Fruit.push_back("Apple");
Fruit.push_back("Pineapple");
Fruit.push_back("Star Apple");
#
FruitIterator = find (Fruit.begin(), Fruit.end(), "Pineapple");
#
if (FruitIterator == Fruit.end()) {
cout << "Fruit not found in list" << endl;
}
else {
cout << *FruitIterator << endl;
}
}
输出是:
Pineapple
如果没有找到指出的对象,就会返回Fruit.end()的值,要是找到了就返回一个指着找到的对象的iterator
--------------------------------------------------------------------------------
使用STL通用算法find_if()在list中搜索对象
这是find()的一个更强大的版本。这个例子演示了find_if(),它接收一个函数对象的参数作为参数, 并使用它来做更复杂的评价对象是否和给出的查找条件相付。
假设我们的list中有一些按年代排列的包含了事件和日期的记录。我们希望找出发生在1997年的事件。
/*|| How to find things in an STL list MkII */#include <string>#include <list>#include <algorithm>#class EventIsIn1997 {public: bool operator () (string& EventRecord) { // year field is at position 12 for 4 characters in EventRecord return EventRecord.substr(12,4)=="1997"; } };#int main (void) { list<string> Events;#// string positions 0123456789012345678901234567890123456789012345 Events.push_back("07 January 1995 Draft plan of house prepared"); Events.push_back("07 February 1996 Detailed plan of house prepared"); Events.push_back("10 January 1997 Client agrees to job"); Events.push_back("15 January 1997 Builder starts work on bedroom"); Events.push_back("30 April 1997 Builder finishes work"); # list<string>::iterator EventIterator = find_if (Events.begin(), Events.end(), EventIsIn1997());# // find_if completes the first time EventIsIn1997()() returns true // for any object. It returns an iterator to that object which we // can dereference to get the object, or if EventIsIn1997()() never // returned true, find_if returns end() if (EventIterator==Events.end()) { cout << "Event not found in list" << endl; } else { cout << *EventIterator << endl; }}
这是程序的输出: 10 January 1997 Client agrees to job
--------------------------------------------------------------------------------
使用STL通用算法search在list中找一个序列
一些字符在STL容器中很好处理,让我们看一看一个难处理的字符序列。我们将定义一个list来放字符。
list<char> Characters;
现在我们有了一个字符序列,它不用任何帮助就知道然后管理内存。它知道它是从哪里开始、到哪里结束。 它非常有用。我不知道我是否说过以null结尾的字符数组。
让我们加入一些我们喜欢的字符到这个list中:
Characters.push_back('');
Characters.push_back('');
Characters.push_back('1');
Characters.push_back('2');
我们将得到多少个空字符呢?
int NumberOfNullCharacters(0); count(Characters.begin(), Characters.end(), '', NumberOfNullCharacters); cout << "We have " << NumberOfNullCharacters << endl;
让我们找字符'1' list<char>::iterator Iter; Iter = find(Characters.begin(), Characters.end(), '1'); cout << "We found " << *Iter << endl;
这个例子演示了STL容器允许你以更标准的方法来处理空字符。现在让我们用STL的search算法来搜索容器中 的两个null。
就象你猜的一样,STL通用算法search()用来搜索一个容器,但是是搜索一个元素串,不象find()和find_if() 只搜索单个的元素。
/*|| How to use the search algorithm in an STL list*/#include <string>#include <list>#include <algorithm>#int main ( void ) { # list<char> TargetCharacters; list<char> ListOfCharacters;# TargetCharacters.push_back(''); TargetCharacters.push_back('');# ListOfCharacters.push_back('1'); ListOfCharacters.push_back('2'); ListOfCharacters.push_back(''); ListOfCharacters.push_back('');# list<char>::iterator PositionOfNulls = search(ListOfCharacters.begin(), ListOfCharacters.end(), TargetCharacters.begin(), TargetCharacters.end());# if (PositionOfNulls!=ListOfCharacters.end()) cout << "We found the nulls" << endl;}
The output of the program will be 这是程序的输出: We found the nulls
search算法在一个序列中找另一个序列的第一次出现的位置。在这个例子里我们在ListOfCharacters中 找TargetCharacters这个序列的第一次出现,TargetCharacters是包含两个null字符的序列。
search的参数是两个指着查找目标的iterator和两个指着搜索范围的iterators。 因此我们我们在整个的ListOfCharacters的范围内查找TargetCharacters这个list的整个序列。
如果TargetCharacters被发现,search就会返回一个指着ListOfCharacters中序列匹配的第一个 字符的iterator。如果没有找到匹配项,search返回ListOfCharacters.end()的值。
--------------------------------------------------------------------------------
使用list的成员函数sort()排序一个list。
要排序一个list,我们要用list的成员函数sort(),而不是通用算法sort()。所有我们用过的算法都是 通用算法。然而,在STL中有时容器支持它自己对一个特殊算法的实现,这通常是为了提高性能。
在这个例子中,list容器有它自己的sort算法,这是因为通用算法仅能为那些提供随机存取里面元素 的容器排序,而由于list是作为一个连接的链表实现的,它不支持对它里面的元素随机存取。所以就需要一个特殊的 sort()成员函数来排序list。
由于各种原因,容器在性能需要较高或有特殊效果需求的场合支持外部函数(extra functions), 这通过利用构造函数的结构特性可以作到。
/*|| How to sort an STL list*/#include <string>#include <list>#include <algorithm>#PrintIt (string& StringToPrint) { cout << StringToPrint << endl;}#int main (void) { list<string> Staff; list<string>::iterator PeopleIterator;# Staff.push_back("John"); Staff.push_back("Bill"); Staff.push_back("Tony"); Staff.push_back("Fidel"); Staff.push_back("Nelson"); # cout << "The unsorted list " << endl; for_each(Staff.begin(), Staff.end(), PrintIt );# Staff.sort();# cout << "The sorted list " << endl; for_each(Staff.begin(), Staff.end(), PrintIt); }
输出是: The unsorted list JohnBillTonyFidelNelsonThe sorted list BillFidelJohnNelsonTony
--------------------------------------------------------------------------------
用list的成员函数插入元素到list中
list的成员函数push_front()和push_back()分别把元素加入到list的前面和后面。你可以使用insert() 把对象插入到list中的任何地方。
insert()可以加入一个对象,一个对象的若干份拷贝,或者一个范围以内的对象。这里是一些 插入对象到list中的例子:
/*|| Using insert to insert elements into a list.*/#include <list>#int main (void) { list<int> list1;# /* || Put integers 0 to 9 in the list */ for (int i = 0; i < 10; ++i) list1.push_back(i); # /* || Insert -1 using the insert member function || Our list will contain -1,0,1,2,3,4,5,6,7,8,9 */ list1.insert(list1.begin(), -1); # /* || Insert an element at the end using insert || Our list will contain -1,0,1,2,3,4,5,6,7,8,9,10 */ list1.insert(list1.end(), 10); # /* || Inserting a range from another container || Our list will contain -1,0,1,2,3,4,5,6,7,8,9,10,11,12 */ int IntArray[2] = ; list1.insert(list1.end(), &IntArray[0], &IntArray[2]);# /* || As an exercise put the code in here to print the lists! || Hint: use PrintIt and accept an interger */}
注意,insert()函数把一个或若干个元素插入到你指出的iterator的位置。你的元素将出现在 iterator指出的位置以前。
--------------------------------------------------------------------------------
List 构造函数
我们已经象这样定义了list: list<int> Fred;
你也可以象这样定义一个list,并同时初始化它的元素: // define a list of 10 elements and initialise them all to 0 list<int> Fred(10, 0); // list now contains 0,0,0,0,0,0,0,0,0,0
- 最新评论
