作者:Jeff Bogan
原文:
http://www.codeproject.com/vcpp/stl/PracticalGuideStl.asp翻譯:
WinterWinter注: 這是一篇非常不錯的文章,以前周翔已經翻譯過了。只是感覺翻譯得有些欠妥之處,特別是一些術語的翻譯,因此這里重新翻譯。
1 介紹
對于當今所有C++程序員來說,STL(標準模板庫的縮寫)都是非常不錯的技術。但我必須要提醒的是要想習慣使用有一定難度,例如,會有很陡峭的學習曲線,其使用許多名字也不是憑直覺就可以知道其意思(或許是因為所有好記的名字都被用光了)。但一旦你學會了STL,你將會因此而受益匪淺。和MFC的容器相比,STL更加靈活且功能強大。
其優勢如下:
- 能方便的排序和搜索。
- 更安全且更容易調試。
- 你能讀懂Unix程序員的代碼注1。
- 將為你的簡歷上增加技能。
2 背景
寫本文檔的目的在于讓讀者可以在這富有挑戰性的計算機科學領域有個良好的開端,不必費力地了解那無窮無盡的行話術語和沉悶的規則,那些行話和規則只是STLer們用于自娛的創造品。
3 使用代碼
本文檔中的代碼對讀者在使用STL實踐之路上有很強的指導作用。
4 定義
- 模板(template)-- 類(以及結構、數據類型、和函數)的宏。有時也叫cookie cutter. 同時和已知范型(generic)形式一樣--一個類模板叫范型類,同樣,一個函數模板叫范型函數。
- STL -- 標準模板庫,由一群聰明人寫的模板,現在作為標準C++語言的一部分被所有人使用。
- 容器(container) -- 可容納一定數據的類。在STL中有vector, set, map, multimap, deque等容器。
- vector -- 一個基礎的數據模板,是一種容器。
- 迭代器(Iterator) -- 一個非常有意思的詞,其實是STL容器內部元素的指針。它同時完成其他許多功能。
5 Hello Word 程序
I always wanted to write one and here is my golden 24 karet opportunity: a hello world program. 這個程序把一個字符串轉換為一個字符vector,然后以逐個字符顯示整個字符串。vector就像是盛放變長數組的花園,在STL所有容器中,大約有一半是基于vector的,故可以這么說,尚若你掌握了這個程序,那么你就理解了整個STL的一半了
//?Program:?Vector?Demo?1
//?Purpose:?用于演示STL?vector

//?#include?"stdafx.h"?-?如果你使用預編譯需要包含此文件[[#ExplainIn2][注2]]
#include?<vector>??//?STL?vector?頭文件.?注意,并沒有".h"
#include?<iostream>??//?需要用到?cout
using?namespace?std;??//?確保命名空間是?std

char*?szHW?=?"Hello?World";??
//?眾所周知,這是個以NULL結尾的字符數組?

int?main(int?argc,?char*?argv[])


{
??vector?<char>?vec;??//?一個字符類型的vector(相當于STL中的數組)

??//?為字符vector定義迭代器
??vector?<char>::iterator?vi;

??//?初始化字符vector,循環整個字符串,把每個字符放入vector中,直至字符串末尾的NULL字符
??char*?cptr?=?szHW;??//??Hello?World?字符串的首地址
??while?(*cptr?!=?'\0')

??
{??vec.push_back(*cptr);??cptr++;??}
??//?push_back?函數把數據插入vector的最后?

??//?把存在STL數組中的每個字符打印到屏幕上
??for?(vi=vec.begin();?vi!=vec.end();?vi++)??
??//?這就是在STL中循環的標準判斷方式-?經常使用?"!="?而不是?"<"?
??//?某些容器可能并沒有重載操作符?"<"?。?
??//begin()和end()會得到vector的開頭和結尾兩個元素的迭代器(指針)?

??
{??cout?<<?*vi;??}??//?使用間接操作符(*)從迭代器中取得數據
??cout?<<?endl;??//?輸出完畢,打印?"\n"

??return?0;
}

push_back 是用來向vector或deque容器中插入數據的標準函數。insert是類似功能的函數,適用于所有容器,但用法更復雜。end()實際上表示在最后的位置再加一,以便循環可以正常執行 - 它返回的指針指向最靠近數組界限的數據。就像普通循環中的數組,比如for (i=0; i<6; i++) {ar[i] = i;} ——ar[6]是不存在的,在循環中不會達到這個元素,所以在循環中不會出現問題。
6 STL的煩惱之一:
STL令人煩惱的地方是在它初始化的時候。STL中容器的初始化比C/C++數組初始化要麻煩的多。你只能一個元素一個元素地來,或者先初始化一個普通數組再通過轉化填放到容器中。我認為人們通常可以這樣做:
//?Program:?Initialization?Demo
//?Purpose:?To?demonstrate?initialization?of?STL?vectors

#include?<cstring>??//?same?as?<string.h>
#include?<vector>
using?namespace?std;


int?ar[10]?=?
{??12,?45,?234,?64,?12,?35,?63,?23,?12,?55??};
char*?str?=?"Hello?World";

int?main(int?argc,?char*?argv[])


{
??vector?<int>?vec1(ar,?ar+10);
??vector?<char>?vec2(str,?str+strlen(str));
??return?0;
}

在編程中,有很多種方法來完成同樣的工作。另一種填充向量的方法是用更加熟悉的方括號,例如:
//?Program:?Vector?Demo?2
//?Purpose:?To?demonstrate?STL?vectors?with
//?counters?and?square?brackets

#include?<cstring>
#include?<vector>
#include?<iostream>
using?namespace?std;

char*?szHW?=?"Hello?World";
int?main(int?argc,?char*?argv[])


{
??vector?<char>?vec(strlen(sHW));?
??//?The?argument?initializes?the?memory?footprint
??int?i,?k?=?0;
??char*?cptr?=?szHW;
??while?(*cptr?!=?'\0')

??
{??vec[k]?=?*cptr;??cptr++;??k++;??}
??for?(i=0;?i<vec.size();?i++)

??
{??cout?<<?vec[i];??}
??cout?<<?endl;
??return?0;
}

這個例子更加清晰,但沒有使用迭代器(iterator)操作,并且定義了額外的整數作為下標,而且,你必須清楚地在程序中說明為vector分配多少內存空間。
7 命名空間(namespace)
與STL相關的概念是命名空間(namespace)。STL定義在std命名空間中。有3種方法聲明使用的命名空間:
- 用using關鍵字使用這個命名空間,在文件的頂部,但在聲明的頭文件下面加入:
using namespace std;
最于簡單工程來說,這是最簡單也是最佳方式。直接把你的代碼定位到std命名空間,
This is the simplest and best for simple projects, limits you to the std namespace, anything you add is improperly put in the std namespace (I think you go to heck for doing this).
- Specify each and every template before use (like prototyping)
using std::cout; using std::endl; using std::flush; using std::set; using std::inserter;
This is slightly more tedious, although a good mnemonic for the functions that will be used, and you can interlace other namespaces easily.
- EVERY time you use a template from the std namespace, use the std scope specifier.
typedef std::vector VEC_STR;
This is tedious but the best way if you are mixing and matching lots of namespaces. Some STL zealots will always use this and call anyone evil who does not. Some people will create macros to simplify matters.
In addition, you can put using namespace std within any scope, for example, at the top of a function or within a control loop. Some Tips
To avoid an annoying error code in debug mode, use the following compiler pragma:
#pragma warning(disable: 4786)
Another gotcha is: you must make sure that the spaces are placed between your angle brackets and the name. This is because >> is the bit shift operator, so:
vector <list<int>> veclis;
will give an error. Instead, write it:
vector > veclis;
to avoid compilation errors. Another Container - The set
This is the explanation lifted from the MS help file of the set: "The template class describes an object that controls a varying-length sequence of elements of type const Key. Each element serves as both a sort key and a value. The sequence is represented in a way that permits lookup, insertion, and removal of an arbitrary element with a number of operations proportional to the logarithm of the number of elements in the sequence (logarithmic time). Moreover, inserting an element invalidates no iterators, and removing an element invalidates only those iterators that point at the removed element."
An alternate, more practical, definition is: A set is a container that contains all unique values. This is useful for cases in which you are required to collect the occurrence of value. It is sorted in an order that is specified at the instantiation of the set. If you need to store data with a key/value pair, then a map is a better choice. A set is organized as a linked list, is faster than a vector on insertion and removal, but slightly slower on search and addition to end.
An example program would be:
// Program: Set Demo // Purpose: To demonstrate STL sets
#include #include #include using namespace std;
int main(int argc, char* argv[]) { set strset; set ::iterator si; strset.insert("cantaloupes"); strset.insert("apple"); strset.insert("orange"); strset.insert("banana"); strset.insert("grapes"); strset.insert("grapes"); // This one overwrites the previous occurrence for (si=strset.begin(); si!=strset.end(); si++) { cout << *si << " "; } cout << endl; return 0; }
// Output: apple banana cantaloupes grapes orange
If you want to become an STL fanatic, you can also replace the output loop in the program with the following lines.
copy(strset.begin(), strset.end(), ostream_iterator(cout, " "));
While instructive, I find this personally less clear and prone to error. If you see it, now you know what it does. All the STL Containers
Containers pre-date templates and are computer science concepts that have been incorporated into STL. The following are the seven containers implemented in STL.
* vector - Your standard safe array. It is expanded in the "front" direction only. * deque - Functionally the same as a vector. Internally, it is different. It can be expanded in both the front and back. * list - Can only be traversed one step at time. If you are already familiar with the concept of a list, an STL list is doubly linked (contains pointer to both the previous and next value). * set - contains unique values that are sorted. * map - sorted set of paired values, one of which is the key on which sorts and searches occur, and the value which is retrieved from the container. E.g. instead of ar[43] = "overripe", a map lets you do this ar["banana"] = "overripe". So if you wanted to draw up a bit of information keyed on full name is easily done. * multiset - same as a set, but does not necessarily have unique values. * multimap - same as a map, but does not necessarily have unique keys.
Note: If you are reading the MFC help then you will also come across the efficiency statement of each container. I.E. (log n * n) insertion time. Unless you are dealing with very large number of values, you should ignore this. If you start to get a noticeable lag or are dealing with time critical stuff then you should learn more about the proper efficiency of various containers. How to Use a Map with some Class
The map is a template that uses a key to obtain a value.
Another issue is that you will want to use your own classes instead of data types, like int that has been used up to now. To create a class that is "template-ready", you must be ensure that the class contains certain member functions and operators. The basics are:
* default constructor (empty, usually) * copy constructor * overload "="
You would overload more operators as required in a specific template, for example, if you plan to have a class that is a key in a map you would have to overload relational operators. But that is another story.
// Program: Map Own Class // Purpose: To demonstrate a map of classes
#include #include #include #include