C++ 将 int 转换为 std::string
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4668760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting an int to std::string
提问by Amir Rachum
What is the shortest way, preferably inline-able, to convert an int to a string? Answers using stl and boost will be welcomed.
将 int 转换为字符串的最短方法是什么,最好是内联的?欢迎使用 stl 和 boost 的答案。
回答by Yochai Timmer
You can use std::to_stringin C++11
您可以在 C++11 中使用std::to_string
int i = 3;
std::string str = std::to_string(i);
回答by Benoit
#include <sstream>
#include <string>
const int i = 3;
std::ostringstream s;
s << i;
const std::string i_as_string(s.str());
回答by ltjax
boost::lexical_cast<std::string>(yourint)
from boost/lexical_cast.hpp
boost::lexical_cast<std::string>(yourint)
从 boost/lexical_cast.hpp
Work's for everything with std::ostream support, but is not as fast as, for example, itoa
适用于支持 std::ostream 的所有内容,但速度不如,例如, itoa
It even appears to be faster than stringstream or scanf:
它甚至似乎比 stringstream 或 scanf 更快:
回答by neuro
Well the well known way to do that is using the stream operator :
那么众所周知的方法是使用流运算符:
#include <sstream>
std::ostringstream s;
int i;
s << i;
std::string converted(s.str());
Of course you can generalize it for any type using a template function ^^
当然,您可以使用模板函数将其概括为任何类型 ^^
#include <sstream>
template<typename T>
std::string toString(const T& value)
{
std::ostringstream oss;
oss << value;
return oss.str();
}
回答by user2622016
If you cannot use std::to_string
from C++11, you can write it as it is defined on cppreference.com:
如果您不能std::to_string
从 C++11 使用,您可以按照 cppreference.com 上的定义编写它:
std::string to_string( int value )
Converts a signed decimal integer to a string with the same content as whatstd::sprintf(buf, "%d", value)
would produce for sufficiently large buf.
std::string to_string( int value )
将有符号的十进制整数转换为与std::sprintf(buf, "%d", value)
足够大的 buf 产生的内容相同的字符串。
Implementation
执行
#include <cstdio>
#include <string>
#include <cassert>
std::string to_string( int x ) {
int length = snprintf( NULL, 0, "%d", x );
assert( length >= 0 );
char* buf = new char[length + 1];
snprintf( buf, length + 1, "%d", x );
std::string str( buf );
delete[] buf;
return str;
}
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.
你可以用它做更多的事情。仅用于"%g"
将 float 或 double 转换为字符串,用于"%x"
将 int 转换为十六进制表示,等等。
回答by Zac Howland
Non-standard function, but its implemented on most common compilers:
非标准函数,但它在最常见的编译器上实现:
int input = MY_VALUE;
char buffer[100] = {0};
int number_base = 10;
std::string output = itoa(input, buffer, number_base);
Update
更新
C++11 introduced several std::to_string
overloads (note that it defaults to base-10).
C++11 引入了几个std::to_string
重载(注意它默认为 base-10)。
回答by DevSolar
The following macro is not quite as compact as a single-use ostringstream
or boost::lexical_cast
.
以下宏不像一次性使用ostringstream
或boost::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 veryversatile, as it converts everythingsupported by operator<<()
, even in combination.
它也非常通用,因为它可以转换支持的所有内容operator<<()
,甚至可以组合使用。
Definition:
定义:
#include <sstream>
#define SSTR( x ) dynamic_cast< std::ostringstream & >( \
( std::ostringstream() << std::dec << x ) ).str()
Explanation:
解释:
The std::dec
is a side-effect-free way to make the anonymous ostringstream
into a generic ostream
so operator<<()
function lookup works correctly for all types. (You get into trouble otherwise if the first argument is a pointer type.)
这std::dec
是一种无副作用的方法,可以使匿名ostringstream
成为泛型,ostream
因此operator<<()
函数查找适用于所有类型。(否则,如果第一个参数是指针类型,则会遇到麻烦。)
The dynamic_cast
returns the type back to ostringstream
so you can call str()
on it.
在dynamic_cast
返回式回ostringstream
,所以你可以调用str()
它。
Use:
用:
#include <string>
int main()
{
int i = 42;
std::string s1 = SSTR( i );
int x = 23;
std::string s2 = SSTR( "i: " << i << ", x: " << x );
return 0;
}
回答by dodo
You can use this function to convert int
to std::string
after including <sstream>
:
您可以使用此函数在包含之后转换int
为:std::string
<sstream>
#include <sstream>
string IntToString (int a)
{
stringstream temp;
temp<<a;
return temp.str();
}
回答by ArtemGr
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/
您可能会在您的项目中包含 itoa 的实现。
这是 itoa 修改为与 std::string 一起使用:http: //www.strudel.org.uk/itoa/
回答by Vipul Dungranee
#include <string>
#include <stdlib.h>
Here, is another easy way to convert int to string
这是将 int 转换为 string 的另一种简单方法
int n = random(65,90);
std::string str1=(__String::createWithFormat("%c",n)->getCString());
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/
您可以访问此链接以获取更多方法 https://www.geeksforgeeks.org/what-is-the-best-way-in-c-to-convert-a-number-to-a-string/