将 int 转换成字符串的最短方法是什么?欢迎使用 stl 和升级的答案。
#include <sstream> #include <string> const int i = 3; std::ostringstream s; s << i; const std::string i_as_string(s.str());
那么,众所周知的方法(在 C + + 11之前)是使用流操作符:
#include <sstream> std::ostringstream s; int i; s << i; std::string converted(s.str());
当然,您可以使用模板函数 ^ ^ 将其泛化为任何类型
#include <sstream> template<typename T> std::string toString(const T& value) { std::ostringstream oss; oss << value; return oss.str(); }
boost/lexical_cast.hpp呼叫 boost::lexical_cast<std::string>(yourint)
boost/lexical_cast.hpp
boost::lexical_cast<std::string>(yourint)
使用 std: : ostream 支持可以解决所有问题,但是速度没有 itoa快
itoa
它甚至似乎比 string stream 或 Scanf 更快:
下面的宏不像一次性使用的 ostringstream或 boost::lexical_cast那样紧凑。
ostringstream
boost::lexical_cast
但是,如果您需要在代码中反复进行字符串转换,那么这个宏的使用比每次直接处理字符串流或显式强制转换要优雅得多。
它也是多功能的 非常,因为它转换 一切支持的 operator<<(),甚至在组合。
operator<<()
定义:
#include <sstream> #define SSTR( x ) dynamic_cast< std::ostringstream & >( \ ( std::ostringstream() << std::dec << x ) ).str()
说明:
std::dec是一种无副作用的方法,可以将匿名的 ostringstream变成通用的 ostream,从而使 operator<<()函数查找对所有类型都能正确工作。(如果第一个参数是指针类型,则会遇到麻烦。)
std::dec
ostream
dynamic_cast将类型返回到 ostringstream,因此您可以对它调用 str()。
dynamic_cast
str()
用途:
#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; }
非标准函数,但在大多数常用编译器上实现:
int input = MY_VALUE; char buffer[100] = {0}; int number_base = 10; std::string output = itoa(input, buffer, number_base);
更新
C + + 11引入了几个 std::to_string重载(注意,它默认为 base-10)。
std::to_string
您可以在项目中包含 itoa 的实现。 下面是对 itoa 进行了修改以适用于 std: : string: < a href = “ http://www.strudel.org.uk/itoa/”rel = “ nofollow”> http://www.strudel.org.uk/itoa/
可以在 C + + 11中使用 < strong > std: : to _ string
int i = 3; std::string str = std::to_string(i);
如果你不能使用 C + + 11中的 std::to_string,你可以按照 cppreference.com 上的定义来编写:
std::string to_string( int value ) 将一个有符号的十进制整数转换为一个字符串,该字符串的内容与 std::sprintf(buf, "%d", value)为足够大缓冲区生成的内容相同。
std::string to_string( int value )
std::sprintf(buf, "%d", value)
实施
#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; }
你可以用它做更多事。只需使用 "%g"将 float 或 double 转换为 string,使用 "%x"将 int 转换为十六进制表示,等等。
"%g"
"%x"
假设我有 integer = 0123456789101112。现在,这个整数可以被 stringstream类转换成一个字符串。
integer = 0123456789101112
stringstream
下面是 C + + 中的代码:
#include <bits/stdc++.h> using namespace std; int main() { int n,i; string s; stringstream st; for(i=0;i<=12;i++) { st<<i; } s=st.str(); cout<<s<<endl; return 0; }
#include <string> #include <stdlib.h>
这里是另一种将 int 转换为 string 的简单方法
int n = random(65,90); std::string str1=(__String::createWithFormat("%c",n)->getCString());
你可浏览此连结了解更多方法 Https://www.geeksforgeeks.org/what-is-the-best-way-in-c-to-convert-a-number-to-a-string/
在包含 <sstream>之后,您可以使用这个函数将 int转换为 std::string:
<sstream>
int
std::string
#include <sstream> string IntToString (int a) { stringstream temp; temp<<a; return temp.str(); }