⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fro_0713.htm

📁 ARM编辑、编译软件
💻 HTM
字号:
<HTML><TITLE>front_insert_iterator, front_inserter</TITLE><BODY>
<A HREF="ref.htm"><IMG SRC="images/banner.gif"></A>
<P><STRONG>Click on the banner to return to the Class Reference home page.</STRONG></P>
<P>&copy;Copyright 1996 Rogue Wave Software</P>
<H2>front_insert_iterator, front_inserter</H2>
<HR><PRE>     Insert Iterator</PRE><HR>
<A NAME="Summary"><H3>Summary</H3></A>
<P>An insert iterator used to insert items at the beginning of a collection.</P>
<H3>Contents</H3>
<UL>
<A HREF="#Synopsis"><LI>Synopsis</LI></A>
<A HREF="#Description"><LI>Description</LI></A>
<A HREF="#Interface"><LI>Interface</LI></A>
<A HREF="#Constructor"><LI>Constructor</LI></A>
<A HREF="#Operators"><LI>Operators</LI></A>
<A HREF="#Non-member  Function"><LI>Non-member  Function</LI></A>
<A HREF="#Example"><LI>Example</LI></A>
<A HREF="#Warnings"><LI>Warnings</LI></A>
<A HREF="#See Also"><LI>See Also</LI></A>
</UL>
<A NAME="Synopsis"><H3>Synopsis</H3></A>
<PRE>#include &#60;iterator></PRE>
<PRE>
template &#60;class Container>
class <B>front_insert_iterator</B> : public output_iterator ;</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>Insert iterators let you <I>insert</I> new elements into a collection rather than copy a new element's value over the value of an existing element.  The class<B><I> front_insert_iterator</B></I> is used to insert items at the beginning of a collection.  The function <SAMP>front_inserter</SAMP> creates an instance of a<B><I> front_insert_iterator</B></I> for a particular collection type.  A <B><I>front_insert_iterator</B></I> can be used with <A HREF="deq_4164.htm"><B><I>deque</B></I></A>s and <A HREF="lis_3222.htm"><B><I>list</B></I></A>s, but not with <A HREF="map_8018.htm"><B><I>map</B></I></A>s or <A HREF="set_1649.htm"><B><I>set</B></I></A>s.</P>
<P>Note that a <B><I>front_insert_iterator</B></I> makes each element that it inserts the new front of the container.  This has the effect of reversing the order of the inserted elements.  For example, if you use a <B><I>front_insert_iterator</B></I> to insert "1" then "2" then "3" onto the front of container <SAMP>exmpl</SAMP>, you will find, after the three insertions, that the first three elements of <SAMP>exmpl</SAMP> are "3 2 1".</P>
<A NAME="Interface"><H3>Interface</H3></A>
<PRE>template &#60;class Container>
 class front_insert_iterator : public output_iterator {

public:
   explicit front_insert_iterator (Container&#38;);
   front_insert_iterator&#60;Container>&#38;
    operator= (const typename Container::value_type&#38;);
   front_insert_iterator&#60;Container>&#38; operator* ();
   front_insert_iterator&#60;Container>&#38; operator++ ();
   front_insert_iterator&#60;Container> operator++ (int);
};
 template &#60;class Container>
  front_insert_iterator&#60;Container> front_inserter (Container&#38;);</PRE>
<A NAME="Constructor"><H3>Constructor</H3></A>
<PRE>explicit
<B>front_insert_iterator</B> (Container&#38; x);</PRE>
<UL><P>Constructor.  Creates an instance of a <B><I>front_insert_iterator</B></I> associated with container <SAMP>x.</SAMP></P>
</UL>
<A NAME="Operators"><H3>Operators</H3></A>
<PRE>front_insert_iterator&#60;Container>&#38;
<B>operator= </B>(const typename Container::value_type&#38; value);</PRE>
<UL><P>Assignment Operator. Inserts a copy of <SAMP>value</SAMP> on the front of the container, and returns <SAMP>*this</SAMP>.</P>
</UL>
<PRE>front_insert_iterator&#60;Container>&#38; 
<B>operator*</B> ();</PRE>
<UL><P>Returns <SAMP>*this</SAMP> (the input iterator itself).</P>
</UL>
<PRE>front_insert_iterator&#60;Container>&#38; 
<B>operator++</B> ();</PRE>
<UL>
</UL>
<PRE>front_insert_iterator&#60;Container> 
<B>operator++</B> (int);</PRE>
<UL><P>Increments the insert iterator and returns <SAMP>*this</SAMP>.</P>
</UL>
<A NAME="Non-member  Function"><H3>Non-member  Function</H3></A>
<PRE>template &#60;class Container>
front_insert_iterator&#60;Container>
<B>front_inserter</B> (Container&#38; x)</PRE>
<UL><P>Returns a <B><I>front_insert_iterator</B></I> that will insert elements at the beginning of container <SAMP>x</SAMP>.  This function allows you to create front insert iterators inline.</P>
</UL>
<A NAME="Example"><H3>Example</H3></A>
<PRE>//
// ins_itr.cpp
//
#include &#60;iterator>
#include &#60;deque>
#include &#60;iostream.h>
 int main ()
 {
   //
   // Initialize a deque using an array.
   //
   int arr[4] = { 3,4,7,8 };
   deque&#60;int> d(arr+0, arr+4);
   //
   // Output the original deque.
   //
   cout &#60;&#60; "Start with a deque: " &#60;&#60; endl &#60;&#60; "     ";
   copy(d.begin(), d.end(), ostream_iterator&#60;int>(cout," "));
   //
   // Insert into the middle.
   //
   insert_iterator&#60;deque&#60;int> > ins(d, d.begin()+2);
   *ins = 5; *ins = 6;
   //
   // Output the new deque.
   //
   cout &#60;&#60; endl &#60;&#60; endl;
   cout &#60;&#60; "Use an insert_iterator: " &#60;&#60; endl &#60;&#60; "     ";
   copy(d.begin(), d.end(), ostream_iterator&#60;int>(cout," "));
   //
   // A deque of four 1s.
   //
   deque&#60;int> d2(4, 1);
  <B> </B>//
   // Insert d2 at front of d.
   //
   copy(d2.begin(), d2.end(), <B>front_inserter</B>(d));
   //
   // Output the new deque.
   //
   cout &#60;&#60; endl &#60;&#60; endl;
   cout &#60;&#60; "Use a front_inserter: " &#60;&#60; endl &#60;&#60; "     ";
   copy(d.begin(), d.end(), ostream_iterator&#60;int>(cout," "));
   //
   // Insert d2 at back of d.
   //
   copy(d2.begin(), d2.end(), back_inserter(d));
   //
   // Output the new deque.
   //
   cout &#60;&#60; endl &#60;&#60; endl;
   cout &#60;&#60; "Use a back_inserter: " &#60;&#60; endl &#60;&#60; "     ";
   copy(d.begin(), d.end(), ostream_iterator&#60;int>(cout," "));
   cout &#60;&#60; endl;
   return 0;
 }
Output :
Start with a deque:
     3 4 7 8
Use an insert_iterator:
     3 4 5 6 7 8
Use a front_inserter:
     1 1 1 1 3 4 5 6 7 8
Use a back_inserter:
     1 1 1 1 3 4 5 6 7 8 1 1 1 1
</PRE>
<A NAME="Warnings"><H3>Warnings</H3></A>
<P>If your compiler does not support default template parameters then you need to always supply the <SAMP>Allocator</SAMP> template argument.  For instance you'll have to write:</P>
<PRE>deque&#60;int, allocator></PRE>
<PRE></PRE><P>instead of:</P>
<PRE>deque&#60;int></PRE>
<PRE></PRE>
<A NAME="See Also"><H3>See Also</H3></A>
<P><A HREF="Ins_1844.htm"><B><I>Insert Iterators</B></I></A></P>
<HR>
<A HREF="For_5773.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="Fun_7437.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -