繁体   English   中英

boost :: lexical_cast int用零填充字符串

[英]boost::lexical_cast int to string padding with zeros

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

范例:

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

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

ps不建议使用sprintf。

为什么要boost::lexical_cast 使用std::stringstream

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

您可以将std::ostringstream与常规流操纵器一起使用来进行格式化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM