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

📄 bac_0189.htm

📁 ARM编辑、编译软件
💻 HTM
字号:
<HTML><TITLE>back_insert_iterator, back_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>back_insert_iterator, back_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 end 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="#Helper Function"><LI>Helper Function</LI></A>
<A HREF="#Example"><LI>Example</LI></A>
<A HREF="#Warning"><LI>Warning</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 back_insert_iterator : public output_iterator;
</PRE>
<A NAME="Description"><H3>Description</H3></A>
<P>Insert iterators let you insert new elements into a collection rather than copy a new element's value over the value of an existing element.  The class<B><I> back_insert_iterator</B></I> is used to insert items at the end of a collection.  The function <SAMP>back_inserter</SAMP> creates an instance of a<B><I> back_insert_iterator</B></I> for a particular collection type.  A <B><I>back_insert_iterator</B></I> can be used with <A HREF="vec_0251.htm"><B><I>vector</B></I></A>s, <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>
<A NAME="Interface"><H3>Interface</H3></A>
<PRE>template &#60;class Container>
 class back_insert_iterator : public output_iterator {
protected:
   Container&#38; container;
public:
   back_insert_iterator (Container&#38;);
   back_insert_iterator&#60;Container>&#38;
    operator= (const Container::value_type&#38;);
   back_insert_iterator&#60;Container>&#38; operator* ();
   back_insert_iterator&#60;Container>&#38; operator++ ();
   back_insert_iterator&#60;Container> operator++ (int);
};
template &#60;class Container>
 back_insert_iterator&#60;Container> back_inserter (Container&#38;);
</PRE>
<A NAME="Constructor"><H3>Constructor</H3></A>
<PRE><B>back_insert_iterator </B>(Container&#38; x);</PRE>
<UL><P>Constructor.  Creates an instance of a <B><I>back_insert_iterator</B></I> associated with container <SAMP>x.</SAMP></P>
</UL>
<A NAME="Operators"><H3>Operators</H3></A>
<PRE>back_insert_iterator&#60;Container>&#38;
<B>operator=</B> (const Container::value_type&#38; value);</PRE>
<UL><P>Inserts a copy of <SAMP>value</SAMP> on the end of the container, and returns <SAMP>*this</SAMP>.</P>
</UL>
<PRE>back_insert_iterator&#60;Container>&#38; 
<B>operator*</B> ();</PRE>
<UL><P>Returns <SAMP>*this</SAMP>.</P>
</UL>
<PRE>back_insert_iterator&#60;Container>&#38; 
<B>operator++</B> ();
back_insert_iterator&#60;Container> 
<B>operator++</B> (int);</PRE>
<UL><P>Increments the input iterator and returns <SAMP>*this</SAMP>.</P>
</UL>
<A NAME="Helper Function"><H3>Helper Function</H3></A>
<PRE>template &#60;class Container>
back_insert_iterator&#60;Container>
<B>back_inserter</B> (Container&#38; x)</PRE>
<UL><P>Returns a <B><I>back_insert_iterator</B></I> that will insert elements at the end of container <SAMP>x</SAMP>.  This function allows you to create 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);
   //
   // Insert d2 at front of d.
   //
   copy(d2.begin(), d2.end(), front_inserter(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," "));
   //
<B>   // Insert d2 at back of d.</B>
<B>   //</B>
<B>   copy(d2.begin(), d2.end(), back_inserter(d));</B>
   //
   // 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="Warning"><H3>Warning</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>vector&#60;int,allocator></PRE>
<PRE></PRE><P>instead of:</P>
<PRE>vector&#60;int></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="aut_3512.htm"><IMG SRC="images/prev.gif"></A> <A HREF="ref.htm#contents"><IMG SRC="images/toc.gif"></A> <A HREF="bas_0007.htm"><IMG SRC="images/next.gif"></A></BODY></HTML>

⌨️ 快捷键说明

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