[20050120]
1. character conversion
1.1 Related Classes
Lexical_cast
Bad_lexical_cast
1.2 definition file: # include <boost/lexical_cast.hpp>
1.3 features:
Used for type conversion. Conversion between strings and values
1.4 example:
Eclipse 3.0, gun g ++, and WINXP passed the test.
# Include <iostream>
# Include <boost/lexical_cast.hpp>
Using namespace STD;
Main ()
{
Int I = 100;
Char x pstr = "10101 ";
Cout <"before convert I is:" <I <Endl;
I = boost: lexical_cast <int> (pstr );
Cout <"after convert I is:" <I <Endl;
Return 0;
}
// Print: Before convert I is: 100
// After convert I is: 10101
1.5 others:
The exception handling for conversion is the same as that for general exception handling.
For example, try
{
// Convert
}
Catch (bad_lexical_cast &)
{
// Handle exceptions
}
2. format the string
2.1 related classes
Format
2.2 definition file: # include <boost/format. HPP>
2.3 features:
Construct a formatted string, some of which are to be determined and can be written as parameters.
2.4 example:
Eclipse 3.0, gun g ++, and WINXP passed the test.
# Include <iostream>
# Include <boost/format. HPP>
# Include <string>
Using namespace STD;
Using boost: format;
Using boost: IO: group;
Int main ()
{
Format FMT ("% 1% % 2% % 3% % 4% % 5% % 6%/N ");
// Replace the parameter with % 1%. 1 indicates the first parameter.
String STR;
Int I = 100;
Cout <FMT % 'G' % 'A' % 'l' % 'P' % 'H' % 'y' <Endl;
// % Operator press-in Parameter
String s_tmp = boost: STR (FMT );
// STR () free function, fomat-> string
// You can also use format. STR ();
Cout <s_tmp;
Return 0;
}
2.5 format description:
Use of "$": followed by the printf format Controller
"|" Usage: simplifies the control operator of the printf format
1. No Parameter order:
Format ("% 5d % 4 S % 5f"); // Add the printf format controller directly after %, and input parameters in sequence.
Format ("% | 5 | % | 4 | % | 5 |); // The effect is the same as the preceding one. The control symbols such as D, S, and F are automatically determined.
2. Parameter order:
Format ("% 1% % 2% % 1%");
Format ("% 1 $ 5d % 2 $5 S % 1 $ 5d );
Format ("% | 1 $5 | % | 2 $5 | % | 1 $5 |); // The effect is the same as above. The control symbols such as D, S, and F are automatically determined.
3. Specific format:
[N$] [Flags] [Width] [. Precision]Type-Char
-
Flag |
Meaning |
Effect on internal stream |
'-' |
Left alignment |
N/A (applied later on the string) |
'=' |
Centered alignment |
N/A (applied later on the string) -Note: added feature, not in printf- |
'_' |
Internal alignment |
Sets internal alignment -Note: added feature, not in printf- |
'+' |
Show sign even for positive numbers |
SetsShowpos |
'#' |
Show numerical base, and decimal point |
SetsShowbaseAndShowpoint |
'0' |
Pad with 0's (inserted after sign or base indicator) |
If not left-aligned, CILSSetfill ('0 ')And SetsInternal Extra actions are taken after Stream Conversion to handle user-defined output. |
'' |
If the string does not begin+Or-, InsertSpaceBefore the converted string |
N/A (applied later on the string) Different to printf's behaviour: it is not affected by internal alignment |
-
Type-Char |
Meaning |
Effect on stream |
P or X |
Hexadecimal output |
SetsHEX |
O |
Octal output |
SetsOct |
E |
Scientific float format |
Sets floatfield bitsScientific |
F |
Fixed float format |
Sets floatfield bitsFixed |
G |
General-default-float format |
UnsetAll floatfield bits |
X, EOrG |
Same effect as their lowercase counterparts, but using uppercase letters for number outputs. (exponents, hex digits ,..) |
Same Effects'X','E', Or'G',Plus Uppercase |
D, IOrU |
DecimalType output |
Sets basefield bitsDec |
SOrS |
String output |
PrecisionSpecification is unset, and its value goes to an internal field for later 'truncation'. (SeePrecisionExplicit) |
COrC |
1-character output |
Only the first character of the conversion string is used. |
% |
Print the character% |
N/ |