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

请输入看板名称,例如:Tech_Job站内搜寻

TOP