c++ - 我可以在不包含任何库的情况下将 int 转换为字符串吗??
问题描述
我可以在没有任何库的情况下将“int”转换为“string”<iostream>
吗?这个'int'包括否定的情况。
解决方案
我可以在没有任何库的情况下将“int”转换为“string”吗?
如果你的意思是:
除了已经用 C++ 构建的库
然后看:to_string
例子:
// to_string example
#include <iostream> // std::cout
#include <string> // std::string, std::to_string
int main ()
{
std::string pi = "pi is " + std::to_string(3.1415926);
std::string perfect = std::to_string(1+2+4+7+14) + " is a perfect number";
std::cout << pi << '\n';
std::cout << perfect << '\n';
return 0;
}
输出:
pi is 3.141593
28 is a perfect number
推荐阅读
- arrays - Why I can't access child property in typescript?
- c# - 加载 2 个 XML,用第三个值替换匹配的值
- python - 如何从 Python 中的不同目录导入文件
- excel - 如何复制某些单元格包含文本的行,并将它们粘贴到另一个位置?
- javascript - 防止 javascript 无休止地附加 dom
- r - 从 Rcpp 中的列表中提取元素似乎有点慢
- python - 如何修复 'mongod[10993]: Error reading config file: No such file or directory' in Ubuntu 18.04
- reactjs - 如何将我的应用程序与反应管理员分开?
- python - Python regex - 不贪婪的量词问题
- amazon-web-services - AWS/EC2/Ubuntu/Nginx - Web 根在哪里?