考虑经常用的转字符串的方法:

std::stringstream ss;
ss << 1.23;
std::string aaa = ss.str();

现在有个更简洁的:

std::string aaa = std::to_string(1.23);

效率方面:C风格的sprintf因为没有动态内存分配,效率最高。std::to_string其次,最差的是std::stringstream。

从C++17开始,提供效率不差于sprintf, 同时类型安全更高的转换函数std::to_char 。

相关文章:

  • 2021-08-16
  • 2021-11-08
  • 2022-01-19
  • 2021-04-29
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-13
  • 2021-10-25
  • 2022-12-23
  • 2022-12-23
  • 2021-06-26
相关资源
相似解决方案