C_and_CPP 板


LINE

※ 引述《forthcoming5 (XDDD)》之銘言: : 最近自學到ifstream等寫法 : 其中有個題目是將ifstream讀出來的檔案 : 做分類+統整,檔案是.txt : txt的內容例如: : &@$@&&@@:((;(& : sh tree f m hi tm it e iuytre : Rule fixed : 100 21 : 200 38 : 300 37 : 400 35 : 500 11 : 如果在rule跟fixed前面的文字、資料不想要 : 直接取下面的Rule跟fixed及後面的數值做處理 : 應該要怎麼做呢? : 老師是有提示用vector搭配parser等作法 : 但想很久一直沒辦法 : 跪求解答,將送上300p幣,感恩 你可以想像 i(f)stream 是由一連串的字元所組成, 要在裡面找尋特定字串可 以用 <algorithm> 裡的 std::search() 來完成, 只是它要求參數必須滿足 ForwardIteartor 概念 (concept), 亦即它預期迭代器 (iterator) 在沒有做 累加時, 多次讀出來的字元必須相同; 但 istream 物件的特性卻是: 沒有做快 取的情況下, 每個字元只能讀一次, 是 InputRange 的概念. 當我們配對到字 串的同時, 我們也丟掉它了. 即使如此, 我們還是可以仿照 std::search() 的 邏輯順勢實作接受 InputIterator 的函式 drop_until(). drop_until() 的架構是這樣的: 本體裡包含一個迴圈, 每次迭代都只對參數做 一次累加還有讀值, 並且複製讀出來的物件往後做參考: template <typename InputIterator, typename InputSentinel> void drop_until(InputIterator first, InputSentinel last) { while (first != last) { auto element = *first++; // do things here } } 接著我們再加入額外的參數 pattern 作為要查找的對象, 它是一種 ForwardR- ange, 到這裡和 std::search() 還蠻像的 (為了能在配對失敗時重來, 我們再 新增一個迭代器物件): template < typename InputIterator, typename InputSentinel, typename ForwardRange > void drop_until(InputIterator first, InputSentinel last, const ForwardRange& pattern) { // match from beginning auto next_to_match = pattern.begin(); while (first != last && next_to_match != pattern.end()) { auto element = *first++; // match succeeding element in next iteration if (element == *next_to_match) { ++next_to_match; // fail to match, start over from second element } else if (element == *pattern.begin()) { next_to_match = std::next(pattern.begin()); // fail to match, start over from first element } else { next_to_match = pattern.begin(); } } } 以上還只是簡單的實作, 雖然沒辦法處理複雜的字串, 但用來解原 po 的問題 已經足夠. 有了 drop_until() 函式, 讀檔程式碼就可以這樣寫: std::ifstream file("input.txt"); drop_until( std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), "Rule fixed\n"sv ); unsigned rule, fixed; while (file >> rule >> fixed) { // do things here } 完整範例: https://wandbox.org/permlink/gAssdOddYQtopotV 不過如果你有辦法把整個檔案都讀進來變成一個大字串, 搭配 std::search() 的話會省下不少功夫 :) --



※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.216.75.43 (臺灣)
※ 文章網址: https://webptt.com/m.aspx?n=bbs/C_and_CPP/M.1602006183.A.438.html
1F:推 CoNsTaR: 說實話原 Po 沒有把檔案的語法講清楚根本無法實作 10/07 03:06
2F:→ CoNsTaR: eg. Rule fixed 出現在上面的部分?多個 Rule fixed?上 10/07 03:06
3F:→ CoNsTaR: 面的部分可以為空?... 10/07 03:06
4F:→ CoNsTaR: 先把 syntax tree 定義好再來 parse 比較實在吧 10/07 03:06
5F:推 ddavid: 也沒那麼無法實作吧,他也講得很清楚上面的東西全捨棄,所 10/07 09:41
6F:→ ddavid: 以根本不用考慮上面有多少東西或可能沒有東西,一行一行讀 10/07 09:42
7F:→ ddavid: 到某行是Rule fixed即可 10/07 09:42
※ 編輯: loveme00835 (123.193.76.216 臺灣), 10/07/2020 14:20:05
8F:推 CoNsTaR: 要是上面可以包含 Rule fixed 你要怎麼知道哪裡屬於“上 10/07 15:32
9F:→ CoNsTaR: 面”? 10/07 15:32
10F:推 ddavid: 對,你當然可以追究細節跟各種例外處理 10/08 12:48
11F:→ ddavid: 但是這跟「無法實作」完全是兩回事 10/08 12:48
12F:→ ddavid: 業界多得是根本不知道上線才會發生什麼莫名其妙狀況的實作 10/08 12:49
13F:→ ddavid: ,沒有人會跟你說我得要把所有可能性毫無遺漏才能「實作」 10/08 12:50
14F:→ ddavid: 這題目的基礎要求已經很明確了,當然可以實作 10/08 12:50
15F:→ ddavid: 只是你的實作可能會有缺陷,比如說我們都不知道原題目定義 10/08 12:51
16F:→ ddavid: 的完整文字,所以我們並不知道會不會有兩行Rule fixed,會 10/08 12:51
17F:→ ddavid: 不會有大小寫不同的rULE FIXED,會不會根本沒有一行是 10/08 12:52
18F:→ ddavid: Rule fixed,會不會在Rule fixed的前中後有\r\n\t 10/08 12:52
19F:→ ddavid: 但你還是可以先寫一個滿足已知最單純情況的實作 10/08 12:53
20F:→ ddavid: 然後如果你想到以上那些特殊情況,可是題目沒講會不會發生 10/08 12:55
21F:→ ddavid: 或該不該處理,那就表示我們可以無視讓它發生錯就錯,或者 10/08 12:55
22F:→ ddavid: 用心點為每個情況寫個處理,終究都還是可以實作一個版本啊 10/08 12:56
23F:→ ddavid: 這邊的用詞應該是「題意不夠嚴謹」而不是「無法實作」, 10/08 12:57
24F:→ ddavid: 「無法實作」這詞我會擺在根本沒講清楚基本要幹嘛或真的做 10/08 12:57
25F:→ ddavid: 不到的情況XD 10/08 12:58
26F:推 lc85301: 傳說中的客戶進門點了一盤炒飯,酒吧陷入大火 10/08 20:35
27F:推 CoNsTaR: 你當然可以寫一些自己都不知道在做什麼的程式去搪塞,但 10/08 22:46
28F:→ CoNsTaR: 對我來說那不叫實作 10/08 22:46







like.gif 您可能會有興趣的文章
icon.png[問題/行為] 貓晚上進房間會不會有憋尿問題
icon.pngRe: [閒聊] 選了錯誤的女孩成為魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一張
icon.png[心得] EMS高領長版毛衣.墨小樓MC1002
icon.png[分享] 丹龍隔熱紙GE55+33+22
icon.png[問題] 清洗洗衣機
icon.png[尋物] 窗台下的空間
icon.png[閒聊] 双極の女神1 木魔爵
icon.png[售車] 新竹 1997 march 1297cc 白色 四門
icon.png[討論] 能從照片感受到攝影者心情嗎
icon.png[狂賀] 賀賀賀賀 賀!島村卯月!總選舉NO.1
icon.png[難過] 羨慕白皮膚的女生
icon.png閱讀文章
icon.png[黑特]
icon.png[問題] SBK S1安裝於安全帽位置
icon.png[分享] 舊woo100絕版開箱!!
icon.pngRe: [無言] 關於小包衛生紙
icon.png[開箱] E5-2683V3 RX480Strix 快睿C1 簡單測試
icon.png[心得] 蒼の海賊龍 地獄 執行者16PT
icon.png[售車] 1999年Virage iO 1.8EXi
icon.png[心得] 挑戰33 LV10 獅子座pt solo
icon.png[閒聊] 手把手教你不被桶之新手主購教學
icon.png[分享] Civic Type R 量產版官方照無預警流出
icon.png[售車] Golf 4 2.0 銀色 自排
icon.png[出售] Graco提籃汽座(有底座)2000元誠可議
icon.png[問題] 請問補牙材質掉了還能再補嗎?(台中半年內
icon.png[問題] 44th 單曲 生寫竟然都給重複的啊啊!
icon.png[心得] 華南紅卡/icash 核卡
icon.png[問題] 拔牙矯正這樣正常嗎
icon.png[贈送] 老莫高業 初業 102年版
icon.png[情報] 三大行動支付 本季掀戰火
icon.png[寶寶] 博客來Amos水蠟筆5/1特價五折
icon.pngRe: [心得] 新鮮人一些面試分享
icon.png[心得] 蒼の海賊龍 地獄 麒麟25PT
icon.pngRe: [閒聊] (君の名は。雷慎入) 君名二創漫畫翻譯
icon.pngRe: [閒聊] OGN中場影片:失蹤人口局 (英文字幕)
icon.png[問題] 台灣大哥大4G訊號差
icon.png[出售] [全國]全新千尋侘草LED燈, 水草

請輸入看板名稱,例如:WOW站內搜尋

TOP