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