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

📄 ins_0332.htm

📁 C++标准库 C++标准库 C++标准库 C++标准库
💻 HTM
字号:
<HTML><HEAD><TITLE>2.4 Insert Iterators</TITLE></HEAD><BODY><A HREF="ug1.htm"><IMG SRC="images/banner.gif"></A><BR><A HREF="str_7181.htm"><IMG SRC="images/prev.gif"></A><A HREF="booktoc1.htm"><IMG SRC="images/toc.gif"></A><A HREF="tindex1.htm"><IMG SRC="images/tindex.gif"></A><A HREF="ite_8402.htm"><IMG SRC="images/next.gif"></A><BR><STRONG>Click on the banner to return to the user guide home page.</STRONG><H2>2.4 Insert Iterators</H2><A NAME="idx22"><!></A><P>Assignment to the dereferenced value of an output iterator is normally used to <I>overwrite </I>the contents of an existing location.  For example, the following invocation of the function<SAMP> copy()</SAMP> transfers values from one vector to another, although the space for the second vector was already set aside (and even initialized) by the declaration statement:</P><PRE>vector&#60;int> a(10);vector&#60;int> b(10);   ...copy (a.begin(), a.end(), b.begin());</PRE><P>Even structures such as lists can be overwritten in this fashion.  The following assumes that the list named <SAMP>c</SAMP> has at least ten elements.  The initial ten locations in the list will be replaced by the contents of the vector <SAMP>a</SAMP>.</P><PRE>list&#60;int> c;   ...copy (a.begin(), a.end(), c.begin());</PRE><P>With structures such as lists and sets, which are dynamically enlarged as new elements are added, it is frequently more appropriate to<I> insert</I> new values into the structure, rather than to <I>overwrite</I> existing locations.  A type of adaptor called an <I>insert iterator</I> allows us to use algorithms such as <SAMP>copy()</SAMP> to insert into the associated container, rather than overwrite elements in the container. The output operations of the iterator are changed into insertions into the associated container.  The following, for example, inserts the values of the vector <SAMP>a</SAMP> into an initially empty list:</P><PRE>list&#60;int> d;copy (a.begin(), a.end(), front_inserter(d));</PRE><A NAME="idx23"><!></A><P>There are three forms of insert iterators, all of which can be used to change a <I>copy</I> operation into an <I>insert</I> operation.  The iterator generated using <SAMP>front_inserter</SAMP>, shown above, inserts values into the front of the container.  The iterator generated by <SAMP>back_inserter</SAMP> places elements into the back of the container.  Both forms can be used with <A HREF="../stdlibcr/lis_3222.htm"><B><I>list</I></B></A>s and <A HREF="../stdlibcr/deq_4164.htm"><B><I>deque</I></B></A>s, but not with <A HREF="../stdlibcr/set_1649.htm"><B><I>set</I></B></A>s or <A HREF="../stdlibcr/map_8018.htm"><B><I>map</I></B></A>s.  <SAMP>back_inserter</SAMP>, but not <SAMP>front_inserter</SAMP>, can be used with <A HREF="../stdlibcr/vec_0251.htm"><B><I>vector</I></B></A>.</P><A NAME="idx24"><!></A><P>The third, and most general form, is <SAMP>inserter</SAMP>, which takes two arguments; a container and an iterator within the container.  This form copies elements into the specified location in the container.  (For a list, this means elements are copied immediately before the specified location).  This form can be used with all the structures for which the previous two forms work, as well as with sets and maps.</P><P>The following simple program illustrates the use of all three forms of insert iterators.  First, the values 3, 2 and 1 are inserted into the front of an initially empty list.  Note that as it is inserted, each value becomes the new front, so that the resultant list is ordered 1, 2, 3. Next, the values 7, 8 and 9 are inserted into the end of the list.  Finally, the <SAMP>find()</SAMP> operation is used to locate an iterator that denotes the 7 value, and the numbers 4, 5 and 6 are inserted immediately prior.  The result is the list of numbers from 1 to 9 in order.</P><PRE>void main() {   int threeToOne [ ] = {3, 2, 1};   int fourToSix [ ] = {4, 5, 6};   int sevenToNine [ ] = {7, 8, 9};   list&#60;int> aList;                          // first insert into the front                          // note that each value becomes new front   copy (threeToOne, threeToOne+3, front_inserter(aList));                          // then insert into the back   copy (sevenToNine, sevenToNine+3, back_inserter(aList));                          // find the seven, and insert into middle   list&#60;int>::iterator seven = find(aList.begin(), aList.end(), 7);   copy (fourToSix, fourToSix+3, inserter(aList, seven));                          // copy result to output   copy (aList.begin(), aList.end(),          ostream_iterator&#60;int,char>(cout, " "));   cout &#60;&#60; endl;</PRE><P>}</P><P>Observe that there is an important and subtle difference between the iterators created by <SAMP>inserter(aList, aList.begin())</SAMP> and <SAMP>front_inserter(aList).</SAMP>  The call on <SAMP>inserter(aList, aList.begin())</SAMP> copies values in sequence, adding each one to the front of a list, whereas <SAMP>front_inserter(aList)</SAMP> copies values making each value the new front.  The result is that <SAMP>front_inserter(aList)</SAMP> reverses the order of the original sequence, while <SAMP>inserter(aList, aList.begin())</SAMP> retains the original order.</P><HR><A HREF="str_7181.htm"><IMG SRC="images/prev.gif"></A> <A HREF="booktoc1.htm"><IMG SRC="images/toc.gif"></A><A HREF="tindex1.htm"><IMG SRC="images/tindex.gif"></A><A HREF="ite_8402.htm"><IMG SRC="images/next.gif"></A><P>&copy;Copyright 1996, Rogue Wave Software, Inc.</P></BODY></HTML>

⌨️ 快捷键说明

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