Convert a vector to a string

Definitely not as elegant as Python, but nothing quite is as elegant as Python in C++.

You could use a stringstream

#include <sstream>
//...

std::stringstream ss;
for(size_t i = 0; i < v.size(); ++i)
{
  if(i != 0)
    ss << ",";
  ss << v[i];
}
std::string s = ss.str();

You could also make use of std::for_each instead.

Leave a Comment