ios_base類
目錄
一,_Fmtfl 格式
二,width 格式
三,precision 格式
四,sync_with_stdio函數
ios_base類是輸入輸出流的一個基礎類。
The class?ios_base?is a multipurpose class that serves as the base class for all I/O stream classes.
一,_Fmtfl 格式
這個成員的每一位,都用來控制流的一個屬性
初始化:_Fmtfl ?= skipws | dec; 初始值是513
讀取:flags()
替換:flags(fmtflags _Newfmtflags)
加格式:setf(fmtflags _Newfmtflags)
在給定的mask內設置格式:setf(fmtflags _Newfmtflags, fmtflags _Mask)
相關庫函數
_NODISCARD fmtflags __CLR_OR_THIS_CALL flags() const {
return _Fmtfl;
}
fmtflags __CLR_OR_THIS_CALL flags(fmtflags _Newfmtflags) { // set format flags to argument
const fmtflags _Oldfmtflags = _Fmtfl;
_Fmtfl = _Newfmtflags & _Fmtmask;
return _Oldfmtflags;
}
fmtflags __CLR_OR_THIS_CALL setf(fmtflags _Newfmtflags) { // merge in format flags argument
const ios_base::fmtflags _Oldfmtflags = _Fmtfl;
_Fmtfl |= _Newfmtflags & _Fmtmask;
return _Oldfmtflags;
}
fmtflags __CLR_OR_THIS_CALL setf(
fmtflags _Newfmtflags, fmtflags _Mask) { // merge in format flags argument under mask argument
const ios_base::fmtflags _Oldfmtflags = _Fmtfl;
_Fmtfl = (_Oldfmtflags & ~_Mask) | (_Newfmtflags & _Mask & _Fmtmask);
return _Oldfmtflags;
}
void __CLR_OR_THIS_CALL unsetf(fmtflags _Mask) { // clear format flags under mask argument
_Fmtfl &= ~_Mask;
}
示例:
int main()
{
cout << cout.flags() << endl;
cout.setf(ios::unitbuf);
cout << cout.flags() << endl;
cout.unsetf(ios::unitbuf);
cout << cout.flags() << endl;
return 0;
}
輸出
513
515
513
其中ios::unitbuf是常數2
其他的 _Fmtfl 格式:
二,width 格式
width用來控制整數輸出的寬度,即占幾個字符
相關庫函數
_NODISCARD streamsize __CLR_OR_THIS_CALL width() const {
return _Wide;
}
streamsize __CLR_OR_THIS_CALL width(streamsize _Newwidth) { // set width to argument
const streamsize _Oldwidth = _Wide;
_Wide = _Newwidth;
return _Oldwidth;
}
示例:
int main()
{
for (int i = 0; i < 100; i++) {
if (i % 10 == 0)cout << endl;
cout.width(3);
cout << i;
}
return 0;
}
三,precision 格式
precision用來控制浮點數的輸出精度
相關庫函數
_NODISCARD streamsize __CLR_OR_THIS_CALL precision() const {
return _Prec;
}
streamsize __CLR_OR_THIS_CALL precision(streamsize _Newprecision) { // set precision to argument
const streamsize _Oldprecision = _Prec;
_Prec = _Newprecision;
return _Oldprecision;
}
示例:
int main()
{
cout.precision(3);
cout << 1.414213562;
return 0;
}
輸出:1.41
四,sync_with_stdio函數
Sets whether the standard C++ streams are synchronized to the standard C streams after each input/output operation.
相關庫函數
static bool __CLRCALL_OR_CDECL sync_with_stdio(
bool _Newsync = true) { // set C/C++ synchronization flag from argument
_BEGIN_LOCK(_LOCK_STREAM) // lock thread to ensure atomicity
const bool _Oldsync = _Sync;
_Sync = _Newsync;
return _Oldsync;
_END_LOCK()
}
其中_Sync是私有靜態成員
__PURE_APPDOMAIN_GLOBAL static bool _Sync;
這表明所有的流共享一個_Sync成員。
iOS
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。