c++ string、int、char数组互相转换(方法简单)

 

 

一。数字--string

(1)数字转string(c++11有的新方法)

例:

int i = 9;

string str = to_string(i);

 

(2)string转数字

stoi();stol();stoul();stoll();stoull();stof()

函数原型:int stoi (const string& str, size_t* idx = 0, int base = 10);

例:

int a;

string str = "1010";

a = stoi(str, 0, 2);//从0位置开始取2个数字

a为10

 

二。string -- char数组

(1)string转char数组

char *p;

string str = "hello";

p = str.c_str();

(2)char数组转string

直接转

char a[10] = "hello";

string str(a);

 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值