[問題] pass iterator by reference

看板C_and_CPP作者 (他口)時間12年前 (2012/08/10 08:41), 編輯推噓1(107)
留言8則, 5人參與, 最新討論串1/2 (看更多)
最近在練習寫點小程式的時候 剛好遇到一個問題需要將一個iterator傳進fuction中, 而該function會對iterator做些移動(++iterator), 因為本身function有其他需要回傳的值, 所以就沒有將iterator回傳,對我來說這直覺地解法就是用 pass iterator by reference 的方式 (ex. double foo(vector<int>::iterator & it)) 不過蠻意外,這樣code不能被compiler接受 要加const才能過(加上const對我來說就沒有pass reference的必要) 在網路上查了一陣子 還沒找到很能說服我的說法 目前看起好像是來封裝保密的問題? 不知道版友們能不能給我一些看法或想法 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 163.30.138.2

08/10 09:47, , 1F
http://codepad.org/KnSOM3ru 看起來是可以的阿..?
08/10 09:47, 1F

08/10 10:37, , 2F
貼一下你的code吧
08/10 10:37, 2F

08/10 11:20, , 3F
const是說 const <>::iter & or <>::const_iter & ?
08/10 11:20, 3F

08/10 13:24, , 4F
原本code有點臭長 類似像這樣http://codepad.org/KuMP6Imb
08/10 13:24, 4F

08/10 13:24, , 5F
const <>::iter
08/10 13:24, 5F

08/10 13:28, , 6F
http://codepad.org/4UpxRqgz 阿不過好像這樣改就可以了
08/10 13:28, 6F

08/10 13:34, , 7F
該不會是begin回傳出來的東西是const <>::iterator orz
08/10 13:34, 7F

08/10 13:55, , 8F
原本的寫法傳給func()的是一個暫存值(rvalue ref
08/10 13:55, 8F
文章代碼(AID): #1G95Yz2_ (C_and_CPP)
文章代碼(AID): #1G95Yz2_ (C_and_CPP)