Alternative to itoa() for converting integer to string C++?(替代 itoa() 将整数转换为字符串 C++?)
问题描述
我想知道是否有 itoa() 的替代方法可以将整数转换为字符串,因为当我在 Visual Studio 中运行它时会收到警告,并且当我尝试在Linux,我得到一个编译错误.
I was wondering if there was an alternative to itoa() for converting an integer to a string because when I run it in visual Studio I get warnings, and when I try to build my program under Linux, I get a compilation error.
推荐答案
在 C++11 中你可以使用 std::to_string:
In C++11 you can use std::to_string:
#include <string>
std::string s = std::to_string(5);
如果您使用的是 C++11 之前的版本,则可以使用 C++ 流:
If you're working with prior to C++11, you could use C++ streams:
#include <sstream>
int i = 5;
std::string s;
std::stringstream out;
out << i;
s = out.str();
取自 http://notfaq.wordpress.com/2006/08/30/c-convert-int-to-string/
这篇关于替代 itoa() 将整数转换为字符串 C++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:替代 itoa() 将整数转换为字符串 C++?
基础教程推荐
- 提升 ASIO 流缓冲 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
- c++ STL设置差异 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
