您的位置:首页 > 编程语言 > C语言/C++

STL之--输入输出流状态的相关函数操作

2016-10-06 18:26 260 查看
clear()函数

功能:更改cin的状态标识符,使之处于正确状态。

定义在
<ios>
头文件中,是basic_ios类的成员函数,以下是相关的源码:

// TEMPLATE CLASS basic_ios
template<class _Elem,class _Traits>
class basic_ios: public ios_base
{   // base class for basic_istream/basic_ostream
public:
typedef basic_ios<_Elem, _Traits> _Myt;
typedef basic_ostream<_Elem, _Traits> _Myos;
typedef basic_streambuf<_Elem, _Traits> _Mysb;
typedef ctype<_Elem> _Ctype;
typedef _Elem char_type;
typedef _Traits traits_type;
typedef typename _Traits::int_type int_type;
typedef typename _Traits::pos_type pos_type;
typedef typename _Traits::off_type off_type;

explicit __CLR_OR_THIS_CALL basic_ios(_Mysb *_Strbuf)
{   // construct from stream buffer pointer
init(_Strbuf);
}

virtual __CLR_OR_THIS_CALL ~basic_ios() _NOEXCEPT
{   // destroy the object
}

void __CLR_OR_THIS_CALL clear(iostate _State = goodbit,
bool _Reraise = false)
{   // set state, possibly reraise exception
ios_base::clear((iostate)(_Mystrbuf == 0
? (int)_State | (int)badbit : (int)_State), _Reraise);
}

void __CLR_OR_THIS_CALL clear(io_state _State)
{   // set state to _State
clear((iostate)_State);
}
//此处省去之后basic_ios的其他成员函数
}


ignore()函数

功能:ignore函数是输入流的成员函数,可用来读取并抛弃残留在流缓冲区中的包括‘\n’在内的字符。

定义在
<istream>
头文件中,用法如
cin.ignore(numeric_limits<int>::max, ‘\n’)
,这里用于读取近乎无限个字符,或者遇到了‘\n’,以下是摘取的ignore函数的源码:

_Myt& __CLR_OR_THIS_CALL
ignore(streamsize _Count = 1,int_type _Metadelim = _Traits::eof())
{   // ignore up to _Count characters, discarding delimiter
ios_base::iostate _State = ios_base::goodbit;
_Chcount = 0;
const sentry _Ok(*this, true);

if (_Ok && 0 < _Count)
{   // state okay, use facet to extract
_TRY_IO_BEGIN
for (; ; )
{   // get a metacharacter if more room in buffer
int_type _Meta;
if (_Count != INT_MAX && --_Count < 0)
break;  //buffer full, quit
else if (_Traits::eq_int_type(_Traits::eof(),
_Meta = _Myios::rdbuf()->sbumpc()))
{   // end of file, quit
_State |= ios_base::eofbit;
break;
}
else
{   // got a character, count it
++_Chcount;
if (_Meta == _Metadelim)
break;  // got a delimiter, quit
}
}
_CATCH_IO_END
}
_Myios::setstate(_State);
return (*this);
}


其中,
_Count
指被拋弃字符的最大数目,默认为1;
_Metadelim
指遇到此字符后ignore函数停止运行(注意:此字符将被拋弃),默认为EOF字符(即遇到文件尾)。

system()系列函数

1.system(“pause>nul”)

用于在输出时,等按下任意键后才会返回,且不显示”请按任意键继续”;

2.system(“pause”)

用于在输出时,等按下任意键后才会返回;

3.system(“cls”)

可以实现清屏操作
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  stl 输入输出 C++
相关文章推荐