Convert a vector<int> to string with fmt library
Asked Answered
P

1

19

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

Parody answered 11/12, 2019 at 7:10 Comment(4)
Sounds like an XY problem.Schizomycete
std::cout << s.substr(1, s.size()-2) << '\n';Adenoidal
I am not familiar with fmt, however, it seems that the prefix { and postfix } are hardcoded in ranges.h header.Torras
@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.Cimbri
H
48

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"
Hueston answered 11/12, 2019 at 7:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.