[問題] std::set 傳遞至function

看板C_and_CPP作者 (ijj)時間10年前 (2013/10/14 23:48), 編輯推噓0(007)
留言7則, 5人參與, 最新討論串1/1
DEVc++ 5.3.0.4 問題(Question):在傳遞std::set到function時,如何call by reference 餵入的資料(Input):在main function 初始化一set,之後call其他function insert 數字 預期的正確結果(Expected Output):在main function得到已經insert數字的 set 錯誤結果(Wrong Output):main function 的set甚麼都沒有 程式碼如下 #include <iostream> // std::cout #include <set> using namespace std; void f(set<int> myset){ set<int>::iterator it; for(int i=1;i<11;i++){ myset.insert(i*10); } } int main () { set<int> myset; set<int>::iterator it; f(myset); for(it=myset.begin();it!=myset.end();it++){ cout<<*it<<" "; } cout<<endl; return 0; } 小弟初次接觸set 感謝各位大大囉 ※ 編輯: ijack 來自: 118.166.94.53 (10/14 23:49)

10/14 23:53, , 1F
關鍵字call by reference
10/14 23:53, 1F

10/15 00:19, , 2F
我有去google只是一直查不到甚麼才想說來問問
10/15 00:19, 2F

10/15 00:20, , 3F
樓上大大可否告知小弟如何修改呢 >< 感謝
10/15 00:20, 3F

10/15 00:26, , 4F
不要被 set<int> 這種怪(?)東西嚇到了
10/15 00:26, 4F

10/15 00:26, , 5F
當它是 int 這種東西就知道要怎麼做了
10/15 00:26, 5F

10/15 02:22, , 6F
void f(set<int> &myset) {
10/15 02:22, 6F

10/17 13:37, , 7F
GOOGLE查不到? 有點虎?
10/17 13:37, 7F
文章代碼(AID): #1IN194U0 (C_and_CPP)