📄 csdn_文档中心_介绍一个很好用的overwrite 迭代器.htm
字号:
</SPAN>operator Cont::iterator()<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 iter;<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>protected:<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>Cont& cont;<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>Cont::iterator iter;<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><FONT size=3> <o:p></o:p></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=3><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">【代码清单</SPAN><SPAN
lang=EN-US>2</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">】辅助函数(</SPAN><SPAN
lang=EN-US>helper function</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">)</SPAN></FONT></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>template<class Cont><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>overwrite_iterator<Cont> overwriter(Cont& x)
<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>{<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
overwrite_iterator<Cont>(x);<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>}<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> <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>template<class Cont><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>overwrite_iterator<Cont> overwriter(Cont& x,
Cont::iterator it) <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>{<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 overwrite_iterator<Cont>(x,
it);<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>}</FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN
lang=EN-US><FONT size=3> </FONT></SPAN><SPAN lang=EN-US><FONT
size=3> <o:p></o:p></FONT></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'"><FONT
size=3>作者介绍:略。</FONT></SPAN></P><BR></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><BR>
<TABLE align=center bgColor=#006699 border=0 cellPadding=0 cellSpacing=0
width=770>
<TBODY>
<TR bgColor=#006699>
<TD align=middle bgColor=#006699 id=white><FONT
color=#ffffff>对该文的评论</FONT></TD>
<TD align=middle>
<SCRIPT src="CSDN_文档中心_介绍一个很好用的overwrite 迭代器.files/readnum.htm"></SCRIPT>
</TD></TR></TBODY></TABLE>
<TABLE align=center bgColor=#666666 border=0 cellPadding=2 cellSpacing=1
width=770>
<TBODY>
<TR>
<TD bgColor=#cccccc colSpan=3><SPAN style="COLOR: #cccccc"><IMG height=16
hspace=1 src="CSDN_文档中心_介绍一个很好用的overwrite 迭代器.files/ico_pencil.gif"
width=16> </SPAN> sky1234 <I>(2004-4-16
10:21:41)</I> </TD></TR>
<TR>
<TD bgColor=#ffffff colSpan=3
width=532><BR>多谢easyjoy的回复。关于typename:“如果要把一个template中的某个标识符号指定为一种型别,就算意图显而易见,关键字
typename也不可或缺,因为C++的一般规则是,除了以typename修饰之外,template内的任何标识符号都被视为一个值而非一个型别。”(见《C++标准库》p11),所以上次提到的需加上typename的那些地方,按照C++标准是必须加上的,vs6可能对标准的支持有问题。同样道理,operator
Cont::iterator的Cont 前也需加上typename。 <BR></TD></TR></TBODY></TABLE>
<TABLE align=center bgColor=#666666 border=0 cellPadding=2 cellSpacing=1
width=770>
<TBODY>
<TR>
<TD bgColor=#cccccc colSpan=3><SPAN style="COLOR: #cccccc"><IMG height=16
hspace=1 src="CSDN_文档中心_介绍一个很好用的overwrite 迭代器.files/ico_pencil.gif"
width=16> </SPAN> easyjoy <I>(2004-4-14
21:37:46)</I> </TD></TR>
<TR>
<TD bgColor=#ffffff colSpan=3 width=532><BR>to
sky1234:(1)谢谢你提出的修正,原文中的确是在operator=中存在bug。3x!(2)我用VS6+SP5在Win2k上编译运行成功。关于typename,我记得C++
Primer中有个地方介绍说typename可以用来帮助某些编译器编译模板程序,区分模板类型参数和模板非类型参数的。具体的内容和位置记不清了,也不知道我有没有记错。(3)关于operator
Cont::iterator,其实文章最后一段已经介绍了,这是个隐式类型转换。see:【因此可以很方便的被用来删除容器中剩下的元素(如果有这个需要的话),或者把另外一个序列中的元素复制接着拷贝过来。】下面是个例子。
//测试overwrite_iterator void test_overwrite_itr() { std::vector<int>
v; std::vector<int> v1; for (int i=0 ;i<5;i++) { v.push_back(i);
v1.push_back(i); } vector<int> v2;
//利用隐式类型转换把copy返回值转换为Cont::iterator,实现两次copy copy(v1.begin(),v1.end(),
overwriter(v2, copy(v.begin(),v.end(),overwriter(v2)))); for (int j=0;
j<v2.size(); j++) printf("%d\n",v2[j]); } <BR></TD></TR></TBODY></TABLE>
<TABLE align=center bgColor=#666666 border=0 cellPadding=2 cellSpacing=1
width=770>
<TBODY>
<TR>
<TD bgColor=#cccccc colSpan=3><SPAN style="COLOR: #cccccc"><IMG height=16
hspace=1 src="CSDN_文档中心_介绍一个很好用的overwrite 迭代器.files/ico_pencil.gif"
width=16> </SPAN> sky1234 <I>(2004-4-12
21:54:55)</I> </TD></TR>
<TR>
<TD bgColor=#ffffff colSpan=3 width=532><BR>很受启发,多谢!
但是我在vs中无法编译其中的代码,在类似Cont::iterator和Cont::value_type的地方前面需加上
typename,不然会造成编译出错,不知道什么原因。
另外,我觉得在operator=中有一个错误,iter==cont.end()成立时,执行cont.push_back(val),这时造成iter不等于cont.end(),下次执行时错误,应该加上一行iter=cont.end(),经测试执行正确。
还有operator Cont::iterator函数是干什么的,实在看不懂,也无法通过编译,删掉不影响使用,为什么,望赐教。
<BR></TD></TR></TBODY></TABLE><BR>
<DIV align=center>
<TABLE align=center bgColor=#cccccc border=0 cellPadding=2 cellSpacing=1
width=770>
<TBODY>
<TR>
<TH bgColor=#006699 id=white><FONT
color=#ffffff>我要评论</FONT></TH></TR></TBODY></TABLE></DIV>
<DIV align=center>
<TABLE border=0 width=770>
<TBODY>
<TR>
<TD>你没有登陆,无法发表评论。 请先<A
href="http://www.csdn.net/member/login.asp?from=/Develop/read_article.asp?id=26644">登陆</A>
<A
href="http://www.csdn.net/expert/zc.asp">我要注册</A><BR></TD></TR></TBODY></TABLE></DIV><BR>
<HR noShade SIZE=1 width=770>
<TABLE border=0 cellPadding=0 cellSpacing=0 width=500>
<TBODY>
<TR align=middle>
<TD height=10 vAlign=bottom><A
href="http://www.csdn.net/intro/intro.asp?id=2">网站简介</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=5">广告服务</A> - <A
href="http://www.csdn.net/map/map.shtm">网站地图</A> - <A
href="http://www.csdn.net/help/help.asp">帮助信息</A> - <A
href="http://www.csdn.net/intro/intro.asp?id=2">联系方式</A> - <A
href="http://www.csdn.net/english">English</A> </TD>
<TD align=middle rowSpan=3><A
href="http://www.hd315.gov.cn/beian/view.asp?bianhao=010202001032100010"><IMG
border=0 height=48 src="CSDN_文档中心_介绍一个很好用的overwrite 迭代器.files/biaoshi.gif"
width=40></A></TD></TR>
<TR align=middle>
<TD vAlign=top>百联美达美公司 版权所有 京ICP证020026号</TD></TR>
<TR align=middle>
<TD vAlign=top><FONT face=Verdana>Copyright © CSDN.net, Inc. All rights
reserved</FONT></TD></TR>
<TR>
<TD height=15></TD>
<TD></TD></TR></TBODY></TABLE></DIV>
<DIV></DIV><!--内容结束//--><!--结束//--></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -