📄 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 + -