Boost's Format Library

xiaoxiao2021-03-06  42

#include #include #include #include "boost / format.hpp" // 2 Custom namespaces, to bring in a few useful names: namespace myns_foroutput {using std :: cout; using std: ::} u;} namespace myns :: group ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ade SETW; Using std :: hex; using std :: div; // gcc-2.95 doesnt define the next ones // using st: showbase; // using st: ip; l // using st: remote; // Using std :: {using namespace myns_foroutput; using namespace myns_manips; std :: cout << Format ("% | 1 $ 1 |% ​​| 2 $ 3 |)")% "Hello"% 3 << STD :: Endl; // REORDERING: COUT << Format ("% 1%% 2%% 3%% 2%% 1% / N")% "o"% "oo"% "o"; // 'Simple 'Style. // Prints "O OO O OO O / N" COUT << Format ("(" (% 1 $ 5D,% 2 $ 5D) / n ")% -23% 35; // POSIX-Printf Style // No REORDERING: COUT << Format ("Writing% S, X =% S:% D-TH Step / n")% "TOTO"% 40. 23% 50; // Prints "Writing Toto, X = 40.23: 50-TH Step / N" COUT << Format ("(x, y) = (% 5d,% 5d) / N")% -23 % 35; COUT << Format ("(% | 5 |,% | 5 |) / N")% -23% 35; COUT << Format ("(x, y) = (% | 1 $ 5 |,% | 2 $ 5 |) / N ")% -23% 35; // all those is the same, it prints" (x, y) = (-23, 35 ) / n "// using manipulators, VIA 'Group': cout << Format ("% 2%% 1%% 2% / N ")% 1% group (setfill ('x'), HEX, SETW (4 ), 16 3);

// Prints "xx13 1 xx13 / n" // printf Directives's type-flag can be used to pass formatting options: cout << format ("_% 1 $ 4D_ IS: _% 1 $ # 4x_, _% 1 $ # 4O_, AND _% 1 $ s_ by default / n ")% 18; //prints" _ 18_ is: _0x12_, _ 022_, and _18_ by default / n "// taking the string value: std :: string s; S = STR (Format ("% D% D")% 11% 22); assert (s == "11 22"); // ------------------ ----------------------------- // %% Prints '%' cout << Format ("%% ##% # x ")% 20 << endl; //prints"% ## 0x14 "// ----------------------------- ---------------- // Enforcing the right number of arguments // Too Much arguments Will throw an exception when feeding the unwanted argument: try {format ("% 1%% 1% ")% 101% 102; // the format-string refers to one argument, twice. Not 2 arguments. // thus giving 2 arguments is an error} catch (boost :: oose :: TOO_MANY_ARGS & EXC) {CERR << EXC .what () << "/ n / t / t *** DONT WORRY, THAT WAS Planned / N ";} // Too Few Arguments When Requesting The Result Will Also Throw An Exception: Try {CERR

Hello 3O OO O OO O (X, Y) = (-23, 35) Writing Toto, X = 40.23: 50-TH Step (x, y) = (-23, 35) (x, y) = -23, 35) = (-23, 35) XX13 1 XX13_ 18_ IS: _0X12_, _ 022_, and _18_ by default% ## 0x14 boost :: TOO_MANY_ARGS: Format-String Refered to Less Arguments Than were passed *** DONT WORRY, THAT WAS Plannedboost :: Too_Few_Args: format-string refered to more arguments Than wele passed *** Dont Worry, That Was Plannedesthing Went Ok, Exiting.

转载请注明原文地址:https://www.9cbs.com/read-54042.html

New Post(0)