14

How to remove {} from the output?

#include <iostream>
#include <vector>
#include <fmt/format.h>
#include <fmt/ranges.h>
int main () {
    std::vector<int> v = {1,2,3};
    std::string s = fmt::format("{}", v);
    std::cout << s << '\n'; // output : {1, 2, 3}
    return 0;
}

how to remove '{' and '}' in output for the above code and only print : 1, 2, 3

sadig
  • 431
  • 1
  • 4
  • 9
  • Sounds like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – CinCout Dec 11 '19 at 07:12
  • 2
    `std::cout << s.substr(1, s.size()-2) << '\n';` – Remy Lebeau Dec 11 '19 at 07:30
  • 1
    I am not familiar with fmt, however, it seems that the prefix `{` and postfix `}` are [hardcoded in `ranges.h` header](https://github.com/fmtlib/fmt/blob/master/include/fmt/ranges.h#L40). – Daniel Langr Dec 11 '19 at 07:30
  • @CinCout: This can't be an XY problem. The key aspect of an "XY problem" is that the "Y" is a strange or unusual problem. There's nothing strange or unusual about this question. – AndyMcoy Jul 18 '23 at 15:43

1 Answers1

40

I quote the fmt api:

#include <fmt/ranges.h>

std::vector<int> v = {1, 2, 3};
fmt::print("{}", fmt::join(v, ", "));
// Output: "1, 2, 3"
Joakim Thorén
  • 1,111
  • 10
  • 17