定义一个list 

我们可以象这样来定义一个 STL的list:   
 
#include <string> 
#include <list> 
int main (void) { 
list<string> Milkshakes; 
这就行了,你已经定义了一个 list。简单吗?list<string> Milkshakes 这句是你声明了 
list<string>模板类 的一个实例,然后就是实例化这个类的一个对象。但是我们别急着做这
个。在这一步其实你只需要知道你定义了 一个字符串的list。你需要包含提供STL list类的
头文件。我用gcc 2.7.2 在我的linux上编译这个测试程序,例如:   
g++ test1.cpp -otest1 
注重iostream.h这个头文件已经被 STL的头文件放弃了。这就是为什么这个例子中没有它的
原因。   
现在我们有了一个list,我们可以看实使用它来装东西了。我们将把一个字符串加到这个list
里。有一个非常 重要的东西叫做 list 的值类型。值类型就是 list 中的对象的类型。在这个
例子中,这个list的值类型就是字符串,string , 这是因为这个 list用来放字符串。   
 
 
 
 
-------------------------------------------------------------------------------- 
 
I使用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"); 
We now have a list with four strings in it. The list member function push_back () places an object 
onto the back of the list. The list member function push_front () puts one on the front. I often 
push_back() some error messages onto a list, and then push_front() a title on the list so it PRints 
before the error messages. 我们现在有个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使我们的软件更加标准。