Hello,
I use this following function (on a microcontroller) to split a string into parts by using a delemiter.
E.g.:
Code:
std::vector<std::string> parts = splitStringToVectorArray("String1;String2;String3", ";");
The function works, but every time I call the function, data keeps persist on the heap memory. So I will run out of memory at all.
I read that this problem could belong to the push_back command.
So my question, is there any command missing in the function to release the used heap memory, or is something else wrong?
Code:
std::vector<std::string> splitStringToVectorArray(const std::string &inputString, const std::string &delimiter)
{
std::vector<std::string> vectorArray;
if (inputString.empty() || delimiter.empty())
{
vectorArray.push_back(inputString);
return vectorArray;
}
size_t initiation = 0;
size_t position;
while ((position = inputString.find(delimiter, initiation)) != std::string::npos)
{
std::string stringValue = inputString.substr(initiation, position - initiation);
if (!stringValue.empty())
{
vectorArray.push_back(stringValue);
}
initiation = position + delimiter.length();
}
if (initiation < inputString.length())
{
vectorArray.push_back(inputString.substr(initiation));
}
return vectorArray;
}
Thanks, Michael