【发布时间】:2011-06-07 19:31:24
【问题描述】:
将 int 转换为字符串的最短方法是什么,最好是可内联的?欢迎使用 stl 和 boost 的答案。
【问题讨论】:
-
在 C++11 中,您可以像
std::string s = std::to_string(42)一样使用to_string -
C++11 和 to_string 万岁! :)
将 int 转换为字符串的最短方法是什么,最好是可内联的?欢迎使用 stl 和 boost 的答案。
【问题讨论】:
std::string s = std::to_string(42) 一样使用 to_string
嗯,众所周知的方法(在 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();
}
【讨论】:
#include <sstream>。
std:: ;)
在包含<sstream>后,您可以使用此函数将int转换为std::string:
#include <sstream>
string IntToString (int a)
{
stringstream temp;
temp<<a;
return temp.str();
}
【讨论】:
#include <string>
#include <stdlib.h>
这里是另一种将int转换为字符串的简单方法
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/
【讨论】:
假设我有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;
}
【讨论】:
如果您不能使用 C++11 中的 std::to_string,您可以按照 cppreference.com 上的定义编写它:
std::string to_string( int value )将有符号十进制整数转换为与std::sprintf(buf, "%d", value)为足够大的 buf 生成的内容相同的字符串。
实施
#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 转换为字符串,使用"%x" 将int 转换为十六进制表示,等等。
【讨论】:
你可以在 C++11 中使用std::to_string
int i = 3;
std::string str = std::to_string(i);
【讨论】:
非标准函数,但在最常见的编译器上实现:
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)。
【讨论】:
ostream 也可以使用,直到您需要将数字字符串保存为二进制、八进制或十六进制格式(例如 base-32)以外的其他格式。
itoa() 或stricmp() 等非标准函数作为anything 的答案。
sprintf 也可以实现 OP 的目标(尽管如果需要除通用基数之外的任何其他内容,仍然会缺乏灵活性)。
boost::lexical_cast<std::string>(yourint) 来自boost/lexical_cast.hpp
支持 std::ostream 的所有东西都可以工作,但不如 itoa 等速度快
它甚至似乎比 stringstream 或 scanf 更快:
【讨论】:
lexical_cast 带来的东西,但感觉 Boost 对于这种类型来说太过分了任务...
您可以在项目中包含 itoa 的实现。
这是修改后的 itoa 以与 std::string 一起使用:http://www.strudel.org.uk/itoa/
【讨论】:
以下宏不如一次性使用的ostringstream 或boost::lexical_cast 紧凑。
但是如果你需要在你的代码中反复转换为字符串,这个宏在使用上比每次都直接处理字符串流或显式转换更优雅。
它还非常通用,因为它可以转换operator<<() 支持的所有内容,甚至可以组合使用。
定义:
#include <sstream>
#define SSTR( x ) dynamic_cast< std::ostringstream & >( \
( std::ostringstream() << std::dec << x ) ).str()
说明:
std::dec 是一种无副作用的方法,可以将匿名 ostringstream 变为通用 ostream,因此 operator<<() 函数查找适用于所有类型。 (如果第一个参数是指针类型,你会遇到麻烦。)
dynamic_cast 将类型返回到ostringstream,因此您可以在其上调用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;
}
【讨论】:
inline template<class T> std::string SSTR( T x ) { return dynamic_cast< std::ostringstream & >( (std::ostringstream() << std::dec << x) ).str() } 不会吗? (尚未测试,但我确实想知道会出现什么问题以及为什么会出错?
int(我的第一个例子)。但是如果没有main 中的编译器可见的ostream(因为它隐藏在模板函数中),它将尝试在我的第二个示例中查找operator<<() 以查找const char [] - 这会发出嘶哑的声音。我知道 OP 只要求int,但这个更通用的宏非常有用(实际上非常普遍),我想我会把它包括在这里。
#include <sstream>
#include <string>
const int i = 3;
std::ostringstream s;
s << i;
const std::string i_as_string(s.str());
【讨论】:
std::ostringstream().operator<<(i).str()?