将 int 转换为 std::string
将 int 转换为字符串的最短方法(最好是内联方法)是什么?使用 stl 和 boost 的答案将受到欢迎。
What is the shortest way, preferably inline-able, to convert an int to a string? Answers using stl and boost will be welcomed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
非标准函数,但在大多数常见编译器上实现:
更新
C++11 引入了几个
std::to_string
重载(请注意,它默认为以 10 为基数)。Non-standard function, but its implemented on most common compilers:
Update
C++11 introduced several
std::to_string
overloads (note that it defaults to base-10).以下宏并不像一次性使用的 ostringstream 或 boost::lexical_cast 那样紧凑。
但是,如果您需要在代码中重复转换为字符串,则使用此宏比每次直接处理字符串流或显式转换更优雅。
它也非常通用,因为它可以转换
operator<<()
支持的所有内容,甚至可以组合使用。定义:
解释:
std::dec
是一种无副作用的方法,可将匿名ostringstream
转换为通用ostream
因此,operator<<()
函数查找对于所有类型都可以正常工作。 (如果第一个参数是指针类型,否则您会遇到麻烦。)dynamic_cast
将类型返回到ostringstream
,以便您可以调用str()就可以了。
使用:
The following macro is not quite as compact as a single-use
ostringstream
orboost::lexical_cast
.But if you need conversion-to-string repeatedly in your code, this macro is more elegant in use than directly handling stringstreams or explicit casting every time.
It is also very versatile, as it converts everything supported by
operator<<()
, even in combination.Definition:
Explanation:
The
std::dec
is a side-effect-free way to make the anonymousostringstream
into a genericostream
sooperator<<()
function lookup works correctly for all types. (You get into trouble otherwise if the first argument is a pointer type.)The
dynamic_cast
returns the type back toostringstream
so you can callstr()
on it.Use:
在包含
后,您可以使用此函数将int
转换为std::string
:You can use this function to convert
int
tostd::string
after including<sstream>
:虽然
std::to_string
是一个应该记住的简单工具,但从 C++20 开始,您可以包含
,它允许更详细的对话int
到string
:输出:
While
std::to_string
is a straightforward tool that should be kept in mind, starting with C++20, you may include<format>
, which allows for more elaborates conversations fromint
tostring
:output:
您可以在您的项目中包含 itoa 的实现。
这是修改为与 std::string 一起使用的 itoa: http://www.strudel.org.uk/itoa /
You might include the implementation of itoa in your project.
Here's itoa modified to work with std::string: http://www.strudel.org.uk/itoa/
假设我有
integer = 0123456789101112
。现在,这个整数可以通过stringstream
类转换为字符串。这是 C++ 中的代码:
Suppose I have
integer = 0123456789101112
. Now, this integer can be converted into a string by thestringstream
class.Here is the code in C++:
这是另一种将 int 转换为 string 的简单方法,
您可以访问此链接了解更多方法
https://www.geeksforgeeks.org/what-is-the-best-way-in-c-to-convert-a-number-to-a-string/
Here, is another easy way to convert int to string
you may visit this link for more methods
https://www.geeksforgeeks.org/what-is-the-best-way-in-c-to-convert-a-number-to-a-string/
您可以在 C++11 中使用 std::to_string
You can use std::to_string in C++11
那么,众所周知的方法(在 C++11 之前)是使用流运算符:
当然,您可以使用模板函数将其推广到任何类型 ^^
Well, the well known way (before C++11) to do that is using the stream operator :
Of course, you can generalize it for any type using a template function ^^
来自
boost/lexical_cast.hpp
的boost::lexical_cast(yourint)
可以在 std::ostream 支持的情况下工作,但速度不如,例如, itoa
甚至看起来比 stringstream 或 scanf 更快:
boost::lexical_cast<std::string>(yourint)
fromboost/lexical_cast.hpp
Work's for everything with std::ostream support, but is not as fast as, for example,
itoa
It even appears to be faster than stringstream or scanf:
如果您无法使用
std::to_string
从 C++11 开始,您可以按照 cppreference.com 上的定义编写它:实施
您可以用它做更多事情。只需使用
"%g"
将 float 或 double 转换为字符串,使用"%x"
将 int 转换为十六进制表示形式,依此类推。If you cannot use
std::to_string
from C++11, you can write it as it is defined on cppreference.com:Implementation
You can do more with it. Just use
"%g"
to convert float or double to string, use"%x"
to convert int to hex representation, and so on.