[問題] Reverse String with recursively

看板C_and_CPP作者 (我討厭)時間6年前發表 (2019/03/11 10:46), 6年前編輯推噓2(201)
留言3則, 3人參與, 6年前最新討論串1/2 (看更多)
開發平台(Platform): (Ex: Win10, Linux, ...) 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) 額外使用到的函數庫(Library Used): (Ex: OpenGL, ...) http://tinyurl.com/y4hq4vfg 問題(Question): submit static int 無法初始化? 餵入的資料(Input): static volatile int i = 0 ; 預期的正確結果(Expected Output): Stdout: init i i1 = 0 i2 = 0 i1 = 1 i2 = 1 i1 = 2 錯誤結果(Wrong Output):i Stdout: runtime error: reference binding to null pointer of type 'struct value_type' (stl_vector.h) - leet code spiral 程式碼(Code):(請善用置底文網頁, 記得排版,禁止使用圖檔) void reverseString(vector<char>& s) { static int i = 0 ; static char *p = nullptr; if( &s[0] != p ) { p = &s[0]; i = 0; cout << "init i " << endl; } cout << "i1 = " << i << endl; if( i >= (s.size()/2) ) return; cout << "i2 = " << i << endl; swap( s[i] , s[s.size()-1-i]); i++; reverseString(s); } 補充說明(Supplement): 我的i怎不會跑... -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 39.10.126.116 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1552301160.A.F50.html ※ 編輯: IhateOGC (39.10.126.116), 03/11/2019 20:38:57 已解決 void reverseString(vector<char>& s) { static int i = 0 ; static char *p = nullptr; if( s.size() != 0 && (&s[0] != p) ){ p = &s[0]; i = 0; } if( i >= (s.size()/2) ) return; swap( s[i] , s[s.size()-1-i]); i++; reverseString(s); } ※ 編輯: IhateOGC (39.10.126.116), 03/11/2019 20:51:19

03/11 20:57, 6年前 , 1F
因為 i 不會自己重置回 0 ?
03/11 20:57, 1F

03/11 21:51, 6年前 , 2F
不會...xd
03/11 21:51, 2F

03/11 22:47, 6年前 , 3F
看output是 s是NULL時,p=&s[0]發生錯誤
03/11 22:47, 3F
文章代碼(AID): #1SXZnezG (C_and_CPP)
文章代碼(AID): #1SXZnezG (C_and_CPP)