日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

C++流操纵算子(格式控制)

發(fā)布時(shí)間:2025/3/20 c/c++ 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++流操纵算子(格式控制) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.


?????? 輸入/輸出的數(shù)據(jù)沒(méi)有指定格式,它們都按缺省的格式輸入/輸出。然而,有時(shí)需要對(duì)數(shù)據(jù)格式進(jìn)行控制。這時(shí)需利用ios類(lèi)中定義的格式控制成員函數(shù),通過(guò)調(diào)用它們來(lái)完成格式的設(shè)置。


ios類(lèi)的格式控制函數(shù)如下所示:


long?flags(?)?const返回當(dāng)前的格式標(biāo)志。
long?flays(long?newflag)設(shè)置格式標(biāo)志為newflag,返回舊的格式標(biāo)志。
long?setf(long?bits)???設(shè)置指定的格式標(biāo)志位,返回舊的格式標(biāo)志。
long?setf(long?bits,long?field)將field指定的格式標(biāo)志位置為bits,返回舊的格式標(biāo)志。
?long?unsetf(long?bits)?清除bits指定的格式標(biāo)志位,返回舊的格式標(biāo)志。
long?fill(char?c)??設(shè)置填充字符,缺省條件下是空格。
??char?fill(?)??返回當(dāng)前填充字符。
int?precision(int?val)??設(shè)置精確度為val,控制輸出浮點(diǎn)數(shù)的有效位,返回舊值。
int?precision(?)返回舊的精確度值。
int?width(int?val)??????設(shè)置顯示數(shù)據(jù)的寬度(域?qū)?,返回舊的域?qū)挕?/span>
int?width(?)??只返回當(dāng)前域?qū)?#xff0c;缺省寬度為0。這時(shí)插入操作能按表示數(shù)據(jù)的最小寬度顯示數(shù)據(jù)。?


預(yù)定義的操縱算子
????使用成員函數(shù)控制格式化輸入輸出時(shí),每個(gè)函數(shù)調(diào)用需要寫(xiě)一條語(yǔ)句,尤其是它不能用在插入或提取運(yùn)算符的表達(dá)式中,而使用操縱算子,則可以在插入和提取運(yùn)算符的表達(dá)式中控制格式化輸入和輸出。在程序中使用操縱算字必須嵌入頭文件
iomanip.h

dec十進(jìn)制的輸入輸出
?hex十六進(jìn)制的輸入輸出
?oct??八進(jìn)制的輸入輸出
ws??提取空白字符
?ends??輸出一個(gè)nul字符
endl??輸出一個(gè)換行字符,同時(shí)刷新流
flush刷新流
resetiosflags(long)請(qǐng)除特定的格式標(biāo)志位
setiosflags(long)?設(shè)置特定的格式標(biāo)志位
setfill(char)設(shè)置填充字符
setprecision(int)設(shè)置輸出浮點(diǎn)數(shù)的精確度
?setw(int)設(shè)置域?qū)捀袷阶兞?/span>

其它流函數(shù)

錯(cuò)誤處理
????在對(duì)一個(gè)流對(duì)象進(jìn)行I/O操作時(shí),可能會(huì)產(chǎn)生錯(cuò)誤。當(dāng)錯(cuò)誤發(fā)生時(shí),錯(cuò)誤的性質(zhì)被記錄在ios類(lèi)的一個(gè)數(shù)據(jù)成員中。
ios類(lèi)中定義的描述錯(cuò)誤狀態(tài)的常量:

?

goodbit?沒(méi)有錯(cuò)誤,正常狀態(tài) ?eofbit?到達(dá)流的結(jié)尾???
failbitI/O操作失敗,清除狀態(tài)字后,可以對(duì)流繼續(xù)進(jìn)行操作。
badbit試圖進(jìn)行非法操作,清除狀態(tài)字后,流可能還可以使用。
hardfail致命錯(cuò)誤,不可恢復(fù)的錯(cuò)誤。


ostream類(lèi)的成員函數(shù)
流的其它成員函數(shù)可以從流中讀取字符或字符串,對(duì)流進(jìn)行無(wú)格式化的輸入?輸出操作,以及直接控制對(duì)流的I/O操作。

?

返回類(lèi)型ios類(lèi)的成員描      述
ostream*tie(ostream*)???將當(dāng)前流與指定的輸出流連接起來(lái)。每當(dāng)需要?讀取當(dāng)前流時(shí),連接的流會(huì)自動(dòng)刷新。C++流庫(kù)已用cin.tie(cout)將輸入流與輸出流連接起來(lái)。要取消與輸出流的連接可采用is.tie(0)
ostream*tie(?)返回指向連接流的指針


?

返回類(lèi)型ostream類(lèi)的成員描      述
ostream&put(char?ch)向流中輸出一個(gè)字符ch,不進(jìn)行任何轉(zhuǎn)換
ostream&write(char*,int)向流中輸出指定長(zhǎng)度的字符串,不進(jìn)行轉(zhuǎn)換
ostream&?flush(?)刷新流,輸出所有緩沖的但還未輸出的數(shù)據(jù)
ostream&seekp(streampos)移動(dòng)流的當(dāng)前指針到給定的絕對(duì)位置
ostream&seekp(sereamoff,seek_dir)流的當(dāng)前指針類(lèi)似與文件的當(dāng)前指針
streamposteelp(?)返回流的當(dāng)前指針的絕對(duì)位置

istream類(lèi)的成員函數(shù)

?

返回類(lèi)型istream類(lèi)的成員描        述
intget(?)讀取并返回一個(gè)字符
istream&get(char&c)讀取字符并存入c中
istream&get(char*ptr,int?len,char?delim='')讀取指定的字符到緩沖區(qū)中,直到遇到指定的分界符為止,分界符不填入緩沖區(qū)。
istream&getline(char*ptr,int?len,char?delim='')與get(char*ptr,int?len,chardelim?='')?類(lèi)似,但將分界符填入緩沖區(qū)。
istream&?putback(?)將最近讀取的字符放回流中
istream&read(char*,int)讀取規(guī)定長(zhǎng)度的字符串到緩沖區(qū)中
intpeek(?)?返回流中下一個(gè)字符,但不移動(dòng)文件指針
istream&seekg(streampos)移動(dòng)當(dāng)前指針到一絕對(duì)地址
istream&?seekg(streampos,seek_dir)移動(dòng)當(dāng)前指針到一相對(duì)地址
streampostellg(?)返回當(dāng)前指針
istream&ignore(int?n=1,delim=EOF)跳過(guò)流中幾個(gè)字符,或直到遇到指定的分界符為止

關(guān)于::符號(hào)的探討

  仔細(xì)查找了一下ios類(lèi)控制符的源文件,可以追溯到頭文件ios_base.h,其中包含了很多控制流的變量或方法,而這些成員都被包圍在了命名空間std中,代碼如下所示。因此,流控制符是std命名空間中的靜態(tài)常量,::則代表了這些常量來(lái)自于std這個(gè)命名空間。

1 // 27.4.2.1.2 Type ios_base::fmtflags 2 /** 3 * @brief This is a bitmask type. 4 * 5 * @c "_Ios_Fmtflags" is implementation-defined, but it is valid to 6 * perform bitwise operations on these values and expect the Right 7 * Thing to happen. Defined objects of type fmtflags are: 8 * - boolalpha 9 * - dec 10 * - fixed 11 * - hex 12 * - internal 13 * - left 14 * - oct 15 * - right 16 * - scientific 17 * - showbase 18 * - showpoint 19 * - showpos 20 * - skipws 21 * - unitbuf 22 * - uppercase 23 * - adjustfield 24 * - basefield 25 * - floatfield 26 */ 27 typedef _Ios_Fmtflags fmtflags; 28 29 /// Insert/extract @c bool in alphabetic rather than numeric format. 30 static const fmtflags boolalpha = fmtflags(__ios_flags::_S_boolalpha); 31 32 /// Converts integer input or generates integer output in decimal base. 33 static const fmtflags dec = fmtflags(__ios_flags::_S_dec); 34 35 /// Generate floating-point output in fixed-point notation. 36 static const fmtflags fixed = fmtflags(__ios_flags::_S_fixed); 37 38 /// Converts integer input or generates integer output in hexadecimal base. 39 static const fmtflags hex = fmtflags(__ios_flags::_S_hex); 40 41 /// Adds fill characters at a designated internal point in certain 42 /// generated output, or identical to @c right if no such point is 43 /// designated. 44 static const fmtflags internal = fmtflags(__ios_flags::_S_internal); 45 46 /// Adds fill characters on the right (final positions) of certain 47 /// generated output. (I.e., the thing you print is flush left.) 48 static const fmtflags left = fmtflags(__ios_flags::_S_left); 49 50 /// Converts integer input or generates integer output in octal base. 51 static const fmtflags oct = fmtflags(__ios_flags::_S_oct); 52 53 /// Adds fill characters on the left (initial positions) of certain 54 /// generated output. (I.e., the thing you print is flush right.) 55 static const fmtflags right = fmtflags(__ios_flags::_S_right); 56 57 /// Generates floating-point output in scientific notation. 58 static const fmtflags scientific = fmtflags(__ios_flags::_S_scientific); 59 60 /// Generates a prefix indicating the numeric base of generated integer 61 /// output. 62 static const fmtflags showbase = fmtflags(__ios_flags::_S_showbase); 63 64 /// Generates a decimal-point character unconditionally in generated 65 /// floating-point output. 66 static const fmtflags showpoint = fmtflags(__ios_flags::_S_showpoint); 67 68 /// Generates a + sign in non-negative generated numeric output. 69 static const fmtflags showpos = fmtflags(__ios_flags::_S_showpos); 70 71 /// Skips leading white space before certain input operations. 72 static const fmtflags skipws = fmtflags(__ios_flags::_S_skipws); 73 74 /// Flushes output after each output operation. 75 static const fmtflags unitbuf = fmtflags(__ios_flags::_S_unitbuf); 76 77 /// Replaces certain lowercase letters with their uppercase equivalents 78 /// in generated output. 79 static const fmtflags uppercase = fmtflags(__ios_flags::_S_uppercase); 80 81 /// A mask of left|right|internal. Useful for the 2-arg form of @c setf. 82 static const fmtflags adjustfield = fmtflags(__ios_flags::_S_adjustfield); 83 84 /// A mask of dec|oct|hex. Useful for the 2-arg form of @c setf. 85 static const fmtflags basefield = fmtflags(__ios_flags::_S_basefield); 86 87 /// A mask of scientific|fixed. Useful for the 2-arg form of @c setf. 88 static const fmtflags floatfield = fmtflags(__ios_flags::_S_floatfield);

?

?

總結(jié)

以上是生活随笔為你收集整理的C++流操纵算子(格式控制)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。