📄 lis_3222.htm
字号:
<B>empty</B> () const;</PRE>
<UL><P>Returns <SAMP>true</SAMP> if the <SAMP>size</SAMP> is zero.</P>
</UL>
<PRE>iterator
<B>erase</B> (iterator position);</PRE>
<UL><P>Removes the element pointed to by <SAMP>position</SAMP>. Returns an <SAMP>iterator</SAMP> pointing to the element following the deleted element, or <SAMP>end()</SAMP> if the deleted item was the last one in this list.</P>
</UL>
<PRE>iterator
<B>erase</B> (iterator first, iterator last);</PRE>
<UL><P>Removes the elements in the range (<SAMP>first, last</SAMP>). Returns an <SAMP>iterator</SAMP> pointing to the element following the element following the last deleted element, or <SAMP>end()</SAMP> if there were no elements after the deleted range.</P>
</UL>
<PRE>reference
<B>front</B> ();</PRE>
<UL><P>Returns a <SAMP>reference</SAMP> to the first element.</P>
</UL>
<PRE>const_reference
<B>front</B> () const;</PRE>
<UL><P>Returns a constant reference to the first element.</P>
</UL>
<PRE>iterator
<B>insert </B>(iterator position);</PRE>
<UL><P>Inserts a copy of the default value for type <SAMP>T</SAMP> before <SAMP>position</SAMP>. Returns an <SAMP>iterator</SAMP> that points to the inserted value. Requires that type <SAMP>T </SAMP>have a default constructor.</P>
</UL>
<PRE>iterator
<B>insert</B> (iterator position, const T& x);</PRE>
<UL><P>Inserts <SAMP>x</SAMP> before <SAMP>position</SAMP>. Returns an <SAMP>iterator</SAMP> that points to the inserted <SAMP>x</SAMP>.</P>
</UL>
<PRE>void
<B>insert</B> (iterator position, size_type n, const T& x);</PRE>
<UL><P>Inserts <SAMP>n</SAMP> copies of <SAMP>x</SAMP> before <SAMP>position</SAMP>.</P>
</UL>
<PRE>template <class InputIterator>
void
<B>insert</B> (iterator position, InputIterator first,
InputIterator last);</PRE>
<UL><P>Inserts copies of the elements in the range <SAMP>[first, last)</SAMP> before <SAMP>position</SAMP>.</P>
</UL>
<PRE>size_type
<B>max_size</B> () const;</PRE>
<UL><P>Returns<SAMP> size()</SAMP> of the largest possible list.</P>
</UL>
<PRE>void <B>merge</B> (list<T, Allocator>& x);</PRE>
<UL><P>Merges a sorted <SAMP>x</SAMP> with a sorted self using <SAMP>operator<</SAMP>. For equal elements in the two lists, elements from self will always precede the elements from <SAMP>x</SAMP>. The <SAMP>merge</SAMP> function leaves <SAMP>x</SAMP> empty.</P>
</UL>
<PRE>template <class Compare>
void
<B>merge</B> (list<T, Allocator>& x, Compare comp);</PRE>
<UL><P>Merges a sorted <SAMP>x</SAMP> with sorted self using a compare function object, <SAMP>comp</SAMP>. For same elements in the two lists, elements from self will always precede the elements from <SAMP>x</SAMP>. The <SAMP>merge</SAMP> function leaves <SAMP>x</SAMP> empty.</P>
</UL>
<PRE>void
<B>pop_back</B> ();</PRE>
<UL><P>Removes the last element.</P>
</UL>
<PRE>void
<B>pop_front</B> ();</PRE>
<UL><P>Removes the first element.</P>
</UL>
<PRE>void
<B>push_back</B> (const T& x);</PRE>
<UL><P>Appends a copy of <SAMP>x</SAMP> to the end of the list.</P>
</UL>
<PRE>void
<B>push_front</B> (const T& x);</PRE>
<UL><P>Appends a copy of <SAMP>x</SAMP> to the front of the list.</P>
</UL>
<PRE>void
<B>remove</B> (const T& value);
template <class Predicate>
void
<B>remove_if</B> (Predicate pred);</PRE>
<UL><P>Removes all elements in the list referred by the list iterator <SAMP>i</SAMP> for which <SAMP>*i == value </SAMP>or<SAMP> pred(*i) == true</SAMP>, whichever is applicable. This is a stable operation, the relative order of list items that are not removed is preserved.</P>
</UL>
<PRE>void
<B>resize</B> (size_type sz);</PRE>
<UL><P>Alters the size of self. If the new size ( <SAMP>sz</SAMP> ) is greater than the current size, <SAMP>sz</SAMP>-<SAMP>size() </SAMP>copies of the default value of type<SAMP> T </SAMP>are inserted at the end of the list. If the new size is smaller than the current capacity, then the list is truncated by erasing <SAMP>size()-sz</SAMP> elements off the end. Otherwise, no action is taken. Requires that type <SAMP>T</SAMP> have a default constructor.</P>
</UL>
<PRE>void
<B>resize</B> (size_type sz, T c);</PRE>
<UL><P>Alters the size of self. If the new size ( <SAMP>sz</SAMP> ) is greater than the current size, <SAMP>sz</SAMP>-<SAMP>size() c</SAMP>'s are inserted at the end of the list. If the new size is smaller than the current capacity, then the list is truncated by erasing <SAMP>size()-sz</SAMP> elements off the end. Otherwise, no action is taken. </P>
</UL>
<PRE>void
<B>reverse</B> ();</PRE>
<UL><P>Reverses the order of the elements.</P>
</UL>
<PRE>size_type
<B>size</B> () const;</PRE>
<UL><P>Returns the number of elements.</P>
</UL>
<PRE>void
<B>sort</B> ();</PRE>
<UL><P>Sorts self according to the <SAMP>operator<</SAMP>. <SAMP>sort</SAMP> maintains the relative order of equal elements.</P>
</UL>
<PRE>template <class Compare>
void
<B>sort</B> (Compare comp);</PRE>
<UL><P>Sorts self according to a comparison function object, <SAMP>comp</SAMP>. This is also a stable sort. </P>
</UL>
<PRE>void
<B>splice</B> (iterator position, list<T, Allocator>& x);</PRE>
<UL><P>Inserts <SAMP>x </SAMP>before <SAMP>position</SAMP> leaving <SAMP>x </SAMP>empty.</P>
</UL>
<PRE>void
<B>splice</B> (iterator position, list<T, Allocator>& x, iterator i);</PRE>
<UL><P>Moves the elements pointed to by iterator <SAMP>i</SAMP> in <SAMP>x</SAMP> to self, inserting it before <SAMP>position</SAMP>. The element is removed from <SAMP>x</SAMP>.</P>
</UL>
<PRE>void
<B>splice</B> (iterator position, list<T, Allocator >& x,
iterator first, iterator last);</PRE>
<UL><P>Moves the elements in the range <SAMP>[first, last)</SAMP> in <SAMP>x</SAMP> to self, inserting before <SAMP>position</SAMP>. The elements in the range <SAMP>[first, last)</SAMP> are removed from <SAMP>x</SAMP>.</P>
</UL>
<PRE>void
<B>swap</B> (list <T, Allocator>& x);</PRE>
<UL><P>Exchanges self with <SAMP>x</SAMP>.</P>
</UL>
<PRE>void
<B>unique</B> ();</PRE>
<UL><P>Erases copies of consecutive repeated elements leaving the first occurrrence.</P>
</UL>
<PRE>template <class BinaryPredicate>
void
<B>unique</B> (BinaryPredicate binary_pred);</PRE>
<UL><P>Erases consecutive elements matching a true condition of the <SAMP>binary_pred</SAMP>. The first occurrence is not removed.</P>
</UL>
<A NAME="Non-member Operators"><H3>Non-member Operators</H3></A>
<PRE>template <class T, class Allocator>
bool <B>operator==</B> (const list<T, Allocator>& x,
const list<T, Allocator>& y);</PRE>
<UL><P>Equality operator. Returns <SAMP>true</SAMP> if <SAMP>x</SAMP> is the same as <SAMP>y</SAMP>.</P>
</UL>
<PRE>template <class T, class Allocator>
bool <B>operator<</B> (const list<T, Allocator>& x,
const list<T,Allocator>& y);</PRE>
<UL><P>Returns <SAMP>true</SAMP> if the sequence defined by the elements contaned in <SAMP>x</SAMP> is lexicographically less than the sequence defined by the elements contained in <SAMP>y</SAMP>.</P>
</UL>
<PRE>template <class T, class Allocator>
void <B>swap</B> (list<T, Allocator>& a, list<T, Allocator>& b);</PRE>
<UL><P>Efficiently swaps the contents of <SAMP>a</SAMP> and <SAMP>b</SAMP>.</P>
</UL>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// list.cpp
//
#include <list>
#include <string>
#include <iostream.h></PRE><PRE> // Print out a list of strings
ostream& operator<<(ostream& out, const list<string>& l)
{
copy(l.begin(), l.end(), ostream_iterator<string>(cout," "));
return out;
}</PRE><PRE> int main(void)
{
// create a list of critters
list<string> critters;
int i;</PRE><PRE> // insert several critters
critters.insert(critters.begin(),"antelope");
critters.insert(critters.begin(),"bear");
critters.insert(critters.begin(),"cat");
// print out the list
cout << critters << endl;
// Change cat to cougar
*find(critters.begin(),critters.end(),"cat") = "cougar";
cout << critters << endl;
// put a zebra at the beginning
// an ocelot ahead of antelope
// and a rat at the end
critters.push_front("zebra");
critters.insert(find(critters.begin(),critters.end(),
"antelope"),"ocelot");
critters.push_back("rat");
cout << critters << endl;
// sort the list (Use list's sort function since the
// generic algorithm requires a random access iterator
// and list only provides bidirectional)
critters.sort();
cout << critters << endl;
// now let's erase half of the critters
int half = critters.size() >> 1;
for(i = 0; i < half; ++i) {
critters.erase(critters.begin());
}
cout << critters << endl;</PRE>
<PRE> return 0;</PRE>
<PRE> }
Output :
cat bear antelope
cougar bear antelope
zebra cougar bear ocelot antelope rat
antelope bear cougar ocelot rat zebra
ocelot rat zebra
</PRE>
<A NAME="Warnings"><H3>Warnings</H3></A>
<P>Member function templates are used in all containers provided by the Standard Template Library. An example of this feature is the constructor for <B><I>list<T, Allocator></B></I> that takes two templated iterators:</P>
<PRE>template <class InputIterator></PRE>
<PRE>list (InputIterator, InputIterator, const Allocator& = Allocator());
</PRE><P><B><I>list</B></I> also has an <SAMP>insert</SAMP> function of this type. These functions, when not restricted by compiler limitations, allow you to use any type of input iterator as arguments. For compilers that do not support this feature, we provide substitute functions that allow you to use an iterator obtained from the same type of container as the one you are constructing (or calling a member function on), or you can use a pointer to the type of element you have in the container.</P>
<P>For example, if your compiler does not support member function templates you can construct a list in the following two ways:</P>
<PRE></PRE>
<PRE>int intarray[10];
list<int> first_list(intarray,intarray + 10);
list<int> second_list(first_list.begin(),first_list.end());
</PRE><P>But not this way:</P>
<PRE></PRE>
<PRE>list<long> long_list(first_list.begin(),first_list.end());
</PRE><P>since the <SAMP>long_list</SAMP> and <SAMP>first_list</SAMP> are not the same type.</P>
<P>Additionally, <B><I>list</B></I> provides a <SAMP>merge </SAMP>function of this type. </P>
<PRE>template <class Compare> void merge (list<T, Allocator>&,
Compare);
</PRE><P>This function allows you to specify a compare function object to be used in merging two lists. In this case, we were unable to provide a substitute function in addition to the merge that uses the <SAMP>operator< </SAMP>as the default. Thus, if your compiler does not support member function templates, all list mergers will use <SAMP>operator<</SAMP>.</P>
<P>Also, many compilers do not support default template arguments. If your compiler is one of these, you need to always supply the <SAMP>Allocator</SAMP> template argument. For instance, you'll have to write: </P>
<PRE>list<int, allocator></PRE>
<PRE></PRE><P>instead of:</P>
<PRE>list<int></PRE>
<A NAME="See Also"><H3>See Also</H3></A>
<P><A HREF="all_7029.htm"><B><I>allocator</B></I></A>, <A HREF="Con_2487.htm"><B><I>Containers</B></I></A>, <A HREF="Ite_5295.htm"><B><I>Iterators</B></I></A></P>
<HR>
<A HREF="lim_2532.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="log_2801.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -