[問題] 有關vector pointer的用法

看板C_and_CPP (C/C++)作者 (統一獅總冠軍)時間15年前 (2011/03/11 00:44), 編輯推噓0(009)
留言9則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) vc++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 有關vector<>::pointer 傳入function 的使用方法 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): 程式碼(Code):(請善用置底文網頁, 記得排版) vector<int> A; vector<int> B; void Select(vector<int>::pointer pvector,int index) { switch(index) { case 1: pvector=&A[0]; break; case 2: pvector=&B[0]; break; } int main() { //A B傳入值 ... vector<int>::pointer pv; //這邊是ok的 pv=&A[0]; pv=&B[0]; //不過這邊要傳function 我就不知道要怎麼使用了 Select( pv,1); return 0; } 補充說明(Supplement): -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 111.254.116.32

03/11 03:04, , 1F
你要做的功能是: 傳入一個指標, 以及一個索引, Select
03/11 03:04, 1F

03/11 03:05, , 2F
函式會回傳對應 vector<int> 物件的第一個元素位址,並
03/11 03:05, 2F

03/11 03:06, , 3F
用傳入的 pointer 物件當作輸出參數
03/11 03:06, 3F

03/11 03:14, , 4F
這種情況不是傳「物件的位址」, 就是傳「物件的參考」
03/11 03:14, 4F

03/11 03:14, , 5F
給他 http://codepad.org/YZG7HW8K 東西不要放全域
03/11 03:14, 5F

03/11 03:15, , 6F
用 .front 你之後換實作結構改的地方會比較少, 使用
03/11 03:15, 6F

03/11 03:16, , 7F
STL 容器時, 少用特定容器才有的操作
03/11 03:16, 7F

03/11 07:54, , 8F
謝謝指導~
03/11 07:54, 8F

03/11 08:54, , 9F
推楊大 XDD
03/11 08:54, 9F
文章代碼(AID): #1DUG02JO (C_and_CPP)
文章代碼(AID): #1DUG02JO (C_and_CPP)