📄 bac_0189.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>©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 <iterator></PRE>
<PRE>
template <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 <class Container>
class back_insert_iterator : public output_iterator {
protected:
Container& container;
public:
back_insert_iterator (Container&);
back_insert_iterator<Container>&
operator= (const Container::value_type&);
back_insert_iterator<Container>& operator* ();
back_insert_iterator<Container>& operator++ ();
back_insert_iterator<Container> operator++ (int);
};
template <class Container>
back_insert_iterator<Container> back_inserter (Container&);
</PRE>
<A NAME="Constructor"><H3>Constructor</H3></A>
<PRE><B>back_insert_iterator </B>(Container& 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<Container>&
<B>operator=</B> (const Container::value_type& 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<Container>&
<B>operator*</B> ();</PRE>
<UL><P>Returns <SAMP>*this</SAMP>.</P>
</UL>
<PRE>back_insert_iterator<Container>&
<B>operator++</B> ();
back_insert_iterator<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 <class Container>
back_insert_iterator<Container>
<B>back_inserter</B> (Container& 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 <iterator>
#include <deque>
#include <iostream.h>
int main ()
{
//
// Initialize a deque using an array.
//
int arr[4] = { 3,4,7,8 };
deque<int> d(arr+0, arr+4);
//
// Output the original deque.
//
cout << "Start with a deque: " << endl << " ";
copy(d.begin(), d.end(), ostream_iterator<int>(cout," "));
//
// Insert into the middle.
//
insert_iterator<deque<int> > ins(d, d.begin()+2);
*ins = 5; *ins = 6;
//
// Output the new deque.
//
cout << endl << endl;
cout << "Use an insert_iterator: " << endl << " ";
copy(d.begin(), d.end(), ostream_iterator<int>(cout," "));
//
// A deque of four 1s.
//
deque<int> d2(4, 1);
//
// Insert d2 at front of d.
//
copy(d2.begin(), d2.end(), front_inserter(d));
//
// Output the new deque.
//
cout << endl << endl;
cout << "Use a front_inserter: " << endl << " ";
copy(d.begin(), d.end(), ostream_iterator<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 << endl << endl;
cout << "Use a back_inserter: " << endl << " ";
copy(d.begin(), d.end(), ostream_iterator<int>(cout," "));
cout << 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<int,allocator></PRE>
<PRE></PRE><P>instead of:</P>
<PRE>vector<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 + -