精華區beta EE_DSnP 關於我們 聯絡資訊
The "history" will never be "removed". So in your experiment (see comments below) --- ※ 引述《karate362 (最近腰不痛的卡拉特)》之銘言: : 這部分是老師寫好的函式 : 按理說是不用改 : 不過我寫的時候是發現啦 : 如果你照這樣做會發生奇妙的事情 : (1) : cmd>rec1 //輸入 : cmd>rec2 : cmd>rec3 : 照這樣輸入之後,假使我按方向鍵上 : 按理來說會出現 : cmd>rec3 : 再按一次會出現 : cmd>rec2 : 不過呢 : 如果我在按一次上之後 : 連續按delete或backspace : 把 : cmd>rec3刪成cmd> 再按enter的話 Please note that you are modifying the "copy" of the 3rd history entry, not the 3rd history itself. : 在下一個cmd> 我再按上 : 會跑出來的不是 : cmd>rec3 : 而是 : cmd>rec2 So after you type in "up" arrow, you goes to second history and that's why you see "rec2". Actually if you type "down" arrow afterwards, you will see "rec3". It is not removed. : 我想這並非我們想要的結果 : 我看了一下addHistory()函式裡的寫法 : 發現老師本來是寫一段這樣 : // add to _history : if (*tmp != 0) { : _history[_historySize++] = tmp; : _history[_historySize].clear(); : _historyIdx = _historySize; : } : 總之 意思應該是說如果我們輸入空字串的話 : 那麼history就不會被更新 : 不過因為把 _historyIdx = _historySize; : 這一句放在if區塊裡,就造成查詢history的 : index都不會被更新 就造成上面的錯誤了 I made the "history" behavior the same as in cshrc (maybe the same as bash?). You can go to your Linux command prompt and try it. It should be consistent with my code. : 我把那一句拿到if區塊外是就OK了啦... : 話說回來 我覺得這篇文章講的東西 : 如果不是我自己打的 我一定看不懂...囧 I understand what you mean, and hopefully you know what I am talking about. I think the reference code is fine. -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 59.121.134.57