jeudi 20 août 2015

How can I convert a list of integer into comma separated string?

I am currently implementing an new software. While parsing a file, I collect a list of line numbers which were skipped. After parsing I would like to output all line numbers as comma separated string.

How can I convert a std::list<int> into a std::string as comma separated list?

Assume this code segment as framework:

#include <list>
#include <string>

void parseFile()
{
    std::list<int> skippedLines;
    // ...parse file...
    if (!skippedLines.empty()) {
        const std::string skippedLinesAsString = ???
        log << "Skipped lines: " << skippedLinesAsString;
    }
}

Please note, I need a std::string as result. The shown stream is just for illustration purposes and can not be used for output.

I implemented it in a traditional way using a loop. But I am sure there is a simple algorithmic solution using a back-inserter or similar.

Aucun commentaire:

Enregistrer un commentaire