Search code examples
c++formatc++20

How to print contents of a container using std::format


Using {fmt}, we can print a container as follows:

#include <vector>
#include <fmt/ranges.h>

int main() {
  std::vector<int> v = {1, 2, 3};
  fmt::print("{}\n", v);
}

Can I do the same in the C++20 standard library version?


Solution

  • Formatting ranges is not a part of C++20 std::format but is supported in C++23:

    import std;
    
    int main() {
      std::vector<int> v = {1, 2, 3};
      std::print("{}\n", v);
    }