Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert a vector<int> to string with fmt library

Tags:

c++

fmt

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

like image 212
sadig Avatar asked Dec 11 '19 07:12

sadig


People also ask

How do you turn a vector into a string?

Convert Vector to String using toString() function To convert elements of a Vector to Strings in R, use the toString() function. The toString() is an inbuilt R function used to produce a single character string describing an R object.

What is fmt library?

{fmt} is an open-source formatting library providing a fast and safe alternative to C stdio and C++ iostreams.

How do I use FMT library?

To use the {fmt} library, add fmt/core. h , fmt/format. h , fmt/format-inl. h , src/format.cc and optionally other headers from a release archive or the Git repository to your project.


1 Answers

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"
like image 174
Joakim Thorén Avatar answered Sep 30 '22 02:09

Joakim Thorén