Domanda

I need to create files with generated names. I use boost::lexical_cast to transform integers to std::string. Is it a possibility to get string with padding zeros; I have no c++11 tools, just everything that MSVS 2008 supports.

Example :

int i = 10;
std::string str = boost::lexical_cast<std::string>(i);

// str = "10"
// expect str = "000010"

p.s. don't suggest to use sprintf please.

È stato utile?

Soluzione

Why boost::lexical_cast? Use std::stringstream

std::ostringstream ss;
ss << std::setw(6) << std::setfill('0') << i;
const std::string str = ss.str();

Altri suggerimenti

You could use std::ostringstream with the normal stream manipulators for formatting.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top