?? csdn_文檔中心_介紹一個很好用的overwrite 迭代器.htm
字號:
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">現在使用輔助函數,用下面的代碼就可以達到我們開始的目的:</SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto">std::copy(start,
end, overwriter(container));<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN
lang=EN-US> <o:p></o:p></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如果你要從容器的某個迭代器指定位置開始拷貝的話,可以用下面的代碼:</SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto">std::copy(start,
end, overwriter(container, it));<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN
lang=EN-US> <o:p></o:p></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt"><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">這個</SPAN><SPAN
lang=EN-US>copy</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">算法執行結果返回一個</SPAN><SPAN
lang=EN-US>overwrite</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">迭代器,指向</SPAN><SPAN
lang=EN-US>container</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中被拷貝的元素的下一個位置。由于這個迭代器有一個到</SPAN><SPAN
lang=EN-US>container</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">原來迭代器</SPAN><SPAN
lang=EN-US>it</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的隱式轉換(</SPAN><SPAN
lang=EN-US>implicit conversion</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">),因此可以很方便的被用來刪除容器中剩下的元素(如果有這個需要的話),或者把另外一個序列中的元素復制接著拷貝過來。所以該解決方案式高效的、彈性的、安全的,更重要的是易用的。</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN
lang=EN-US> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">【代碼清單</SPAN><SPAN
lang=EN-US>1</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">】</SPAN><SPAN
lang=EN-US>overwrite_iterator</SPAN><SPAN
style="FONT-FAMILY: 宋體; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的實現</SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto">#include
<iterator><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"> <o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto">template<class
Cont><o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><SPAN
style="mso-spacerun: yes"> </SPAN>class
overwrite_iterator<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><SPAN
style="mso-spacerun: yes">
</SPAN>: public std::iterator<std::output_iterator_tag, void,
void> {<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto">public:<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><SPAN
style="mso-spacerun: yes"> </SPAN>typedef Cont
container_type;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><SPAN
style="mso-spacerun: yes"> </SPAN>typedef
Cont::value_type value_type;<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><SPAN
style="mso-spacerun: yes"> </SPAN>explicit
overwrite_iterator(Cont& x)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><SPAN
style="mso-spacerun: yes">
</SPAN>: cont(x), iter(x.begin())<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><SPAN
style="mso-spacerun: yes">
</SPAN>{}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><SPAN
style="mso-spacerun: yes">
</SPAN>overwrite_iterator(Cont& x, Cont::iterator
it)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><SPAN
style="mso-spacerun: yes">
</SPAN>: cont(x), iter(it)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><SPAN
style="mso-spacerun: yes">
</SPAN>{}<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><SPAN
style="mso-spacerun: yes">
</SPAN>overwrite_iterator& operator=(const Cont::value_type&
val)<o:p></o:p></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><SPAN
style="mso-spacerun: yes">
</SPAN>{</SPAN></P><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
size=3><SPAN style="mso-spacerun: yes">
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; COLOR: green; mso-shading: white; mso-pattern: gray-15 auto"><FONT
face="Times New Roman"><SPAN
style="mso-spacerun: yes">
</SPAN>//iter == cont.end() ? cont.push_back(val) : (*iter = val,
++iter); thx to sky1234<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; FONT-FAMILY: 'Times New Roman'; FONT-SIZE: 10.5pt; mso-bidi-font-size: 12.0pt; mso-shading: white; mso-pattern: gray-15 auto; mso-fareast-font-family: 宋體; mso-font-kerning: 1.0pt; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA">iter
== cont.end() ? (cont.push_back(val)<SPAN style="COLOR: red">,
iter=cont.end()</SPAN> ): (*iter = val,
++iter);</SPAN></SPAN></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
size=3><SPAN
style="mso-spacerun: yes">
</SPAN>return *this;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
size=3><SPAN style="mso-spacerun: yes">
</SPAN>}<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
size=3><SPAN style="mso-spacerun: yes">
</SPAN>overwrite_iterator&
operator*()<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
size=3><SPAN style="mso-spacerun: yes">
</SPAN>{<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
size=3><SPAN
style="mso-spacerun: yes">
</SPAN>return *this;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
size=3><SPAN style="mso-spacerun: yes">
</SPAN>}<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
size=3><SPAN style="mso-spacerun: yes">
</SPAN>overwrite_iterator&
operator++()<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
size=3><SPAN style="mso-spacerun: yes">
</SPAN>{<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
size=3><SPAN
style="mso-spacerun: yes">
</SPAN>return *this;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
size=3><SPAN style="mso-spacerun: yes">
</SPAN>}<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
size=3><SPAN style="mso-spacerun: yes">
</SPAN>overwrite_iterator
operator++(int)<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
size=3><SPAN style="mso-spacerun: yes">
</SPAN>{<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
size=3><SPAN
style="mso-spacerun: yes">
</SPAN>return *this;<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
size=3><SPAN style="mso-spacerun: yes">
</SPAN>}<o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="BACKGROUND: #d9d9d9; mso-shading: white; mso-pattern: gray-15 auto"><FONT
size=3><SPAN style="mso-spacerun: yes">
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -