VC++中listlist的使用方法总结.docx
文本预览下载声明
以下是引自msdn帮助文档(中文是我自己翻译的,错误之处请包涵。): The template class describes an object that controls a varying-length sequence of elements of type?T. The sequence is stored as a bidirectional linked list of elements, each containing a member of type?T.?本模板类描述了一个对象,这个对象是类型为T的可变长度的序列元素。这个序列采用双向链表的方式存储每一个元素,其中每一个元素的数据流行都是T。 The object allocates and frees storage for the sequence it controls through a protected object named?allocator, of?class?A. Such an allocator object must have the same external interface as an object of template class?allocator. Note that?allocatoris not copied when the object is assigned.?对序列对象的分配和释放操作通过一个受保护的对象allocator进行。这样一个allocator对象必须有相同的外部接口作为一个模板类分配器的对象。注意:当对象被分配之后allocator不能被复制。?List reallocation?occurs when a member function must insert or erase elements of the controlled sequence. In all such cases, only iterators or references that point at erased portions of the controlled sequence become?invalid.?当一个成员要进行insert或者erase操作时,列表的重新分配操作发生。在这种情况下,只有迭代器或者引用所指向的要删除的对象的指针变为无效。msdn帮助文档自带的例子下面为msdn帮助文档中自带的一个例子,该例展示了如何使用迭代器读取列表中的元素和进行插入操作。#include list#include iostreamusing namespace std ;typedef listint LISTINT;void main(){ int rgTest1[] = {5,6,7}; int rgTest2[] = {10,11,12}; LISTINT listInt; LISTINT listAnother; LISTINT::iterator i; // Insert one at a time listInt.insert (listInt.begin(), 2); listInt.insert (listInt.begin(), 1); listInt.insert (listInt.end(), 3); // 1 2 3 for (i = listInt.begin(); i != listInt.end(); ++i) cout *i ; cout endl; // Insert 3 fours listInt.insert (listInt.end(), 3, 4); // 1 2 3 4 4 4 for (i = listInt.begin(); i != listInt.end(); ++i) cout *i ; cout endl; // Insert an array in there listInt.insert (listInt.end(), rgTest1, rgTest1 + 3); // 1 2 3 4 4 4 5 6 7 for (i = listInt.begin(); i != listInt.end(); ++i) cout *i ; cout endl; // Insert another LISTINT listAnother.insert (listAnother.begin(), rgTest2, rgTest2+3); listInt.insert (listInt.end(), listAnother.begin(), listAnother.end()); // 1 2 3 4 4 4 5 6 7 10 11 12 for (
显示全部