blob: 4c12ba43e12a074820b2b3999bfb2d07d9927776 [file] [log] [blame]
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed
with this work for additional information regarding copyright
ownership. The ASF licenses this file to you under the Apache
License, Version 2.0 (the License); you may not use this file
except in compliance with the License. You may obtain a copy of
the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied. See the License for the specific language governing
permissions and limitations under the License.
Copyright 1999-2007 Rogue Wave Software, Inc.
-->
<HTML>
<HEAD>
<TITLE>back_insert_iterator, back_inserter()</TITLE>
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD>
<BODY BGCOLOR=#FFFFFF>
<A HREF="auto-ptr.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="bad-alloc.html"><IMG SRC="images/bnext.gif" WIDTH=25 HEIGHT=21 ALT="Next file" BORDER=O></A><DIV CLASS="DOCUMENTNAME"><B>Apache C++ Standard Library Reference Guide</B></DIV>
<H2>back_insert_iterator, back_inserter()</H2>
<P><B>Library:</B>&nbsp;&nbsp;<A HREF="2-8.html">Iterators</A></P>
<PRE><HR><B><I><A HREF="back-insert-iterator.html">back_insert_iterator</A></I></B> <IMG SRC="images/inherits.gif"> <B><I><A HREF="iterator.html">iterator</A></I></B><HR></PRE>
<UL>
<LI><A HREF="#sec1">Local Index</A></LI>
<LI><A HREF="#sec2">Summary</A></LI>
<LI><A HREF="#sec3">Synopsis</A></LI>
<LI><A HREF="#sec4">Description</A></LI>
<LI><A HREF="#sec5">Interface</A></LI>
<LI><A HREF="#sec6">Member Types</A></LI>
<LI><A HREF="#sec7">Constructors</A></LI>
<LI><A HREF="#sec8">Operators</A></LI>
<LI><A HREF="#sec9">Helper Functions</A></LI>
<LI><A HREF="#sec10">Example</A></LI>
<LI><A HREF="#sec11">See Also</A></LI>
<LI><A HREF="#sec12">Standards Conformance</A></LI>
</UL>
<A NAME="sec1"><H3>Local Index</H3></A>
<H4>Members</H4>
<UL><TABLE CELLPADDING=3>
<TR><TD VALIGN=top>
<A HREF="#idx39">back_inserter()</A><BR>
<A HREF="#idx34">back_insert_iterator()</A><BR>
</TD>
<TD VALIGN=top><A HREF="#idx33">container_type</A><BR>
<A HREF="#idx36">operator*()</A><BR>
</TD>
<TD VALIGN=top><A HREF="#idx38">operator++()</A><BR>
<A HREF="#idx35">operator=()</A><BR>
</TD>
<TD VALIGN=top></TD></TR>
</TABLE></UL>
<A NAME="sec2"><H3>Summary</H3></A>
<P>An output iterator used to insert items at the end of a collection</P>
<A NAME="sec3"><H3>Synopsis</H3></A>
<PRE>#include &lt;iterator&gt;
namespace std {
template &lt;class Container&gt;
class back_insert_iterator;
}
</PRE>
<A NAME="sec4"><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 template specialization <B><I><A HREF="back-insert-iterator.html">back_insert_iterator</A></I></B> is used to insert items at the end of a collection. The convenience function template <SAMP><A HREF="back-insert-iterator.html">back_inserter()</A></SAMP> creates an instance of a <B><I>back_insert_iterator</I></B> for a particular collection type. A <B><I>back_insert_iterator</I></B> can be used with any container that defines the <SAMP>push_back()</SAMP> member function, specifically all sequences (e.g., <B><I><A HREF="vector.html">vector</A></I></B>, <B><I><A HREF="deque.html">deque</A></I></B>, <B><I><A HREF="basic-string.html">basic_string</A></I></B>, and <B><I><A HREF="list.html">list</A></I></B>), but not with associative containers (e.g., <B><I><A HREF="map.html">map</A></I></B> or <B><I><A HREF="set.html">set</A></I></B>).</P>
<A NAME="sec5"><H3>Interface</H3></A>
<UL><PRE>namespace std {
template &lt;class Container&gt;
class back_insert_iterator
: public iterator &lt;output_iterator_tag, void, void, void,
void&gt; {
protected:
Container* container;
public:
typedef Container container_type;
explicit back_insert_iterator (container_type&amp;);
back_insert_iterator&amp; operator=
(const typename
container_type::const_reference value);
back_insert_iterator&amp; operator* ();
back_insert_iterator&amp; operator++ ();
back_insert_iterator operator++ (int);
};
template &lt;class Container&gt;
back_insert_iterator&lt;Container&gt; back_inserter(Container&amp;);
}
</PRE></UL>
<A NAME="sec6"><H3>Member Types</H3></A>
<A NAME="idx33"></A><PRE><B>container_type</B></PRE>
<UL>
<P>The type of container acted on by this iterator.</P>
</UL>
<A NAME="sec7"><H3>Constructors</H3></A>
<A NAME="idx34"></A><PRE>explicit
<B>back_insert_iterator</B> (container_type&amp; x);</PRE>
<UL>
<P>Constructor. Creates an instance of a <B><I><A HREF="back-insert-iterator.html">back_insert_iterator</A></I></B> associated with container <SAMP>x.</SAMP></P>
</UL>
<A NAME="sec8"><H3>Operators</H3></A>
<A NAME="idx35"></A><PRE>back_insert_iterator&amp;
<B>operator=</B> (const typename container_type::constant_reference value);</PRE>
<UL>
<P>Inserts a copy of <SAMP>value</SAMP> at the end of the container by calling <SAMP>container-&gt;push_back(value)</SAMP>, and returns <SAMP>*this</SAMP>.</P>
</UL>
<A NAME="idx36"></A><PRE>back_insert_iterator&amp;
<B>operator*</B> ();</PRE>
<UL>
<P>Returns <SAMP>*this</SAMP> (the iterator itself).</P>
</UL>
<A NAME="idx37"></A><PRE>back_insert_iterator&amp;
<B>operator* </B>();</PRE>
<UL>
<P>Returns <SAMP>*this</SAMP>.</P>
</UL>
<A NAME="idx38"></A><PRE>back_insert_iterator&amp;
<B>operator++</B> ();
back_insert_iterator
<B>operator++</B> (int);</PRE>
<UL>
<P>Increments the input iterator and returns <SAMP>*this</SAMP>.</P>
</UL>
<A NAME="sec9"><H3>Helper Functions</H3></A>
<A NAME="idx39"></A><PRE>template &lt;class Container&gt;
back_insert_iterator&lt;Container&gt;
<B>back_inserter</B> (Container&amp; x)</PRE>
<UL>
<P>Returns a <B><I><A HREF="back-insert-iterator.html">back_insert_iterator</A></I></B> that inserts elements at the end of container <SAMP>x</SAMP>.</P>
</UL>
<A NAME="sec10"><H3>Example</H3></A>
<UL><PRE>//
// ins_itr.cpp
//
#include &lt;algorithm&gt; // for copy
#include &lt;iostream&gt; // for cout, endl
#include &lt;iterator&gt; // for ostream_iterator, xxx_inserter
#include &lt;deque&gt; // for deque
int main ()
{
// Typedefs for convenience.
typedef std::deque&lt;int, std::allocator&lt;int&gt; &gt; Deque;
typedef std::ostream_iterator&lt;int, char,
std::char_traits&lt;char&gt; &gt; os_iter;
// Initialize a deque using an array.
Deque::value_type arr[] = { 3, 4, 7, 8 };
Deque d (arr, arr + sizeof arr / sizeof *arr);
// Output the original deque.
std::cout &lt;&lt; "Start with a deque: \n ";
std::copy (d.begin (), d.end (), os_iter (std::cout, " "));
// Insert into the middle.
std::insert_iterator&lt;Deque&gt; ins (d, d.begin () + 2);
*ins = 5;
*ins = 6;
// Output the new deque.
std::cout &lt;&lt; "\n\nUse an insert_iterator: \n ";
std::copy (d.begin (), d.end (), os_iter (std::cout, " "));
// A deque of four 1s.
Deque d2 (4, 1);
// Insert d2 at front of d.
std::copy (d2.begin (), d2.end (),
std::front_inserter (d));
// Output the new deque.
std::cout &lt;&lt; "\n\nUse a front_inserter: \n ";
std::copy (d.begin (), d.end (), os_iter (std::cout, " "));
// Insert d2 at back of d.
std::copy (d2.begin (), d2.end (), std::back_inserter (d));
// Output the new deque.
std::cout &lt;&lt; "\n\nUse a back_inserter: \n ";
std::copy (d.begin (), d.end (), os_iter (std::cout, " "));
std::cout &lt;&lt; std::endl;
return 0;
}
Program Output
</PRE></UL>
<UL><PRE>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></UL>
<A NAME="sec11"><H3>See Also</H3></A>
<P><A HREF="insertiterators.html">Insert Iterators</A></P>
<A NAME="sec12"><H3>Standards Conformance</H3></A>
<P><I>ISO/IEC 14882:1998 -- International Standard for Information Systems -- Programming Language C++, Section 24.4.2.1</I></P>
<BR>
<HR>
<A HREF="auto-ptr.html"><IMG SRC="images/bprev.gif" WIDTH=20 HEIGHT=21 ALT="Previous file" BORDER=O></A><A HREF="noframes.html"><IMG SRC="images/btop.gif" WIDTH=56 HEIGHT=21 ALT="Top of Document" BORDER=O></A><A HREF="booktoc.html"><IMG SRC="images/btoc.gif" WIDTH=56 HEIGHT=21 ALT="Contents" BORDER=O></A><A HREF="tindex.html"><IMG SRC="images/bindex.gif" WIDTH=56 HEIGHT=21 ALT="Index page" BORDER=O></A><A HREF="bad-alloc.html"><IMG SRC="images/bnext.gif" WIDTH=20 HEIGHT=21 ALT="Next file" BORDER=O></A>
<!-- Google Analytics tracking code -->
<script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-1775151-1";
urchinTracker();
</script>
<!-- end of Google Analytics tracking code -->
</BODY>
</HTML>