Skip to content

Latest commit

 

History

History
114 lines (92 loc) · 4.46 KB

321.md

File metadata and controls

114 lines (92 loc) · 4.46 KB
Info

Example

int main() {
  std::cout << std::format("{}",   std::vector{1, 2, 3}); // [1, 2, 3]
  std::cout << std::format("{:n}", std::vector{1, 2, 3}); // 1, 2, 3
  std::cout << std::format("{}",   std::tuple{'1', 2., 3}); // ('1', 2, 3)
  std::cout << std::format("{}",   std::vector{std::pair{'a',1}, std::pair{'b',2}}); // [(a, 1), (b, 2)]
  std::cout << std::format("{:m}", std::vector{std::pair{'a',1}, std::pair{'b',2}}); // {(a, 1), (b, 2)}
}

https://godbolt.org/z/4P4ev9r6Y

Puzzle

Can you add required format strings to satisfy the formatted output?

int main() {
  using namespace boost::ut;
  using namespace std::literals::string_literals;

  expect(R"(("hello", 42))"s == std::format("TODO", std::tuple{"hello", 42}));
  expect("[0x1, 0x2, 0x3]"s  == std::format("TODO", std::vector{1, 2, 3}));
  expect("[*1*, *2*, *3*]"s  == std::format("TODO", std::vector{1, 2, 3}));
  expect("Q L"s              == std::format("TODO", std::string{'Q', ' ', 'L'}));
  expect(R"(["a", "bc"])"s   == std::format("TODO", std::vector{std::vector{'a'}, std::vector{'b','c'}}));
  expect("[[97], [98, 99]]"s == std::format("TODO", std::vector{std::vector{'a'}, std::vector{'b','c'}}));
}

https://godbolt.org/z/66c7Mecx3

Solutions

expect(R"(("hello", 42))"s == std::format("{}", std::tuple{"hello", 42}));
expect("[0x1, 0x2, 0x3]"s == std::format("{::#x}", std::vector{1, 2, 3}));
expect("[*1*, *2*, *3*]"s == std::format("{::*^3}", std::vector{1, 2, 3}));
expect("Q L"s == std::format("{}", std::string{'Q', ' ', 'L'}));
expect(R"(["a", "bc"])"s == std::format("{::?s}", std::vector{std::vector{'a'}, std::vector{'b','c'}}));
expect("[[97], [98, 99]]"s == std::format("{:::d}", std::vector{std::vector{'a'}, std::vector{'b','c'}}));

https://godbolt.org/z/cKcq39qWc

expect(R"(("hello", 42))"s == std::format("{}", std::tuple{"hello", 42}));
expect("[0x1, 0x2, 0x3]"s == std::format("{::#x}", std::vector{1, 2, 3}));
expect("[*1*, *2*, *3*]"s == std::format("{::*^3}", std::vector{1, 2, 3}));
expect("Q L"s == std::format("{:s}", std::string{'Q', ' ', 'L'}));
expect(R"(["a", "bc"])"s ==
       std::format("{::?s}",
                   std::vector{std::vector{'a'}, std::vector{'b', 'c'}}));
expect("[[97], [98, 99]]"s ==
       std::format("{:::d}",
                   std::vector{std::vector{'a'}, std::vector{'b', 'c'}}));

https://godbolt.org/z/sbGvrTxMa

expect(R"(("hello", 42))"s == std::format("{}", std::tuple{"hello", 42}));
expect("[0x1, 0x2, 0x3]"s == std::format("{::#x}", std::vector{1, 2, 3}));
expect("[*1*, *2*, *3*]"s == std::format("{::*^3}", std::vector{1, 2, 3}));
expect("Q L"s == std::format("{}", std::string{'Q', ' ', 'L'}));
expect(R"(["a", "bc"])"s ==
       std::format("{::?s}",
                   std::vector{std::vector{'a'}, std::vector{'b', 'c'}}));
expect("[[97], [98, 99]]"s ==
       std::format("{:::d}",
                   std::vector{std::vector{'a'}, std::vector{'b', 'c'}}));

https://godbolt.org/z/4Mhs1KzMs

expect(R"(("hello", 42))"s == std::format("{}", std::tuple{"hello", 42}));
expect("[0x1, 0x2, 0x3]"s == std::format("{::#x}", std::vector{1, 2, 3}));
expect("[*1*, *2*, *3*]"s == std::format("{::*^3}", std::vector{1, 2, 3}));
expect("Q L"s == std::format("{:'^3}", std::string{'Q', ' ', 'L'}));
expect(R"(["a", "bc"])"s ==
       std::format("{::?s}",
                   std::vector{std::vector{'a'}, std::vector{'b', 'c'}}));
expect("[[97], [98, 99]]"s ==
       std::format("{:::d}",
                   std::vector{std::vector{'a'}, std::vector{'b', 'c'}}));

https://godbolt.org/z/v9GqWM5s5

expect(R"(("hello", 42))"s == std::format("{}", std::tuple{"hello", 42}));
expect("[0x1, 0x2, 0x3]"s == std::format("{::#x}", std::vector{1, 2, 3}));
expect("[*1*, *2*, *3*]"s == std::format("{::*^3}", std::vector{1, 2, 3}));
expect("Q L"s == std::format("{}", std::string{'Q', ' ', 'L'}));
expect(R"(["a", "bc"])"s ==
       std::format("{::?s}",
                   std::vector{std::vector{'a'}, std::vector{'b', 'c'}}));
expect("[[97], [98, 99]]"s ==
       std::format("{:::d}",
                   std::vector{std::vector{'a'}, std::vector{'b', 'c'}}));

https://godbolt.org/z/6jbqTMG15