📄 slist.html
字号:
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<META NAME="Author" CONTENT="Zafir Anjum">
<TITLE>MFC Programmer's SourceBook : STL Programmer's Guide</TITLE>
<META name="description"
content="A freely available implementation
of the C++ Standard Template Library, including
hypertext documentation.">
<META name="keywords"
content="generic programming, STL, standard template library">
</HEAD>
<SCRIPT LANGUAGE="JavaScript"><!--
var adcategory = "cpp";
// -->
</SCRIPT>
<body background="../../fancyhome/back.gif" bgcolor="#FFFFFF" >
<SCRIPT LANGUAGE="JavaScript"><!--
var nfrm = location.href.indexOf("_nfrm_");
var validframes = (top.frames.length > 0 && top.frames['ad'] && top.frames['logo'] );
var random = Math.random();
if( !validframes && nfrm == -1 )
{
var dclkPage = "www.codeguru.com/";
if( self.adcategory )
dclkPage += adcategory;
else
dclkPage += "mfc";
document.write('<nolayer><center>');
document.write('<iframe src="http://ad.doubleclick.net/adi/' + dclkPage + ';ord='
+ random + '" width=470 height=62 marginwidth=0 marginheight=0 hspace=0 vspace=0 '
+ 'frameborder=0 scrolling=no bordercolor="#000000">');
document.write('<a href="http://ad.doubleclick.net/jump/' + dclkPage + ';ord='
+ random + '">');
document.write('<img src="http://ad.doubleclick.net/ad/' + dclkPage + ';ord='
+ random + '" height=60 width=468>' + '</a>');
document.write('</iframe>');
document.write('</center></nolayer>');
document.write('<layer src="http://ad.doubleclick.net/adl/' + dclkPage +
';ord=' + random + '"></layer>');
document.write('<ilayer visibility=hide width=468 height=83></ilayer>');
}
// top.location = "/show.cgi?" + adcategory + "=" + location.pathname;
// -->
</SCRIPT>
<noscript>
<p align="center">
<a href="http://ad.doubleclick.net/jump/www.codeguru.com/cpp;ord=NupaO9FCY34AAHXfmuc">
<img src="http://ad.doubleclick.net/ad/www.codeguru.com/cpp;ord=NupaO9FCY34AAHXfmuc"></a>
</p>
</noscript>
<BR Clear>
<H1>slist<T, Alloc></H1>
<Table CellPadding=0 CellSpacing=0 width=100%>
<TR>
<TD Align=left><Img src = "containers.gif" Alt="" WIDTH = "194" HEIGHT = "38" ></TD>
<TD Align=right><Img src = "type.gif" Alt="" WIDTH = "194" HEIGHT = "39" ></TD>
</TR>
<TR>
<TD Align=left VAlign=top><b>Category</b>: containers</TD>
<TD Align=right VAlign=top><b>Component type</b>: type</TD>
</TR>
</Table>
<h3>Description</h3>
An <tt>slist</tt> is a singly linked list: a list where each element is
linked to the next element, but not to the previous element. <A href="#1">[1]</A> That is,
it is a <A href="Sequence.html">Sequence</A> that supports forward but not backward traversal,
and (amortized) constant time insertion and removal of elements.
<tt>Slist</tt>s, like <tt><A href="List.html">list</A></tt>s, have the important property that insertion
and splicing do not invalidate iterators to list elements, and that
even removal invalidates only the iterators that point to the elements
that are removed. The ordering of iterators may be changed (that is,
<tt>slist<T>::iterator</tt> might have a different predecessor or successor
after a list operation than it did before), but the iterators
themselves will not be invalidated or made to point to different
elements unless that invalidation or mutation is explicit. <A href="#2">[2]</A>
<P>
The main difference between <tt>slist</tt> and <tt><A href="List.html">list</A></tt> is that <tt><A href="List.html" tppabs="http://www.sgi.com/Technology/STL/List.shtml">list</A></tt>'s
iterators are <A href="BidirectionalIterator.html">bidirectional iterators</A>, while <tt>slist</tt>'s iterators
are <A href="ForwardIterator.html">forward iterators</A>. This means that <tt>slist</tt> is less versatile
than <tt><A href="List.html">list</A></tt>; frequently, however, <A href="BidirectionalIterator.html" tppabs="http://www.sgi.com/Technology/STL/BidirectionalIterator.shtml">bidirectional iterators</A>
are unnecessary. You should usually use <tt>slist</tt> unless you actually
need the extra functionality of <tt><A href="List.html">list</A></tt>, because singly linked
lists are smaller and faster than double linked lists.
<P>
<b>Important performance note</b>: like every other <A href="Sequence.html">Sequence</A>, <tt>slist</tt>
defines the member functions <tt>insert</tt> and <tt>erase</tt>. Using these member
functions carelessly, however, can result in disastrously slow
programs. The problem is that <tt>insert</tt>'s first argument is an
iterator <tt>pos</tt>, and that it inserts the new element(s) <i>before</i>
<tt>pos</tt>. This means that <tt>insert</tt> must find the iterator just
before <tt>pos</tt>; this is a constant-time operation for <tt><A href="List.html">list</A></tt>,
since <tt><A href="List.html">list</A></tt> has bidirectional iterators, but for <tt>slist</tt>
it must find that iterator by traversing the list from the beginning
up to <tt>pos</tt>. In other words: <tt>insert</tt> and <tt>erase</tt> are slow operations
anywhere but near the beginning of the <tt>slist</tt>.
<P>
<tt>Slist</tt> provides the member functions <tt>insert_after</tt> and
<tt>erase_after</tt>, which are constant time operations: you should always
use <tt>insert_after</tt> and <tt>erase_after</tt> whenever possible. If you find
that <tt>insert_after</tt> and <tt>erase_after</tt> aren't adequate for your needs,
and that you often need to use <tt>insert</tt> and <tt>erase</tt> in the middle of
the list, then you should probably use <tt><A href="List.html">list</A></tt> instead of <tt>slist</tt>.
<h3>Definition</h3>
Defined in <A href="slist.h">slist.h</A>.
<h3>Example</h3>
<pre>
int main() {
slist<int> L;
L.push_front(0);
L.push_front(1);
L.insert_after(L.begin(), 2);
<A href="copy.html">copy</A>(L.begin(), L.end(), // The output is 1 2 0
<A href="ostream_iterator.html">ostream_iterator</A><int>(cout, " "));
cout << endl;
slist<int>::iterator back = L.previous(L.end());
back = L.insert_after(back, 3);
back = L.insert_after(back, 4);
back = L.insert_after(back, 5);
<A href="copy.html">copy</A>(L.begin(), L.end(), // The output is 1 2 0 3 4 5
<A href="ostream_iterator.html">ostream_iterator</A><int>(cout, " "));
cout << endl;
}
</pre>
<h3>Template parameters</h3>
<Table border>
<TR>
<TH>
Parameter
</TH>
<TH>
Description
</TH>
<TH>
Default
</TH>
</TR>
<TR>
<TD VAlign=top>
<tt>T</tt>
</TD>
<TD VAlign=top>
The <tt>slist</tt>'s value type: the type of object that is stored in the list.
</TD>
<TD VAlign=top>
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>Alloc</tt>
</TD>
<TD VAlign=top>
The <tt>slist</tt>'s allocator, used for all internal memory management.
</TD>
<TD VAlign=top>
<tt><A href="Allocators.html">alloc</A></tt>
</TD>
</tr>
</table>
<h3>Model of</h3>
<A href="FrontInsertionSequence.html">Front Insertion Sequence</A>
<h3>Type requirements</h3>
None, except for those imposed by the requirements of
<A href="FrontInsertionSequence.html">Front Insertion Sequence</A>.
<h3>Public base classes</h3>
None.
<h3>Members</h3>
<Table border>
<TR>
<TH>
Member
</TH>
<TH>
Where defined
</TH>
<TH>
Description
</TH>
</TR>
<TR>
<TD VAlign=top>
<tt>value_type</tt>
</TD>
<TD VAlign=top>
<A href="Container.html">Container</A>
</TD>
<TD VAlign=top>
The type of object, <tt>T</tt>, stored in the <tt>slist</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>pointer</tt>
</TD>
<TD VAlign=top>
<A href="Container.html">Container</A>
</TD>
<TD VAlign=top>
Pointer to <tt>T</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>reference</tt>
</TD>
<TD VAlign=top>
<A href="Container.html">Container</A>
</TD>
<TD VAlign=top>
Reference to <tt>T</tt>
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>const_reference</tt>
</TD>
<TD VAlign=top>
<A href="Container.html">Container</A>
</TD>
<TD VAlign=top>
Const reference to <tt>T</tt>
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>size_type</tt>
</TD>
<TD VAlign=top>
<A href="Container.html">Container</A>
</TD>
<TD VAlign=top>
An unsigned integral type.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>difference_type</tt>
</TD>
<TD VAlign=top>
<A href="Container.html">Container</A>
</TD>
<TD VAlign=top>
A signed integral type.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>iterator</tt>
</TD>
<TD VAlign=top>
<A href="Container.html">Container</A>
</TD>
<TD VAlign=top>
Iterator used to iterate through an <tt>slist</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>const_iterator</tt>
</TD>
<TD VAlign=top>
<A href="Container.html">Container</A>
</TD>
<TD VAlign=top>
Const iterator used to iterate through an <tt>slist</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>iterator begin()</tt>
</TD>
<TD VAlign=top>
<A href="Container.html">Container</A>
</TD>
<TD VAlign=top>
Returns an <tt>iterator</tt> pointing to the beginning of the <tt>slist</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>iterator end()</tt>
</TD>
<TD VAlign=top>
<A href="Container.html">Container</A>
</TD>
<TD VAlign=top>
Returns an <tt>iterator</tt> pointing to the end of the <tt>slist</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>const_iterator begin() const</tt>
</TD>
<TD VAlign=top>
<A href="Container.html">Container</A>
</TD>
<TD VAlign=top>
Returns a <tt>const_iterator</tt> pointing to the beginning of the <tt>slist</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>const_iterator end() const</tt>
</TD>
<TD VAlign=top>
<A href="Container.html">Container</A>
</TD>
<TD VAlign=top>
Returns a <tt>const_iterator</tt> pointing to the end of the <tt>slist</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>size_type size() const</tt>
</TD>
<TD VAlign=top>
<A href="Container.html">Container</A>
</TD>
<TD VAlign=top>
Returns the size of the <tt>slist</tt>. Note: you should not assume that
this function is constant time. It is permitted to be <i>O(N</i>),
where <i>N</i> is the number of elements in the <tt>slist</tt>. If you wish to
test whether an <tt>slist</tt> is empty, you should write <tt>L.empty()</tt> rather
than <tt>L.size() == 0</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>size_type max_size() const</tt>
</TD>
<TD VAlign=top>
<A href="Container.html">Container</A>
</TD>
<TD VAlign=top>
Returns the largest possible size of the <tt>slist</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>bool empty() const</tt>
</TD>
<TD VAlign=top>
<A href="Container.html">Container</A>
</TD>
<TD VAlign=top>
<tt>true</tt> if the <tt>slist</tt>'s size is <tt>0</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>slist()</tt>
</TD>
<TD VAlign=top>
<A href="Container.html">Container</A>
</TD>
<TD VAlign=top>
Creates an empty slist.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>slist(size_type n)</tt>
</TD>
<TD VAlign=top>
<A href="Sequence.html">Sequence</A>
</TD>
<TD VAlign=top>
Creates an <tt>slist</tt> with <tt>n</tt> elements, each of which is a copy of <tt>T()</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>slist(size_type n, const T& t)</tt>
</TD>
<TD VAlign=top>
<A href="Sequence.html">Sequence</A>
</TD>
<TD VAlign=top>
Creates an slist with <tt>n</tt> copies of <tt>t</tt>.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>slist(const slist&)</tt>
</TD>
<TD VAlign=top>
<A href="Container.html">Container</A>
</TD>
<TD VAlign=top>
The copy constructor.
</TD>
</TR>
<TR>
<TD VAlign=top>
<pre>
template <class InputIterator>
slist(InputIterator f, InputIterator l)
<A href="#3">[3]</A>
</pre>
</TD>
<TD VAlign=top>
<A href="Sequence.html">Sequence</A>
</TD>
<TD VAlign=top>
Creates an <tt>slist</tt> with a copy of a range.
</TD>
</TR>
<TR>
<TD VAlign=top>
<tt>~slist()</tt>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -