make vectorToString behave as it should, without appending separators when it isn't required

This commit is contained in:
Czarek Nakamoto
2024-04-19 16:25:05 +02:00
parent a1d03a28a7
commit 7c0c08627c

View File

@@ -11,12 +11,19 @@
#include <cstring>
const char* vectorToString(const std::vector<std::string>& vec, const std::string separator) {
// Check if the vector is empty
if (vec.empty()) {
return "";
}
// Concatenate all strings in the vector
std::string result;
for (const auto& str : vec) {
result += str;
for (size_t i = 0; i < vec.size() - 1; ++i) {
result += vec[i];
result += separator;
}
result += vec.back(); // Append the last string without the separator
const char* cstr = result.c_str();
return cstr;
}