I\'m trying to build a string using data elements stored in a std::list, where I want commas placed only between the elements (ie, if elements are {A,B,C,D} in list, result stri
You could avoid this problem altogether by using:
std::string Compose(DataItemList& dilList)
{
std::stringstream ssDataSegment;
for(iterItems = dilList.begin(); iterItems != dilList.end(); iterItems++)
{
ssDataSegment << (*iterItems)->ToString() << ","; // always write ","
}
std::string result = ssDataSegment.str();
return result.substr(0, result.length()-1); // skip the last ","
}
You first write the "," for all elements (even for the last one). Than afterwards, you remove the unwanted last "," using substr
. This additionally results in clearer code.