@@ -58,27 +58,30 @@ void ReplaceAll(std::string& in_out, const std::string& search, const std::strin
58
58
}
59
59
60
60
/* *
61
- * Join a list of items
61
+ * Join all container items. Typically used to concatenate strings but accepts
62
+ * containers with elements of any type.
62
63
*
63
- * @param list The list to join
64
- * @param separator The separator
65
- * @param unary_op Apply this operator to each item in the list
64
+ * @param container The items to join
65
+ * @param separator The separator
66
+ * @param unary_op Apply this operator to each item
66
67
*/
67
- template <typename T , typename BaseType , typename UnaryOp>
68
- auto Join (const std::vector<T>& list , const BaseType & separator, UnaryOp unary_op)
68
+ template <typename C , typename S , typename UnaryOp>
69
+ auto Join (const C& container , const S & separator, UnaryOp unary_op)
69
70
{
70
- decltype (unary_op (list.at (0 ))) ret;
71
- for (size_t i = 0 ; i < list.size (); ++i) {
72
- if (i > 0 ) ret += separator;
73
- ret += unary_op (list.at (i));
71
+ decltype (unary_op (*container.begin ())) ret;
72
+ bool first{true };
73
+ for (const auto & item : container) {
74
+ if (!first) ret += separator;
75
+ ret += unary_op (item);
76
+ first = false ;
74
77
}
75
78
return ret;
76
79
}
77
80
78
- template <typename T , typename T2 >
79
- T Join (const std::vector<T>& list , const T2 & separator)
81
+ template <typename C , typename S >
82
+ auto Join (const C& container , const S & separator)
80
83
{
81
- return Join (list , separator, [](const T & i) { return i; });
84
+ return Join (container , separator, [](const auto & i) { return i; });
82
85
}
83
86
84
87
/* *
0 commit comments