[問題] Reverse String with recursively

作者: IhateOGC (我討厭)   2019-03-11 18:45:55
開發平台(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怎不會跑...
作者: CoNsTaR ((const *))   2019-03-11 20:57:00
因為 i 不會自己重置回 0 ?
作者: IhateOGC (我討厭)   2019-03-11 21:51:00
不會...xd
作者: kokal (細菌)   2019-03-11 22:47:00
看output是 s是NULL時,p=&s[0]發生錯誤

Links booklink

Contact Us: admin [ a t ] ucptt.com