forked from 0voice/cpp_new_features
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path007_format_formatted_size.cpp
More file actions
30 lines (21 loc) · 1.06 KB
/
007_format_formatted_size.cpp
File metadata and controls
30 lines (21 loc) · 1.06 KB
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
#include <format>
#include <iostream>
#include <vector>
#include <string_view>
int main()
{
using namespace std::literals::string_view_literals;
constexpr auto fmt_str { "Hubble's H{0} {1} {2:*^4} miles/sec/mpc."sv };
constexpr auto sub_zero { "₀"sv }; // { "\u2080"sv } => { 0xe2, 0x82, 0x80 };
constexpr auto aprox_equ { "≅"sv }; // { "\u2245"sv } => { 0xe2, 0x89, 0x85 };
constexpr int Ho { 42 }; // H₀
const auto min_buffer_size = std::formatted_size(fmt_str, sub_zero, aprox_equ, Ho);
std::cout << "Min buffer size = " << min_buffer_size << '\n';
// Use std::vector as dynamic buffer. Note: buffer does not include the trailing '\0'.
std::vector<char> buffer(min_buffer_size);
std::format_to_n(buffer.data(), buffer.size(), fmt_str, sub_zero, aprox_equ, Ho);
std::cout << "Buffer: \"" << std::string_view{buffer.data(), min_buffer_size} << "\"\n";
// Or we can print the buffer directly by adding the trailing '\0'.
buffer.push_back('\0');
std::cout << "Buffer: \"" << buffer.data() << "\"\n";
}