Try boost format
Http://www.cnblogs.com/wuerping/archive/2005/04/21/142308.html
Overview
STD: string is a good stuff, but in actual use, basically encounter unpleasant things in every program: format the string. Me
Even for this reason, we can introduce platform-related MFC, ATL, and other heavyweight frameworks that do not need to be used in the project to easily format strings.
. I tried to extract the format function of ATL: cstring and use it. However, the underlying function of ATL: cstring calls a Windows unique function and cannot cross the platform.
Of course, with boost: format, we don't have to worry about it anymore. Boost: Format reloads the '%' operator. You can call the '%' operator multiple times
It is easy to format a string and implements the Formatting Function of string in ATL: cstring and C. Apart from the strange syntax at the beginning, the function is quite interesting.
Fen!
1. Boost: Format
Basic Syntax: boost: Format (format-string) % arg1 % arg2 %... % argn
The following example shows how to use boost: Format
// Method 1
Cout <boost: Format ("% s") % "output content" <Endl;
// Method 2
STD: String S;
S = STR (boost: Format ("% s") % "output content ");
Cout <S <Endl;
// Method 3
Boost: Format formater ("% s ");
Formater % "output content ";
STD: String S = formater. STR ();
Cout <S <Endl;
// Method 4
Cout <boost: Format ("% 1%") % boost: IO: group (Hex, showbase, 40) <Endl;
Ii. Boost: The actual instance used by format
Format Syntax: [N $] [flags] [width] [. Precision] type-Char
// ATL: cstring Style
Cout <boost: Format ("\ n % s"
"% 1 t decimal = [% d] \ n"
"% 1 t formatted decimal = [% 5D] \ n"
"% 1 t formatted in decimal format, prefill '0' = [% 05d] \ n"
"% 1 t hexadecimal = [% x] \ n"
"% 1 t octal = [% O] \ n"
"% 1 t floating point = [% F] \ n"
"% 1 t formatted floating point = [% 3.3f] \ n"
"% 1 t scientific COUNT = [% E] \ n"
) % "Example: \ n" % 15% 15% 15% 15% 15% 15.01% 15.01% <Endl;
// C #: String Style
Cout <boost: Format ("% 1%"
"% 1 t decimal = [% 2 $ D] \ n"
"% 1 t formatted decimal = [% 2 $ 5D] \ n"
"% 1 t formatted in decimal format, prefill '0' = [% 2 $ 05d] \ n"
"% 1 t hexadecimal = [% 2 $ X] \ n"
"% 1 t octal = [% 2 $ o] \ n"
"% 1 t floating point = [% 3 $ F] \ n"
"% 1 t formatted floating point = [% 3 $ 3.3f] \ n"
"% 1 t scientific COUNT = [% 3 $ E] \ n"
) % "Example: \ n" % 15% 15.01 <Endl;
Output result
/**//*
Example:
Decimal = [15]
Formatted decimal = [15]
Format in decimal format. Add '0' = [00015].
Hexadecimal = [F]
Octal = [17]
Floating Point = [15.010000]
Formatted floating point = [15.010]
Scientific COUNT = [1.501000e + 001]
*/
3. Boost: new format specifier of format
% {Nt}
When N is a positive number, N absolute tabs are inserted.
Cout <boost: Format ("[% 10 t]") <Endl;
% {NTX}
Use X as the fill character to replace the fill character of the current stream (usually a space by default)
Cout <boost: Format ("[% 10 t *]") <Endl;
Iv. Exception Handling
General Syntax:
Try
{
Cout <boost: Format ("% d") % 1 <Endl;
}
Catch (STD: exception const & E)
{
Cout <E. What () <Endl;
// Output content:
// Boost: too_few_args: format-string refered to more arguments than were passed
}
In the boost: format document, there is a way to handle exceptions, but I personally feel that it is not practical. The following is an example in this document.
// Boost: IO: all_error_bits Selects all errors
// Boost: IO: too_many_args_bit selects errors due to passing too into arguments.
// Boost: IO: too_few_args_bit selects errors due to asking for the srting result before all arguments are passed
Boost: Format my_fmt (const STD: string & f_string)
{
Using namespace boost: IO;
Format fmter (f_string );
Fmter. Exceptions (all_error_bits ^ (too_many_args_bit | too_few_args_bit ));
Return fmter;
}
Cout <my_fmt ("% 1% % 2% \ n") % 1% 2% 3% 5;
5. There are other functions, but I don't want to go into details if I feel that I am useless for the moment.