Sketch
Programming often encounters the need to format the data used, such as generating strings to record logs, generating complete data sent to the network, and so on.
It may be necessary to format int, string, char, etc. Common methods include snprintf, std::string, std::stringstream, boost::format, etc.
snprintf supports c language, which may be the most efficient of these methods. The efficiency comparison between them can be referred to. Comparing the performance of a sequence of several generators .
But in situations where efficiency requirements are not very stringent, it may be more convenient to use other methods. Here we introduce boost::format.
boost::format uses
If you are already using the boost library, formatting with format will be a very simple matter.
As mentioned earlier, format is not as fast as snprintf, but it is type-safe. It uses streams to build output, so there is no memory overflow.
format has two usage styles, one similar to printf and the other using placeholders, similar to c #.
Floating-point format
Code directly:
const double almostpi = 22.0 / 7.0; // Printf printf("Pi is %f\n", almostpi); // Boost format with printf syntax boost::format printf_formatting("Pi is %f\n"); std::cout << printf_formatting % almostpi; // Boost format with positional syntax boost::format position_formatting("Pi is %1%\n"); std::cout << position_formatting % almostpi; // Output: // Pi is 3.142857 // Pi is 3.142857 // Pi is 3.14286
Obviously, format is similar to printf in that it uses% f to format a floating point number with the default precision of 6 decimal places.
This conclusion can be validated with larger data, such as:
const double almostpi = 22.0 / 7.0; const double distancetosun = 149600000000.0; // meters const double earthorbitlength = 2 * almostpi * distancetosun; // Printf printf("Earth orbit distance is %f meters\n", earthorbitlength); // Boost format with printf syntax boost::format printf_formatting("Earth orbit distance is %f meters\n"); std::cout << printf_formatting % earthorbitlength; // Boost format with positional syntax boost::format position_formatting("Earth orbit distance is %1% meters\n"); std::cout << position_formatting % earthorbitlength; // Output: // Earth orbit distance is 940342857142.857180 meters // Earth orbit distance is 940342857142.857180 meters // Earth orbit distance is 9.40343e+011 meters
When the% 1% placeholder is used, the scientific counting method (about 6-7 character lengths) is used when the value is large.
On Scientific Counting
Generally speaking, only when the placeholder is used and the value is large, will it be converted to scientific counting method. Such as:
boost::format scinotation("Do we use scientific? '%1%'\n");// % llu also shows scientific counting for (size_t n=0; n<15; ++n) { double value = 1 * pow(10, n) + 0.5; std::cout << scinotation % value; } // Output: // Do we use scientific? '1.5' // Do we use scientific? '10.5' // Do we use scientific? '100.5' // Do we use scientific? '1000.5' // Do we use scientific? '10000.5' // Do we use scientific? '100001' // Do we use scientific? '1e+006' // Do we use scientific? '1e+007' // Do we use scientific? '1e+008' // Do we use scientific? '1e+009' // Do we use scientific? '1e+010' // Do we use scientific? '1e+011' // Do we use scientific? '1e+012' // Do we use scientific? '1e+013' // Do we use scientific? '1e+014'
In addition, in practical use, if mismatched markers are used, such as% llu, while the actual value is double.
The% e display can be used to specify a scientific counting display.
Format control
The grammar of format control is:%[N$][flags][width][.precision]type-char. Such as:
// Printf printf("Pi is '%10.2f'\n", almostpi); // Boost format with printf syntax boost::format printf_formatting("Pi is '%10.2f'\n"); std::cout << printf_formatting % almostpi; // Boost format with positional syntax boost::format position_formatting("Pi is '%1$10.2f'\n"); std::cout << position_formatting % almostpi; // Output: // Pi is ' 3.14' // Pi is ' 3.14' // Pi is ' 3.14'
Common examples
Simple way to use, can directly output the desired content:
// Direct Output cout << boost::format("%s") % "this is what i want" << endl; // Combining string string s; s = str(boost::format("%s") % "this is what i want"); cout << s << endl; // Using formater boost::format fmt("%s"); fmt % "this is what i want"; string s = fmt.str(); cout << s << endl; // placeholder cout << boost::format("%1%") % "this is what i want" << endl;
exception handling
format exceptions throw exception information, so use try statement blocks to handle exceptions, otherwise the program will terminate by default. Such as:
try { cout << boost::format("%d%d") % 1 << endl; } catch (std::exception const &e) { cout << e.what() << endl; } catch (...) { cout << "format error" << endl; } // output // boost::too_few_args: format-string refered to more arguments than were passed
Reference material:
Boost::format (and wformat) examples – numbers: int, float, double
Comparing the performance of a sequence of several generators
Taste the format of boost