[英]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.