usingnamespacestd;intm" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

cin.getline對cin.gcount()的影響

系統(tǒng) 1975 0

cin.getline對cin.gcount()的影響 - delphiwcdj的專欄 - 博客頻道 - CSDN.NET

cin.getline對cin.gcount()的影響

分類: C/C++ 195人閱讀 評論 (0) 收藏 舉報

2011-05-08 wcdj

?

問題

測試下面代碼,解釋cin.gcount的輸出為什么會不同?

?

  1. #include<iostream> ??
  2. using ? namespace ?std;??
  3. int ?main()??
  4. {??
  5. ???? //?case?1: ??
  6. ???? char ?a[10];??
  7. ????cin.getline(a,10);?????????????? //?123456789 ??
  8. ????cout?<<?a?<<?endl;?????????? //?123456789 ??
  9. ????cout?<<?cin.gcount();????? //?Note,?10 ??
  10. ????cout?<<?endl;??
  11. ???? //?case?2: ??
  12. ???? char ?b[10];??
  13. ????cin.getline(b,10);?????????????? //?1234567890 ??
  14. ????cout?<<?b?<<?endl;?????????? //?123456789 ??
  15. ????cout?<<?cin.gcount();????? //?Note,?9 ??
  16. ???? return ?0;??
  17. }??

?

?

分析

getline()是按行讀入字符串,后面可以指定2個參數(shù)或3個參數(shù),其在istream中的實現(xiàn)如下:

  1. _Myt&?__CLR_OR_THIS_CALL?getline(_Elem?*_Str,?streamsize?_Count)??
  2. {??? //?get?up?to?_Count?characters?into?NTCS,?discard?newline ??
  3. ???? return ?(getline(_Str,?_Count,?_Myios::widen( '/n' )));??
  4. }??

?

可以看出,兩個參數(shù)的getline()實際也是調(diào)用了以'/n'為結(jié)束符的三參數(shù)getline()函數(shù)。
同時當(dāng)我們單步進入getline()的實現(xiàn)我們會發(fā)現(xiàn)這樣的判斷語句:

  1. _Myt&?__CLR_OR_THIS_CALL?getline(_Elem?*_Str,??
  2. ?????????????????????????????????streamsize?_Count,?_Elem?_Delim)??
  3. {??? //?get?up?to?_Count?characters?into?NTCS,?discard?_Delim ??
  4. ????_DEBUG_POINTER(_Str);??
  5. ????ios_base::iostate?_State?=?ios_base::goodbit;??
  6. ????_Chcount?=?0;??
  7. ???? const ?sentry?_Ok(* this ,? true );??
  8. ???? if ?(_Ok?&&?0?<?_Count)??
  9. ????{??? //?state?okay,?use?facet?to?extract ??
  10. ????????int_type?_Metadelim?=?_Traits::to_int_type(_Delim);??
  11. ????????_TRY_IO_BEGIN??
  12. ????????????int_type?_Meta?=?_Myios::rdbuf()->sgetc(); //?從輸入流讀一個字符 ??
  13. ???????? for ?(;?;?_Meta?=?_Myios::rdbuf()->snextc())??
  14. ???????????? if ?(_Traits::eq_int_type(_Traits::eof(),?_Meta))??
  15. ????????????{??? //?end?of?file,?quit ??
  16. ????????????????_State?|=?ios_base::eofbit; //?遇到文件尾,getline結(jié)束 ??
  17. ???????????????? break ;??
  18. ????????????}??
  19. ???????????? else ? if ?(_Meta?==?_Metadelim)??
  20. ????????????{??? //?got?a?delimiter,?discard?it?and?quit ??
  21. ????????????????++_Chcount;??
  22. ????????????????_Myios::rdbuf()->sbumpc(); //?這句把結(jié)束符讀掉了,如果不指定結(jié)束符,那就是把'/n'讀掉了 ??
  23. ???????????????? break ;??
  24. ????????????} //?遇到結(jié)束符,getline結(jié)束,注意這里的順序,它是先判斷是否遇到結(jié)束符,后判斷是否讀入了指定個數(shù)的。在這里計數(shù)器仍然執(zhí)行++操作 ??
  25. ???????????? else ? if ?(--_Count?<=?0)??
  26. ????????????{??? //?buffer?full,?quit ??
  27. ????????????????_State?|=?ios_base::failbit;??
  28. ???????????????? break ;??
  29. ????????????} //?讀到了指定個數(shù),執(zhí)行到這里已經(jīng)隱含了在指定個數(shù)的最后一位仍然不是結(jié)束符,由于在執(zhí)行這部分時,在對長度計數(shù)器沒有進行++操作 ??
  30. ???????????? else ??
  31. ????????????{??? //?got?a?character,?add?it?to?string ??
  32. ????????????????++_Chcount;??
  33. ????????????????*_Str++?=?_Traits::to_char_type(_Meta);??
  34. ????????????}??
  35. ????????????_CATCH_IO_END??
  36. ????}??
  37. ????*_Str?=?_Elem();???? //?add?terminating?null?character ??
  38. ????_Myios::setstate(_Chcount?==?0???_State?|?ios_base::failbit?:?_State);??
  39. ???? return ?(* this );??
  40. }??

?

所以在對于輸入 12345789 時,在最后我們輸入了 '/n',因此在判斷時,計數(shù)器仍然加1,而相對當(dāng)我們輸入 1234567890 時,在判斷 a[9] 時,由于--_Count <= 0,所以計數(shù)器沒有被遞加,因此在產(chǎn)生了區(qū)別。

?

結(jié)論:
通過對getline的分析,可以找到輸出結(jié)果不同的原因。

?


?

PS:

?

MSDN:

basic_istream::gcount
Returns the number of characters read during the last unformatted input.

  1. streamsize?gcount(?)? const ;??


Return Value
The extraction count.
Remarks
Use basic_istream::get to read unformatted characters.
Example
  1. //?basic_istream_gcount.cpp ??
  2. //?compile?with:?/EHsc ??
  3. #include?<iostream> ??
  4. using ? namespace ?std;??
  5. int ?main(?)???
  6. {??
  7. ????cout?<<? "Type?the?letter?'a':?" ;??
  8. ????ws(?cin?);??
  9. ???? char ?c[10];??
  10. ????cin.get(?&c[0],9?); //?a ??
  11. ????cout?<<?c?<<?endl; //?a ??
  12. ????cout?<<?cin.gcount(?)?<<?endl; //?1??注意,這里為什么是1,分析方法同getline,可以在get的實現(xiàn)中找的具體的原因 ??
  13. }???

Requirements
Header: <istream>
Namespace: std

?


參考

istream::getline
http://www.cplusplus.com/reference/iostream/istream/getline/
istream::get
http://www.cplusplus.com/reference/iostream/istream/get/
basic_istream::gcount
http://www.cplusplus.com/reference/iostream/istream/gcount/
http://msdn.microsoft.com/zh-cn/library/3w9exa29%28v=VS.90%29.aspx
cin.getline()
http://blog.csdn.net/kof2001kop/archive/2011/04/03/6299778.aspx
http://social.msdn.microsoft.com/Forums/zh-CN/visualcpluszhchs/thread/bacc495d-1170-4a20-90b6-80a37ba3d5b5

?

cin.getline對cin.gcount()的影響


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 海口市| 宜黄县| 聂荣县| 南川市| 平度市| 北海市| 安岳县| 双柏县| 南雄市| 牡丹江市| 天长市| 南岸区| 从化市| 多伦县| 伊宁市| 获嘉县| 沙河市| 潞城市| 安平县| 甘洛县| 阿克苏市| 和静县| 镇远县| 丰原市| 长乐市| 呼伦贝尔市| 平凉市| 建平县| 沅陵县| 射洪县| 天峻县| 贵南县| 乡宁县| 民权县| 珠海市| 正镶白旗| 石渠县| 紫金县| 伊春市| 深州市| 孝感市|