未验证 提交 73742d36 编写于 作者: S sneaxiy 提交者: GitHub

add convert func for string helper (#38600)

上级 096afbe1
......@@ -193,6 +193,22 @@ std::string join_strings(const Container& strs, const std::string& delim) {
return str;
}
template <class Container, class DelimT, class ConvertFunc>
std::string join_strings(const Container& strs, DelimT&& delim,
ConvertFunc&& func) {
std::stringstream ss;
size_t i = 0;
for (const auto& elem : strs) {
if (i > 0) {
ss << delim;
}
ss << func(elem);
++i;
}
return ss.str();
}
// A helper class for reading lines from file. A line buffer is maintained. It
// doesn't need to know the maximum possible length of a line.
......
......@@ -56,3 +56,10 @@ TEST(StringHelper, JoinStrings) {
result = paddle::string::join_strings(v, " new ");
EXPECT_EQ(result, "hello new world");
}
TEST(StringHelper, JoinStringsWithConversion) {
std::vector<int> v = {2, 3};
auto result =
paddle::string::join_strings(v, ",", [](int x) { return x * x; });
EXPECT_EQ(result, "4,9");
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册