Skip to content

Instantly share code, notes, and snippets.

@aeppert
Last active August 29, 2015 14:24
Show Gist options
  • Save aeppert/b57cb541d0a290837bf2 to your computer and use it in GitHub Desktop.
Save aeppert/b57cb541d0a290837bf2 to your computer and use it in GitHub Desktop.
Template for string conversion with padding and prefix facility
#include <iostream>
#include <sstream>
#include <string>
#include <limits>
using namespace std;
template <class T> std::string to_string(T t, ios_base & (*f)(ios_base&), std::string prefix = "", bool pad = true)
{
std::ostringstream oss;
if(pad) {
oss.fill('0');
oss.width(std::numeric_limits<T>::digits/4);
}
oss << f << t;
prefix += oss.str();
return prefix;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment