📄 lis_8858.htm
字号:
<HEAD><TITLE>List Operations</TITLE></HEAD>
<BODY>
<A HREF="ug.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the user guide home page.</STRONG></P>
<P>©Copyright 1996 Rogue Wave Software</P>
<H2>List Operations</H2>
<P>The member functions provided by the list data type are described in more detail below. Note that while member functions provide basic operations, the utility of the data structure is greatly extended through the use of the generic algorithms described in Chapters <a href="gen_9895.htm">13</a> and <a href="ord_1635.htm">14</a>.</P>
<A NAME="declarationandinitializationoflists"><H3>Declaration and Initialization of Lists</H3></A>
<A HREF="sidebar.htm#sidebar18"><IMG SRC="images/note.gif" BORDER=0> <STRONG>Memory Management</STRONG></A>
<P>There are a variety of ways to declare a list. In the simplest form, a list is declared by simply stating the type of element the collection will maintain. This can be a primitive language type (such as <SAMP>integer</SAMP> or <SAMP>double</SAMP>), a pointer type, or a user-defined type. In the latter case, the user-defined type <I>must</I> implement a default constructor (a constructor with no arguments), as this constructor is in some cases used to initialize newly created elements. A collection declared in this fashion will initially not contain any elements.</P>
<PRE> list <int> list_one;
list <Widget *> list_two;
list <Widget> list_three;
</PRE>
<P>An alternative form of declaration creates a collection that initially contains some number of equal elements. The constructor for this form is declared as <SAMP>explicit</SAMP>, meaning it cannot be used as a conversion operator. This prevents integers from inadvertently being converted into lists. The constructor for this form takes two arguments, a size and an initial value. The second argument is optional. If only the number of initial elements to be created is given, these values will be initialized with the default constructor; otherwise the elements will be initialized with the value of the second argument:</P>
<PRE> list <int> list_four (5); // five elements, initialized to zero
list <double> list_five (4, 3.14); // 4 values, initially 3.14
list <Widget> wlist_six (4); // default constructor, 4 elements
list <Widget> list_six (3, Widget(7)); // 3 copies of Widget(7)
</PRE>
<P>Lists can also be initialized using elements from another collection, using a beginning and ending iterator pair. The arguments can be any form of iterator, thus collections can be initialized with values drawn from any of the container classes in the standard library that support iterators. Because this requires the ability to specialize a member function using a template, some compilers may not yet support this feature. In these cases an alternative technique using the <SAMP>copy()</SAMP> generic algorithm can be employed.
When a list is initialized using <SAMP>copy(),</SAMP> an <I>insert iterator</I> must be constructed to convert the output operations performed by the copy operation into list insertions. (<i><a href="ins_0332.htm">See Chapter 2: Insert Iterators</a></i>.) The inserter requires two arguments; the list into which the value is to be inserted, and an iterator indicating the location at which values will be placed. Insert iterators can also be used to copy elements into an arbitrary location in an existing list.</P>
<PRE> list <double> list_seven (aVector.begin(), aVector.end());
// the following is equivalent to the above
list <double> list_eight;
copy (aVector.begin(), aVector.end(),
inserter(list_eight, list_eight.begin()));
</PRE>
<P>The <SAMP>insert()</SAMP> operation, to be described in <a href="lis_8858.htm#placingelementsintoalist"><i>Placing Elements into a List</i></a>, can also be used to place values denoted by an iterator into a list. Insert iterators can be used to initialize a list with a sequence of values produced by a <I>generator</I> (<i>see <a href="ini_5794.htm#initializeasequencewithgeneratedvalues">Chapter 13: Initialize a Sequence with Generated Values</a></i>). This is illustrated by the following:</P>
<PRE> list <int> list_nine;
// initialize list 1 2 3 ... 7
generate_n (inserter(list_nine, list_nine.begin()),
7, iotaGen(1));
</PRE>
<P>A <I>copy constructor</I> can be used to initialize a list with values drawn from another list. The assignment operator performs the same actions. In both cases the assignment operator for the element type is used to copy each new value.</P>
<PRE> list <int> list_ten (list_nine); // copy constructor
list <Widget> list_eleven;
list_eleven = list_six; // values copied by assignment
</PRE>
<P>The <SAMP>assign()</SAMP> member function is similar to the assignment operator, but is more versatile and, in some cases, requires more arguments. Like an assignment, the existing values in the container are deleted, and replaced with the values specified by the arguments. If a destructor is provided for the container element type, it will be invoked for the elements being removed. There are two forms of <SAMP>assign().</SAMP> The first takes two iterator arguments that specify a subsequence of an existing container. The values from this subsequence then become the new elements in the receiver. The second version of assign takes a count and an optional value of the container element type. After the call the container will hold the number of elements specified by the count, which will be equal to either the default value for the container type or the initial value specified.</P>
<PRE> list_six.assign(list_ten.begin(), list_ten.end());
list_four.assign(3, 7); // three copies of value seven
list_five.assign(12); // twelve copies of value zero
</PRE>
<P>Finally, two lists can exchange their entire contents by means of the operation <SAMP>swap().</SAMP> The argument container will take on the values of the receiver, while the receiver will assume those of the argument. A swap is very efficient, and should be used, where appropriate, in preference to an explicit element-by-element transfer.</P>
<PRE> list_ten.swap(list_nine); // exchange lists nine and ten
</PRE>
<A NAME="typedefinitions"><H3>Type Definitions</H3></A>
<P>The class <A HREF="../stdref/lis_3222.htm"><B><I>list</I></B></A> includes a number of type definitions. The most common use for these is in declaration statements. For example, an iterator for a list of integers can be declared as follows:</P>
<PRE>list<int>::iterator location;
</PRE>
<P>In addition to <SAMP>iterator</SAMP>, the following types are defined:</P>
<CENTER><TABLE CELLSPACING=3 CELLPADDING=3>
<TR VALIGN=top>
<TD>
<SAMP>value_type</SAMP><BR>
</TD>
<TD>
The type associated with the elements the list maintains.<BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
<SAMP>const_iterator</SAMP><BR>
</TD>
<TD>
An iterator that does not allow modification of the underlying sequence.<BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
<SAMP>reverse_iterator</SAMP><BR>
</TD>
<TD>
An iterator that moves in a backward direction.<BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
<SAMP>const_reverse_iterator</SAMP><BR>
</TD>
<TD>
A combination constant and reverse iterator.<BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
<SAMP>reference</SAMP><BR>
</TD>
<TD>
A reference to an underlying element.<BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
<SAMP>const_reference</SAMP><BR>
</TD>
<TD>
A reference to an underlying element that will not permit the element to be modified.<BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
<SAMP>size_type</SAMP><BR>
</TD>
<TD>
An unsigned integer type, used to refer to the size of containers.<BR>
</TD>
</TR>
<TR VALIGN=top>
<TD>
<SAMP>difference_type</SAMP><BR>
</TD>
<TD>
A signed integer type, used to describe distances between iterators.<BR>
</TD>
</TR>
</TABLE></CENTER>
<A NAME="placingelementsintoalist"><H3>Placing Elements into a List</H3></A>
<P>Values can be inserted into a list in a variety of ways. Elements are most commonly added to the front or back of a list. These tasks are provided by the <SAMP>push_front()</SAMP> and <SAMP>push_back()</SAMP> operations, respectively. These operations are efficient (constant time) for both types of containers.</P>
<PRE> list_seven.push_front(1.2);
list_eleven.push_back (Widget(6));
</PRE>
<P>In a previous discussion (<i><a href="#declarationandinitializationoflists">Declaration and Initialization of Lists</a></i>) we noted how, with the aid of an insert iterator and the <SAMP>copy()</SAMP> or <SAMP>generate()</SAMP> generic algorithm, values can be placed into a list at a location denoted by an iterator. There is also a member function, named <SAMP>insert(),</SAMP> that avoids the need to construct the inserter. As we will describe shortly, the values returned by the iterator generating functions <SAMP>begin()</SAMP> and <SAMP>end()</SAMP> denote the beginning and end of a list, respectively. An insert using one of these is equivalent to <SAMP>push_front()</SAMP> or <SAMP>push_back(),</SAMP> respectively. If we specify only one iterator, the default element value is inserted.</P>
<PRE>// insert default type at beginning of list
list_eleven.insert(list_eleven.begin());
// insert widget 8 at end of list
list_eleven.insert(list_eleven.end(), Widget(8));
</PRE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -