0%

String Format for C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* Example Usage:
* fmt("%s: %d", str.c_str(), integer);
*/

template <class ...Args>
const std::string fmt(const std::string &format, Args... args) {
size_t BUFSIZE = std::snprintf(NULL, 0, format.c_str(), args...) + 1;
char *buf = new char[BUFSIZE];
memset(buf, 0, BUFSIZE * sizeof(char));
std::snprintf(buf, BUFSIZE, format.c_str(), args...);
std::string str(buf);
delete[] buf;
return str;
}