daj*_*daj 9 c++ string int casting
如何做整数 - >字符串转换已在互联网上多次回答...但是,我正在寻找最紧凑的"C++ - 方式"来做到这一点.
既然你能够使用重载+运算符连接字符串,那么最好能够按照python-ish x =(stringVariable + str(intVariable))连接的方式做一些事情,但我不知道如果在C++中有一种规范的方法可以做到这一点.
我看到的最常见的解决方案是:
stringstream:如果可能的话,最好不要使用3行代码(声明,写入流,转换为字符串)来连接一些字母和数字.
itoa:这有效,但我正在寻找一个规范的C++解决方案.此外,我认为itoa在技术上是非标准的,尽管我可能是错的.
提升格式/提升词法演员:这也有效,但是没有什么能在vanilla C++中完成这项任务吗?
Ker*_* SB 17
#include <string>
Run Code Online (Sandbox Code Playgroud)
字符串到整数: int n = std::stoi(s);
整数到字符串: std::string s = std::to_string(n);