Skip to content

Commit fb0c824

Browse files
author
lishuangquan
committed
add tostr.h
Change-Id: I61db7993192e82b453427bdc68c681af7a48d49b
1 parent 87c6c6e commit fb0c824

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

tostr.h

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
3+
#include <string>
4+
#include <sstream>
5+
#include <vector>
6+
7+
namespace cil {
8+
9+
template <typename T>
10+
std::string tostr(const T& t) {
11+
std::ostringstream os;
12+
os << t;
13+
return os.str();
14+
}
15+
16+
template <typename T>
17+
void push_to_str_vec(std::vector<std::string>& strs, const T& t) {
18+
strs.push_back(tostr(t));
19+
}
20+
template <typename T, typename ...Args>
21+
void push_to_str_vec(std::vector<std::string>& strs, const T& t, const Args&... args) {
22+
push_to_str_vec(strs, t);
23+
push_to_str_vec(strs, args...);
24+
}
25+
26+
template <typename T, typename ...Args>
27+
std::vector<std::string> tostrs(const T& t, const Args&... args) {
28+
std::vector<std::string> strs;
29+
push_to_str_vec(strs, t, args...);
30+
return strs;
31+
};
32+
33+
template <typename ...Args>
34+
std::string tostr_with_delim(const std::string& delim, const Args&... args) {
35+
std::vector<std::string> strs;
36+
push_to_str_vec(strs, args...);
37+
std::string ret;
38+
for (size_t i = 0; i < strs.size(); ++i) {
39+
if (i > 0) ret += delim;
40+
ret += strs[i];
41+
}
42+
return ret;
43+
};
44+
45+
template <typename ...Args>
46+
std::string tostr(const Args&... args) {
47+
return tostr_with_delim(" ", args...);
48+
};
49+
50+
} // namespace cil

0 commit comments

Comments
 (0)