📄 for_5394.htm
字号:
<TR VALIGN=top><TD><SAMP>showpos</SAMP><BR></TD><TD> </TD><TD>Generates a <SAMP>+</SAMP> sign in non-negative generated numeric output<BR></TD><TD><SAMP>+</SAMP><BR></TD><TD>0<BR></TD></TR><TR VALIGN=top><TD><SAMP>showpoint</SAMP><BR></TD><TD> </TD><TD>Always generates a decimal-point in generated floating-point output<BR></TD><TD><SAMP>#</SAMP><BR></TD><TD>0<BR></TD></TR><TR VALIGN=top><TD><SAMP>showbase</SAMP><BR></TD><TD> </TD><TD>Generates a prefix indicating the numeric base of a generated integer output<BR></TD><TD> </TD><TD>0<BR></TD></TR><TR VALIGN=top><TD><SAMP>skipws</SAMP><BR></TD><TD> </TD><TD>Skips leading white space before certain input operations<BR></TD><TD> </TD><TD>1<BR></TD></TR><TR VALIGN=top><TD><SAMP>unitbuf</SAMP><BR></TD><TD> </TD><TD>Flushes output after each formatting operation<BR></TD><TD> </TD><TD>0<BR></TD></TR><TR VALIGN=top><TD><SAMP>uppercase</SAMP> <BR></TD><TD> </TD><TD>Replaces certain lowercase letters with their uppercase equivalents in generated output<BR></TD><TD><SAMP>%X</SAMP><BR><SAMP>%E</SAMP><BR><SAMP>%G</SAMP><BR></TD><TD>0<BR></TD></TR></TABLE></CENTER><P>The effect of setting a format parameter is usually permanent; that is, the parameter setting is in effect until the setting is explicitly changed. The only exception to this rule is the field width. The width is automatically reset to its default value <SAMP>0</SAMP> after each input or output operation that uses the field width. Here is an example:</FN><PRE>int i; char* s[11];cin >> setw(10) >> i >> s; \\1cout << setw(10) <<i << s; \\2</PRE><TABLE CELLPADDING="3"><TR VALIGN="top"><TD>//1</TD><TD>Extracting an integer is independent of the specified field width. The extractor for integers always reads as many digits as belong to the integer. As extraction of integers does not use the field width setting, the field width of <SAMP>10</SAMP> is still in effect when a character sequence is subsequently extracted. Only 10 characters will be extracted in this case. After the extraction, the field width is reset to <SAMP>0</SAMP>.</TD></TR><TR VALIGN="top"><TD>//2</TD><TD>The inserter for integers uses the specified field width and fills the field with padding characters if necessary. After the insertion, it resets the field width to <SAMP>0</SAMP>. Hence, the subsequent insertion of the string will not fill the field with padding characters for a string with less than 10 characters. </TD></TR></TABLE><P><I>Please note:</I> With the exception of the field width, all format parameter settings are permanent. The field width parameter is reset after each use.</P><P>The following code sample shows how you can control formatting by using some of the parameters:</P><PRE>#include <iostream>using namespace ::std;// _ios_base::fmtflags original_flags = cout.flags(); \\1cout<< 812<<'|';cout.setf(ios_base::left,ios_base::adjustfield); \\2cout.width(10); \\3cout<< 813 << 815 << '\n';cout.unsetf(ios_base::adjustfield); \\4cout.precision(2);cout.setf(ios_base::uppercase|ios_base::scientific); \\5cout << 831.0 << ' ' << 8e2;cout.flags(original_flags); \\6</PRE><TABLE CELLPADDING="3"><TR VALIGN="top"><TD>//1</TD><TD>Store the current format flag setting, in order to restore it later on.</TD></TR><TR VALIGN="top"><TD>//2</TD><TD>Change the adjustment from the default setting <SAMP>right</SAMP> to <SAMP>left</SAMP>.</TD></TR><TR VALIGN="top"><TD>//3</TD><TD>Set the field width from its default <SAMP>0</SAMP> to <SAMP>10</SAMP>. A field width of <SAMP>0</SAMP> means that no padding characters are inserted, and this is the default behavior of all insertions.</TD></TR><TR VALIGN="top"><TD>//4</TD><TD>Clear the adjustment flags.</TD></TR><TR VALIGN="top"><TD>//5</TD><TD>Change the precision for floating-point values from its default <SAMP>6</SAMP> to <SAMP>2</SAMP>, and set yet another couple of format flags that affect floating-point values.</TD></TR><TR VALIGN="top"><TD>//6</TD><TD>Restore the original flags.</TD></TR></TABLE><P>The output is:</P><PRE>812|813 8158.31E+02 8.00E+02</PRE></LI></UL><A NAME="2.3.3.2"><H4>2.3.3.2 Manipulators</H4></A><P>Format control requires calling a stream's member functions. Each such call interrupts the respective shift expression. But what if you need to change formats within a shift expression? This is possible in iostreams. Instead of writing:</P><PRE>cout<< 812 << '|';cout.setf(ios_base::left,ios_base::adjustfield);cout.width(10);cout<< 813 << 815 << '\n';</PRE><P>you can write:</P><PRE>cout<< 812 << '|' << left << setw(10) << 813 << 815 << endl;</PRE><P>In this example, objects like <SAMP>left</SAMP>, <SAMP>setw</SAMP>, and <SAMP>endl</SAMP> are called <I>manipulators</I>. A manipulator is an object of a certain type; let's call the type <SAMP>manip</SAMP> for the time being. There are overloaded versions of<SAMP> basic_istream <charT,traits>:: operator>>()</SAMP> and <SAMP>basic_ostream <charT,traits>:: operator<<() </SAMP>for type <SAMP>manip</SAMP>. Hence a manipulator can be extracted from or inserted into a stream together with other objects that have the shift operators defined. (<A HREF="man_6665.htm">Section 2.8</A> explains in greater detail how manipulators work and how you can implement your own manipulators.)</P><P>The effect of a manipulator need not be an actual input to or output from the stream. Most manipulators set just one of the above described format flags, or do some other kind of stream manipulation. For example, an expression like:</P><PRE><SAMP>cout << left; </SAMP></PRE><P>is equivalent to: </P><PRE><SAMP>cout.setf (ios_base::left, ios_base::adjustfield);.</SAMP></PRE><P>Nothing is inserted into the stream. The only effect is that the format flag for adjusting the output to the left is set.</P><P>On the other hand, the manipulator <SAMP>endl</SAMP> inserts the newline character to the stream, and flushes to the underlying stream buffer. The expression:</P><PRE><SAMP>cout << endl;</SAMP> </PRE><P>is equivalent to: </P><PRE><SAMP>cout << '\n'; cout.flush();</SAMP></PRE><P>Some manipulators take arguments, like <SAMP>setw(int)</SAMP>. The<SAMP> setw </SAMP>manipulator sets the field width. The expression:</P><PRE><SAMP>cout << setw(10);</SAMP></PRE><P>is equivalent to: </P><PRE><SAMP>cout.width(10);</SAMP></PRE><P>In general, you can think of a manipulator as an object you can insert into or extract from a stream, in order to manipulate that stream.</P><P>Some manipulators can be applied only to output streams, others only to input streams. Most manipulators change format bits only in one of the stream base classes, <B><I><B><I>ios_base</I></B></B></I> or <B><I><B><I>basic_ios<charT,traits></I></B></B></I>. These can be applied to input and output streams.</P><P>Table 4 below gives an overview of all manipulators defined by iostreams. The first column, <B>Manipulator</B>, lists its name. All manipulators are classes defined in the namespace <SAMP>::std</SAMP>. The second column, <B>Use</B>, indicates whether the manipulator is intended to be used with istreams (<SAMP>i</SAMP>), ostreams (<SAMP>o</SAMP>), or both (<SAMP>io</SAMP>). The third column, <B>Effect</B>, summarizes the effect of the manipulator. The last column, <B>Equivalent</B>, lists the corresponding call to the stream's member function.</P><P>Note that the second column indicates only the <I>intended</I> use of a manipulator. In many cases, it is possible to apply an output manipulator to an input stream, and vice versa. Generally, this kind of non-intended manipulation is harmless in that it has no effect. For instance, if you apply the output manipulator <SAMP>showpoint</SAMP> to an input stream, the manipulation will simply be ignored. However, if you use an output manipulator on a bidirectional stream during input, the manipulation will affect not current input operations, but subsequent output operations.</P><H4>Table 4: Manipulators</H4><CENTER><TABLE BORDER CELLSPACING=3 CELLPADDING=3><TR VALIGN=top><TD><B>Manipulator</B><BR></TD><TD><B>Use</B><BR></TD><TD><B>Effect</B><BR></TD><TD><B>Equivalent</B><BR></TD></TR><TR VALIGN=top><TD><SAMP>boolalpha</SAMP><BR></TD><TD><SAMP>io</SAMP><BR></TD><TD>Puts <SAMP>bool</SAMP> values in alphabetic format<BR></TD><TD><SAMP>io.setf(ios_base::boolalpha)</SAMP><BR></TD></TR><TR VALIGN=top><TD><SAMP>dec</SAMP><BR></TD><TD><SAMP>io</SAMP><BR></TD><TD>Converts integers to/from decimal notation<BR></TD><TD><SAMP>io.setf(ios_base::dec, ios_base::basefield)</SAMP><BR></TD></TR><TR VALIGN=top><TD><SAMP>endl</SAMP><BR></TD><TD><SAMP>o</SAMP><BR></TD><TD>Inserts newline and flushes buffer<BR></TD><TD><SAMP>o.put(o.widen('\n'));</SAMP><BR><SAMP>o.flush()</SAMP><BR></TD></TR><TR VALIGN=top><TD><SAMP>ends</SAMP><BR></TD><TD><SAMP>o</SAMP><BR></TD><TD>Inserts end of string character<BR></TD><TD><SAMP>o.put(o.widen('\0'))</SAMP><BR></TD></TR><TR VALIGN=top><TD><SAMP>fixed</SAMP><BR></TD><TD><SAMP>o</SAMP><BR></TD><TD>Puts floating point values in fixed-point notation<BR></TD><TD><SAMP>o.setf(ios_base::fixed, ios_base::floatfield)</SAMP><BR></TD></TR><TR VALIGN=top><TD><SAMP>flush</SAMP><BR></TD><TD><SAMP>o</SAMP><BR></TD><TD>Flushes stream buffer<BR></TD><TD><SAMP>o.flush()</SAMP><BR></TD></TR><TR VALIGN=top><TD><SAMP>hex</SAMP><BR></TD><TD><SAMP>io</SAMP><BR></TD><TD>Converts integers to/from hexadecimal notation<BR></TD><TD><SAMP>io.setf(ios_base::hex, ios_base::basefield)</SAMP><BR></TD></TR><TR VALIGN=top><TD><SAMP>internal</SAMP><BR></TD><TD><SAMP>o</SAMP><BR></TD><TD>Adds fill characters at a designated internal point<BR></TD><TD><SAMP>o.setf(ios_base::internal, ios_base::adjustfield)</SAMP><BR></TD></TR><TR VALIGN=top><TD><SAMP>left</SAMP><BR></TD><TD><SAMP>o</SAMP><BR></TD><TD>Adds fill characters for adjustment to the left<BR></TD><TD><SAMP>o.setf(ios_base::left, ios_base::adjustfield)</SAMP><BR></TD></TR><TR VALIGN=top><TD><SAMP>noboolalpha</SAMP><BR></TD><TD><SAMP>io</SAMP><BR></TD><TD>Resets the above<BR></TD><TD><SAMP>io.unsetf(ios_base::boolalpha)</SAMP><BR></TD></TR><TR VALIGN=top><TD><SAMP>noshowbase</SAMP><BR></TD><TD><SAMP>o</SAMP><BR></TD><TD>Resets the above<BR></TD><TD><SAMP>o.unsetf (ios_base::showbase)</SAMP><BR></TD></TR><TR VALIGN=top><TD><SAMP>noshowpoint</SAMP><BR></TD><TD><SAMP>o</SAMP><BR></TD><TD>Resets the above<BR></TD><TD><SAMP>o.unsetf (ios_base::showpoint)</SAMP><BR></TD></TR><TR VALIGN=top><TD><SAMP>noshowpos</SAMP><BR></TD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -