Skip to content

Instantly share code, notes, and snippets.

@mtao
Created December 13, 2016 16:38
Show Gist options
  • Save mtao/2c3059831e961190eb5f37bf60675ecc to your computer and use it in GitHub Desktop.
Save mtao/2c3059831e961190eb5f37bf60675ecc to your computer and use it in GitHub Desktop.
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include <sstream>
template <typename T>
std::string to_csv(const std::vector<T>& vec) {
std::stringstream ss;
std::copy(vec.begin(),vec.end()-1,std::ostream_iterator<T>(ss,","));
ss << vec.back();
return ss.str();
}
int main() {
std::vector<int> a = {0,1,2,3};
std::vector<double> b = {1,1,2,3,5};
std::cout << to_csv(a) << std::endl;
std::cout << to_csv(b) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment