[問題] C++中iterator的疑問

看板C_and_CPP (C/C++)作者 (真實之眼)時間15年前 (2011/03/13 12:07), 編輯推噓3(303)
留言6則, 3人參與, 最新討論串1/3 (看更多)
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) VC++ 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) 問題(Question): 請問一下為何下述用法會造成錯誤 vector<string> svec(10,"B"); vector<string>::iterator it = &svec[0]; 餵入的資料(Input): 預期的正確結果(Expected Output): 錯誤結果(Wrong Output): error C2440: '正在初始化' : 無法由 'std::basic_string<_Elem,_Traits,_Ax> *' 轉 換為 'std::_Vector_iterator<_Ty,_Alloc>' 程式碼(Code):(請善用置底文網頁, 記得排版) 補充說明(Supplement): 在我的認知裡面, pointer也是一種iterator, 那為何可以使用: string *test = &svec[0]; 但是卻不可以使用 vector<string>::iterator it = &svec[0]; 請各位先進解惑 :) -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.132.254.64

03/13 12:12, , 1F
iterator 可以用 pointer 實作,但並沒規定一定是這樣
03/13 12:12, 1F

03/13 12:47, , 2F
因為他是 class 不是 pointer 阿 基本觀念都沒有 ...
03/13 12:47, 2F

03/13 13:34, , 3F
被你知道它的型態, 還需要typedef然後::來定義物件嗎?
03/13 13:34, 3F

03/13 13:34, , 4F
這是inforation hiding的一個手法
03/13 13:34, 4F

03/13 13:35, , 5F
^m
03/13 13:35, 5F

03/13 13:38, , 6F
喜歡取址就用 vector<string>::pointer
03/13 13:38, 6F
文章代碼(AID): #1DV4CU3V (C_and_CPP)
文章代碼(AID): #1DV4CU3V (C_and_CPP)