1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| #include <iostream> #include <vector> #include <unordered_map> #include <fmt/core.h> #include <fmt/format.h> #include <fmt/chrono.h> #include <fmt/ranges.h> #include <fmt/os.h> #include <fmt/color.h>
void simple_print() { fmt::print("Hello, {}\n", "world!"); }
void string_format() { std::string s = fmt::format("The answer is {}.", 42); fmt::print("{}\n", s); }
void float_format() { fmt::print("The answer is {:.2f}\n", 1.12345678); }
void position_param() { fmt::print("I'd rather be {1} than {0}.\n", "right", "happy"); }
void named_param() { fmt::print("Hello, {name}! The answer is {number}. Goodbye, {name}.\n", fmt::arg("name", "World"), fmt::arg("number", 42)); }
void suffix_named_param() { using namespace fmt::literals; fmt::print("Hello, {name}! The answer is {number}. Goodbye, {name}.\n", "name"_a = "World", "number"_a = 22); }
void time_format() { using namespace std::literals::chrono_literals; fmt::print("Default format: {} {}\n", 42s, 100ms); fmt::print("strftime-like format: {:%H:%M:%S}\n", 3h + 15min + 30s); }
void collection_format() { std::vector<int> v = {1, 2, 3}; fmt::print("v: {}\n", v);
std::unordered_map<std::string, int> m{{"a", 1}, {"b", 2}}; fmt::print("m: {}\n", m); }
void format_to_file() { auto out = fmt::output_file("test.txt"); out.print("Don't {}", "Panic"); }
void font_format() { fmt::print(fg(fmt::color::crimson) | fmt::emphasis::bold, "Hello, {}!\n", "world"); fmt::print(fg(fmt::color::floral_white) | bg(fmt::color::slate_gray) | fmt::emphasis::underline, "Hello, {}!\n", "мир"); fmt::print(fg(fmt::color::steel_blue) | fmt::emphasis::italic, "Hello, {}!\n", "世界"); }
int main() {
simple_print();
string_format();
float_format();
position_param();
named_param();
suffix_named_param();
time_format();
collection_format();
format_to_file();
font_format();
return 0; }
|