C_and_CPP 板


LINE

※ 引述《khoguan (Khoguan Phuann)》之銘言: : ※ 引述《penggw (~夢的點滴~)》之銘言: : : 現在我要輸入一個時間 : : 格式 : : 例: 12:59 : : 9:00 : : 把小時存在int hour裡面 : : 分鐘存在int min裡面 : : scanf("%d:%d",&hour,&min)可以輸入 : : 那用cin要如何達成同樣目的? : C++ standard library 沒有額外設計類似 scanf 這種 : 功能的函式可以用。或者繼續沿用 scanf 或者就要寫得 : 比較麻煩一點。像是 : char buf[3]; : cin.getline(buf, 3, ':'); // 到 : 為止 : int hour = atoi(buf); : cin.getline(buf, 3); // 到 end of line 為止 : int min = atoi(buf); 以上是簡單的做法。因為我感到 field parsing 是常常要用到 的操作,C++ 標準程式庫未能提供現成的東西可以用,一時手癢, 就寫了一個比較一般化的解法,可以指定一種或多種欄位分隔號。 因為已經超出 penggw 的需求,因此我重訂了標題,尚祈海涵。 我寫的工具,使用的方法,是一種 I/O manipulator 技巧的應用: Field timef(":"); // 指定用 : 做為欄位分隔符號,可以一次指定多種符號 // 讀進的資料只要遇到其中任何一個都視為欄位分隔號 // 連續兩個分隔號,中間仍算是一欄,為空資料欄位 // 預設以一行為record,最後一欄後面不要再加分隔符 //Fild timef(":", false); // 這樣則表示最後一欄後面要加分隔符,一欄的 // 資料就可以跨行,但是 client code 要自行 // 處理掉後面的多餘字元(包括'\n'),例如使用 // cin.ignore(INT_MAX, '\n'); // 後續的輸入才不會有問題 // 輸入整數的例子 int hour = 0, min = 0; cin >> timef(hour) >> timef(min); //如12:23的12存到 hour, 23存到 min // 輸入字串的例子 Field namef("|;"); // | 和 ; 都視為分隔號 string name1, name2, name3; cin >> namef(name1) >> namef(name2) >> namef(name3); /* 輸入 Brian W. Kernighan|Dennis M. Ritchie;Bjarne Stroustrup 以後,name1 == Brian W. Kernighan name2 == Dennis M. Ritche name3 == Bjarne Stroustrup */ 用 string 去接,可以輸入任意長度的字串。也可以用 char arrary 去接,程式會自動將超出的輸入部份丟掉,不會 overflow. 如 char n1[30], n2[30], n3[30]; cin >> namef(n1) >> namef(n2) >> namef(n3); char array 和 char* 不同,若是用 char* 去接,只會存到whitespace 之前,並且輸入欄位長度不能超過 buffer 各種 type 也可混合在一起使用。 用起來還不錯 :-) 不過若要更複雜的 parsing 功能,可能就得 借用行家所做的 regular expression library 了。 程式碼實作如下,敬請批評指教。 /* * C++ Field Parsing Manipulator for istream * by Khoguan Phuann, 2005/05/23 */ #include <iostream> #include <string> #include <cstring> #include <sstream> using namespace std; template <class T> struct GetField; class Field { public: Field(const string& d = " ", bool e = true) : di(d), eol(e) { } template <class T> GetField<T> operator() (T& val) { return GetField<T>(*this, val); } // 3 friend operator>> template <class T> friend istream& operator>> (istream& is, const GetField<T>& gf); template <int LEN> friend istream& operator>> (istream& is, const GetField<char[LEN]>& gf); friend istream& operator>> (istream& is, const GetField<string>& gf); private: string di; // delimiters string bool eol; // wheter \n serves as delimiter }; template <class T> struct GetField { GetField(const Field& ff, T& v) : f(ff), val(v) { } const Field& f; T& val; }; template <class T> istream& operator>> (istream& is, const GetField<T>& gf) { string buf; char ch; while (is.get(ch)) { if (gf.f.di.find(ch) != -1 || (gf.f.eol && ch == '\n')) { istringstream istr(buf); istr >> gf.val; break; } buf += ch; } return is; } istream& operator>> (istream& is, const GetField<string>& gf) /* overloaded for GetField<string> */ { string tmp; char ch; while (is.get(ch)) { if (gf.f.di.find(ch) != -1 || (gf.f.eol && ch == '\n')) break; tmp += ch; } gf.val = tmp; return is; } template <int LEN> istream& operator>> (istream& is, const GetField<char[LEN]>& gf) /* overloaded for GetField<char[LEN]> */ { string buf; char* pc = gf.val; char ch; while (is.get(ch)) { if (gf.f.di.find(ch) != -1 || (gf.f.eol && ch == '\n')) break; buf += ch; } strncpy(pc, buf.c_str(), LEN-1); pc[LEN-1] = 0; return is; } /* End of C++ Field Parsing Manipulator for istream */ --



※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.130.208.168







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燈, 水草

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

TOP