Let's say I have a generic list of int
s. It can be an std::set
:
std::set<int> myInts;
myInts.insert(1);
myInts.insert(2);
myInts.insert(3);
I want to convert them to a std::string
with commas between values (not after the last value!).
I searched here and there are many different solutions.
One for example is an easy one-liner:
copy(v.begin(), v.end(), ostream_iterator<string>(cout, ","));
This works, but
- it puts an extra "," at the end
- as I need string as output I'd have to use an intermediate extra stringstream which causes unnecessary performance-overhead.
I could use a basic foreach too, where I can correct the last comma problem, and can also convert those ints to string, and concatenate them to the output string, but it feels not the best (fastest) way of doing this.
Boost is not available, and compiler is CLANG with C++14 support.
What is the best way (performance-wise) to convert that set<int>
to CSV string? (I can measure execution time difference between ostream_iterator
approach and foreach
approach, but the question here if there is anything else which fulfills all my wishes?