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个数字

扫描二维码关注公众号,回复: 10982073 查看本文章

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);

发布了59 篇原创文章 · 获赞 46 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/sinat_41852207/article/details/104743794