[STL ] Container中如何新增member function?

看板C_and_CPP (C/C++)作者 (雲淡風輕)時間17年前 (2009/04/29 17:32), 編輯推噓5(505)
留言10則, 4人參與, 最新討論串1/1
想問一下~ 若今天我使用STL中的container 想對其新增 operator function或其它member function的話 我應該怎做? ex. stack中 它所用的Member functions (constructor) empty() Test whether container is empty (public member function) size() Return size (public member function) top() Access next element (public member function) push() Add element (public member function) pop() Remove element (public member function) 我想要新增 operator[](size_t i); 不知該如何下手? 還是我只能用繼承的方式去處理及新增? 感謝回答~ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.138.155.196

04/29 17:37, , 1F
直接宣一個stack當成員變數如何?
04/29 17:37, 1F

04/29 17:49, , 2F
我是想看看有沒有直接寫 不要自己寫class處理的方法~
04/29 17:49, 2F

04/29 18:03, , 3F
看來只能用 friend 啦 請參閱 operator <<(..)
04/29 18:03, 3F

04/29 18:05, , 4F
我記錯了 請無視之
04/29 18:05, 4F

04/29 18:06, , 5F
剛剛還在想 我直接宣告stack 那這樣怎用friend XD
04/29 18:06, 5F

04/29 18:08, , 6F
請忘了stack吧 改用deque
04/29 18:08, 6F

04/29 18:09, , 7F
stack是deque的功能限縮版
04/29 18:09, 7F

04/29 18:22, , 8F
看來要自己加member function 只能自開class+繼承囉~
04/29 18:22, 8F

04/29 22:59, , 9F
繼承 STL 容器是重大錯誤,這樣寫 0 分。
04/29 22:59, 9F

04/30 01:25, , 10F
多用組合,少用繼承
04/30 01:25, 10F
文章代碼(AID): #19-1wrZo (C_and_CPP)
文章代碼(AID): #19-1wrZo (C_and_CPP)