blob: ddb66ed4b3e756b41f291b58c9c0d3f6f4967b2c [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>insert_iterator, inserter()</TITLE>
<LINK REL=StyleSheet HREF="../rw.css" TYPE="text/css" TITLE="Apache stdcxx Stylesheet"></HEAD>
<BODY BGCOLOR=#FFFFFF>
<A HREF="insertiterators.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="invalid-argument.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>insert_iterator, inserter()</H2>
<P><B>Library:</B>&nbsp;&nbsp;<A HREF="2-8.html">Iterators</A></P>
<PRE><HR><B><I><A HREF="insert-iterator.html">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">Nonmember 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="#idx663">container_type</A><BR>
<A HREF="#idx664">insert_iterator()</A><BR>
</TD>
<TD VALIGN=top><A HREF="#idx666">operator*()</A><BR>
<A HREF="#idx667">operator++()</A><BR>
</TD>
<TD VALIGN=top><A HREF="#idx665">operator=()</A><BR>
</TD></TR>
</TABLE></UL>
<H4>Non-Members</H4>
<UL><TABLE CELLPADDING=3>
<TR><TD VALIGN=top>
<A HREF="#idx668">inserter()</A><BR>
</TD>
<TD VALIGN=top></TD></TR>
</TABLE></UL>
<A NAME="sec2"><H3>Summary</H3></A>
<P><B><I><A HREF="insert-iterator.html">insert_iterator</A></I></B> is an insert iterator used to insert items into a collection rather than overwrite the collection</P>
<A NAME="sec3"><H3>Synopsis</H3></A>
<PRE>#include &lt;iterator&gt;
namespace std {
template &lt;class Container&gt;
class 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 <B><I><A HREF="insert-iterator.html">insert_iterator</A></I></B> is used to insert items into a specified location of a collection. The function <SAMP><A HREF="insert-iterator.html">inserter()</A></SAMP> creates an instance of an <B><I>insert_iterator</I></B> given a particular collection type and iterator. An <B><I>insert_iterator</I></B> can be used with <B><I><A HREF="vector.html">vector</A></I></B>s, <B><I><A HREF="deque.html">deque</A></I></B>s, <B><I><A HREF="list.html">list</A></I></B>s, <B><I><A HREF="map.html">map</A></I></B>s and <B><I><A HREF="set.html">set</A></I></B>s.</P>
<A NAME="sec5"><H3>Interface</H3></A>
<UL><PRE>namespace std{
template &lt;class Container&gt;
class insert_iterator : public
iterator&lt;output_iterator_tag,void,void,void,void&gt;; {
protected:
Container* container;
public:
typedef Container container_type;
insert_iterator(Container&amp;, typename Container::iterator);
insert_iterator&lt;Container&gt;&amp;
operator=(const typename Container::value_type&amp;);
insert_iterator&lt;Container&gt;&amp; operator*();
insert_iterator&lt;Container&gt;&amp; operator++();
insert_iterator&lt;Container&gt;&amp; operator++(int);
};
template &lt;class Container, class Iterator&gt;
insert_iterator&lt;Container&gt; inserter(Container&amp;, Iterator)
}
</PRE></UL>
<A NAME="sec6"><H3>Member Types</H3></A>
<A NAME="idx663"></A><PRE><B>container_type</B></PRE>
<UL>
<P>The type of container acted on by the iterator.</P>
</UL>
<A NAME="sec7"><H3>Constructors</H3></A>
<A NAME="idx664"></A><PRE><B>insert_iterator</B>(Container&amp; x,
typename Container::iterator i);</PRE>
<UL>
<P>Creates an instance of an <B><I><A HREF="insert-iterator.html">insert_iterator</A></I></B> associated with container <SAMP>x</SAMP> and iterator <SAMP>i.</SAMP></P>
</UL>
<A NAME="sec8"><H3>Operators</H3></A>
<A NAME="idx665"></A><PRE>insert_iterator&lt;Container&gt;&amp;
<B>operator=</B>(const typename Container::value_type&amp; value);</PRE>
<UL>
<P>Inserts a copy of <SAMP>value</SAMP> into the container at the location specified by the <SAMP>insert_iterator</SAMP>, increments the iterator, and returns <SAMP>*this</SAMP>.</P>
</UL>
<A NAME="idx666"></A><PRE>insert_iterator&lt;Container&gt;&amp;
<B>operator*</B>();</PRE>
<UL>
<P>Returns <SAMP>*this</SAMP> (the input iterator itself).</P>
</UL>
<A NAME="idx667"></A><PRE>insert_iterator&lt;Container&gt;&amp;
<B>operator++</B>();
insert_iterator&lt;Container&gt;&amp;
<B>operator++</B>(int);</PRE>
<UL>
<P>Increments the insert iterator and returns <SAMP>*this</SAMP>.</P>
</UL>
<A NAME="sec9"><H3>Nonmember Functions</H3></A>
<A NAME="idx668"></A><PRE>template &lt;class Container, class Iterator&gt;
insert_iterator&lt;Container&gt;
<B>inserter</B>(Container&amp; x, Iterator i);</PRE>
<UL>
<P>Returns an <B><I><A HREF="insert-iterator.html">insert_iterator</A></I></B> that inserts elements into container <SAMP>x</SAMP> at location <SAMP>i</SAMP>. This function allows you to create insert iterators inline.</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><B><I><A HREF="back-insert-iterator.html">back_insert_iterator</A></I></B>, <B><I><A HREF="front-insert-iterator.html">front_insert_iterator</A></I></B>, <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.5</I></P>
<BR>
<HR>
<A HREF="insertiterators.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="invalid-argument.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>