在C++中将int转换为等价字符串的最简单方法是什么?我知道两种方法。有没有更简单的方法?

(1)

int a = 10;
char *intStr = itoa(a);
string str = string(intStr);

(2)

int a = 10;
stringstream ss;
ss << a;
string str = ss.str();

当前回答

如果您需要将具有固定位数的整数快速转换为左填充“0”的char*,这是little-endian体系结构(所有x86、x86_64和其他)的示例:

如果要转换两位数:

int32_t s = 0x3030 | (n/10) | (n%10) << 8;

如果要转换三位数:

int32_t s = 0x303030 | (n/100) | (n/10%10) << 8 | (n%10) << 16;

如果要转换四位数:

int64_t s = 0x30303030 | (n/1000) | (n/100%10)<<8 | (n/10%10)<<16 | (n%10)<<24;

如此类推,直到七位数。在本例中,n是给定的整数。转换后,它的字符串表示形式可以访问为(char*)&s:

std::cout << (char*)&s << std::endl;

注意:如果您需要使用大端字节顺序,虽然我没有测试它,但这里有一个示例:对于三位数,它是int32_ts=0x00303030|(n/100)<<24|(n/10%10)<<16|(n/10)<<8;对于四位数(64位拱门):int64_t s=0x0000000030303030 |(n/1000)<<56 |(n/100%10)<<48 |(n/10%10)<<40 |(n/10%)<<32;我认为它应该起作用。

其他回答

C++20:std::format现在将是惯用的方式。


C++17:

几年后,在与@v.oddou的讨论中,C++17提供了一种方法来实现最初基于宏的类型不可知的解决方案(保留在下面),而不需要经历宏的丑陋。

// variadic template
template < typename... Args >
std::string sstr( Args &&... args )
{
    std::ostringstream sstr;
    // fold expression
    ( sstr << std::dec << ... << args );
    return sstr.str();
}

用法:

int i = 42;
std::string s = sstr( "i is: ", i );
puts( sstr( i ).c_str() );

Foo x( 42 );
throw std::runtime_error( sstr( "Foo is '", x, "', i is ", i ) );

C++98:

由于“将…转换为字符串”是一个反复出现的问题,我总是在C++源代码的中心标题中定义SSTR()宏:

#include <sstream>

#define SSTR( x ) static_cast< std::ostringstream & >( \
        ( std::ostringstream() << std::dec << x ) ).str()

使用尽可能简单:

int i = 42;
std::string s = SSTR( "i is: " << i );
puts( SSTR( i ).c_str() );

Foo x( 42 );
throw std::runtime_error( SSTR( "Foo is '" << x << "', i is " << i ) );

以上是C++98兼容的(如果您不能使用C++11 std::to_string),并且不需要任何第三方包含(如果您无法使用Boost lexical_cast<>);但这两种解决方案都有更好的性能。

您可以按照Matthieu M.的建议使用C++11中提供的std::to_string:

std::string s = std::to_string(42);

或者,如果性能非常关键(例如,如果进行大量转换),可以使用{fmt}库中的fmt::format_int将整数转换为std::string:

std::string s = fmt::format_int(42).str();

或C字符串:

fmt::format_int f(42);
const char* s = f.c_str();

后者不执行任何动态内存分配,比BoostKarma基准测试上std::to_string的libstdc++实现快70%以上。有关详细信息,请参阅每秒将一亿个整数转换为字符串。

免责声明:我是{fmt}库的作者。

添加一些语法糖是相当容易的,它允许人们以流式的方式动态地编写字符串

#include <string>
#include <sstream>

struct strmake {
    std::stringstream s;
    template <typename T> strmake& operator << (const T& x) {
        s << x; return *this;
    }   
    operator std::string() {return s.str();}
};

现在,您可以向strmake()添加任何所需的内容(前提是为其定义了运算符<<(std::ostream&…)),并使用它代替std::string。

例子:

#include <iostream>

int main() {
    std::string x =
      strmake() << "Current time is " << 5+5 << ":" << 5*5 << " GST";
    std::cout << x << std::endl;
}

如果您需要将具有固定位数的整数快速转换为左填充“0”的char*,这是little-endian体系结构(所有x86、x86_64和其他)的示例:

如果要转换两位数:

int32_t s = 0x3030 | (n/10) | (n%10) << 8;

如果要转换三位数:

int32_t s = 0x303030 | (n/100) | (n/10%10) << 8 | (n%10) << 16;

如果要转换四位数:

int64_t s = 0x30303030 | (n/1000) | (n/100%10)<<8 | (n/10%10)<<16 | (n%10)<<24;

如此类推,直到七位数。在本例中,n是给定的整数。转换后,它的字符串表示形式可以访问为(char*)&s:

std::cout << (char*)&s << std::endl;

注意:如果您需要使用大端字节顺序,虽然我没有测试它,但这里有一个示例:对于三位数,它是int32_ts=0x00303030|(n/100)<<24|(n/10%10)<<16|(n/10)<<8;对于四位数(64位拱门):int64_t s=0x0000000030303030 |(n/1000)<<56 |(n/100%10)<<48 |(n/10%10)<<40 |(n/10%)<<32;我认为它应该起作用。

使用字符串流会更容易:

#include <sstream>

int x = 42;          // The integer
string str;          // The string
ostringstream temp;  // 'temp' as in temporary
temp << x;
str = temp.str();    // str is 'temp' as string

或生成函数:

#include <sstream>

string IntToString(int a)
{
    ostringstream temp;
    temp << a;
    return temp.str();
}