int转string的多种方式

本文详细介绍了在C++中将整数转换为字符串的多种方法,包括使用itoa、stringstream、C++11标准库to_string、Boost库的lexical_cast,以及将字符串转换回整数的方法。适合C++开发者学习和参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

int a = 10;
char *intStr = itoa(a);
string str = string(intStr);
int a = 10;
stringstream ss;
ss << a;
string str = ss.str();

3.C++ 11(并不是编译器都支持)

#include <string> 
 
std::string s = std::to_string(42);

4.C++ 98

#include <sstream>
 
#define SSTR( x ) static_cast< std::ostringstream & >( \
        ( std::ostringstream() << std::dec << x ) ).str()
#include <boost/lexical_cast.hpp>
 
int num = 4;
std::string str = boost::lexical_cast<std::string>(num);

原博主:https://blog.youkuaiyun.com/zmdsjtu/article/details/80360398

  • string转int
std::string str = "123";
int n = atoi(str.c_str());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值