1

我需要使用生成的名称创建文件。我boost::lexical_cast用来将整数转换为std::string. 是否有可能获得带有填充零的字符串?我没有c++11 tools,只有所有MSVS 2008支持的东西。

例子 :

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

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

ps 不建议使用 sprintf 请。

4

2 回答 2

5

为什么boost::lexical_cast?利用std::stringstream

std::ostringstream ss;
ss << std::setw(6) << std::setfill('0') << i;
const std::string str = ss.str();
于 2014-01-15T08:06:16.163 回答
2

您可以使用std::ostringstream普通的流操纵器进行格式化。

于 2014-01-15T08:06:43.887 回答