本文整理汇总了C++中Istream::imbue方法的典型用法代码示例。如果您正苦于以下问题:C++ Istream::imbue方法的具体用法?C++ Istream::imbue怎么用?C++ Istream::imbue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Istream
的用法示例。
在下文中一共展示了Istream::imbue方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: test_ctor
void test_ctor (const charT*, const Traits*,
const char *cname, const char *tname)
{
typedef std::basic_istream<charT, Traits> Istream;
typedef typename Istream::sentry Sentry;
memfun_info (__LINE__, cname, tname, "sentry (%{$ISTREAM}&, bool)");
const charT cbuf[] = { 'a', 'b', 'c', 'd', 'e', ' ', 'f', '\0' };
const std::ios_base::iostate states[] = {
std::ios_base::badbit,
std::ios_base::eofbit,
std::ios_base::failbit,
std::ios_base::goodbit,
std::ios_base::badbit | std::ios_base::eofbit,
std::ios_base::badbit | std::ios_base::failbit,
std::ios_base::eofbit | std::ios_base::failbit,
std::ios_base::badbit | std::ios_base::eofbit | std::ios_base::failbit
};
//////////////////////////////////////////////////////////////
// exercise 27.6.1.1.2, p1:
// - is.good() is true
// - is.tie() is not null
// = the function calls is.tie().flush()
unsigned iter = 0; // iteration counter
for (unsigned i = 0; i != sizeof states / sizeof *states; ++i) {
for (unsigned j = 0; j != 2; ++j /* noskipws */) {
Streambuf<charT, Traits>
sb (cbuf, cbuf + sizeof cbuf / sizeof *cbuf);
Istream is (&sb);
// flush() is called iff
// all of the following conditions hold
const bool flush_called = is.good () && 0 != is.tie ();
const Sentry guard (is, 0 != j);
_RWSTD_UNUSED (guard);
rw_assert (flush_called == sb.nsyncs_, 0, __LINE__,
"%u. basic_istream<%s, %s>::sentry::sentry"
"(basic_istream &is, bool noskipws = %d); "
"expected to call is.flush () %d times, got %d"
"initial is.state () = %{Is}, is.flags() & "
"ios::skipws = %d",
iter, cname, tname, 0 != j, flush_called, sb.nsyncs_,
states [i], is.flags () & std::ios_base::skipws);
++iter;
}
}
//////////////////////////////////////////////////////////////
// exercise 27.6.1.1.2, p1:
// - is.good() is true
// - noskipws is zero
// - is.flags() & ios_base::skipws
// = the function extracts and discards each character as long
// as the next available input character c is a whitespace
// character.
for (unsigned i = 0; i != sizeof states / sizeof *states; ++i) {
for (unsigned j = 0; j != 2; ++j /* noskipws */) {
for (unsigned k = 0; k != 2; ++k /* ios_base::skipws */) {
for (charT wc = charT ('a'); wc != charT ('c'); ++wc) {
const Ctype<charT> ctp (1, wc);
Streambuf<charT, Traits>
sb (cbuf, cbuf + sizeof cbuf / sizeof *cbuf);
Istream is (&sb);
is.setstate (states [i]);
if (k)
is.setf (std::ios_base::skipws);
else
is.unsetf (std::ios_base::skipws);
const std::locale loc =
is.imbue (std::locale (is.getloc (), &ctp));
// imbue the previous locale into the stream
// buffer to verify that the sentry ctor uses
// the locale imbued in the stream object and
// not the one in the stream buffer
sb.pubimbue (loc);
// a whitespace character is extracted iff
// all of the following conditions hold
const bool extract =
is.good ()
&& 0 == j
&& is.flags () & std::ios_base::skipws
//.........这里部分代码省略.........
示例2: test_ok
void test_ok (const charT*, const Traits*,
const char *cname, const char *tname)
{
typedef std::basic_istream<charT, Traits> Istream;
typedef typename Istream::sentry Sentry;
memfun_info (__LINE__, cname, tname, "operator bool () const");
const charT cbuf[] = { 'a', 'b', 'c', 'd', 'e', ' ', 'f', '\0' };
const std::ios_base::iostate states[] = {
std::ios_base::badbit,
std::ios_base::eofbit,
std::ios_base::failbit,
std::ios_base::goodbit,
std::ios_base::badbit | std::ios_base::eofbit,
std::ios_base::badbit | std::ios_base::failbit,
std::ios_base::eofbit | std::ios_base::failbit,
std::ios_base::badbit | std::ios_base::eofbit | std::ios_base::failbit
};
//////////////////////////////////////////////////////////////
// exercise 27.6.1.1.2, p5:
// - is.good() is true
// - noskipws is zero
// - is.flags() & ios_base::skipws
// - the function extracts and discards each character as long
// as the next available input character c is a whitespace
// character
// = if, after any preparation is completed, is.good() is true,
// ok_ != false otherwise, ok_ == false.
unsigned iter = 0; // iteration counter
for (unsigned i = 0; i != sizeof states / sizeof *states; ++i) {
for (unsigned j = 0; j != 2; ++j /* noskipws */) {
for (unsigned k = 0; k != 2; ++k /* ios_base::skipws */) {
for (charT wc = charT ('a'); wc != charT ('c'); ++wc) {
const Ctype<charT> ctp (1, wc);
Streambuf<charT, Traits>
sb (cbuf, cbuf + sizeof cbuf / sizeof *cbuf);
Istream is (&sb);
is.setstate (states [i]);
if (k)
is.setf (std::ios_base::skipws);
else
is.unsetf (std::ios_base::skipws);
const std::locale loc =
is.imbue (std::locale (is.getloc (), &ctp));
// imbue the previous locale into the stream
// buffer to verify that the sentry ctor uses
// the locale imbued in the stream object and
// not the one in the stream buffer
sb.pubimbue (loc);
const Sentry guard (is, 0 != j);
_RWSTD_UNUSED (guard);
const bool success =
is.good () && guard
|| !is.good () && !guard;
rw_assert (success, 0, __LINE__,
"%u. %{$SENTRY}"
"(%{$ISTREAM} &is, bool noskipws "
"= %d).operator bool() == %d; initial "
"is.state() = %{Is}, is.flags() & "
"ios::skipws = %d",
iter, j, is.good (),
states [i], k);
++iter;
}
}
}
}
}
示例3: if
//.........这里部分代码省略.........
if (-1 == exceptions) {
// get the initial stream object's exceptions
exceptions = is.exceptions ();
}
else {
// unmask the stream objects exceptions (must be done
// before calling setstate() to prevent the latter from
// throwing ios::failure)
is.exceptions (std::ios::iostate (exceptions));
}
if (-1 == init_state) {
// get the initial stream object's state
init_state = is.rdstate ();
}
else {
// set the stream object's initial state
#ifndef _RWSTD_NO_EXCEPTIONS
try {
is.setstate (std::ios::iostate (init_state));
}
catch (...) {
// ignore exceptions
}
#else // if defined ( _RWSTD_NO_EXCEPTIONS)
is.setstate (std::ios::iostate (init_state));
#endif // _RWSTD_NO_EXCEPTIONS
}
// construct a locale object that treats only the specified `white'
// characters as whitespace (all others are treated normally)
const std::locale loc =
is.imbue (make_locale ((CharT*)0, (Traits*)0, locale_data));
// imbue the previous locale into the stream buffer to verify that
// the ws manipulator uses the locale imbued in the stream object
// and not the one in the stream buffer
sb.pubimbue (loc);
// initialize the variable to the initial value to detect
// the extractor setting it when it's not supposed to
ArithmeticType value = init_value;
// format the FUNCALL environment variable w/o writing out any output
rw_fprintf (0,
"%{$FUNCALL!:@}",
"%{$CLASS}(%{*Ac}).operator>>(%s& = %{@}): "
"initial flags() = %{If}, rdstate() = %{Is}, "
"exceptions() = %{Is}, whitespace = %{#s}, numpunct = { "
".dp=%{#c}, .ts=%{#c}, .grp=%{#s}, .fn=%{#s}, .tn=%{#s} }",
int (sizeof *cbuf), cbuf, aname, valfmt, init_value,
flags, init_state,
exceptions, locale_data.whitespace,
locale_data.decimal_point, locale_data.thousands_sep,
locale_data.grouping, locale_data.falsename,
locale_data.truename);
#ifndef _RWSTD_NO_EXCEPTIONS
int caught = 0;
try {
is >> value;
}
catch (Exception&) {