码迷,mamicode.com
首页 > 其他好文 > 详细

string int 相互转换

时间:2017-07-14 21:14:34      阅读:183      评论:0      收藏:0      [点我收藏+]

标签:常量   string类   转换   操作方法   UI   sof   操作   mil   兼容   

1、string转int

  string str = "1234";

  int a;

  a = stoi(str)     ---> C++11 

  uint32_t b = stoul(str)   ---> C++11

  a = atoi(str.c_str()); 

    { const char *c_str();

    c_str()函数返回一个指向正规C字符串的指针常量, 内容与本string串相同. 

    这是为了与c语言兼容,在c语言中没有string类型,故必须通过string类对象的成员函数c_str()把string 对象转换成c中的字符串样式。

    注意:一定要使用strcpy()函数 等来操作方法c_str()返回的指针 }

2、int 转 string

  to_string(a);

string int 相互转换

标签:常量   string类   转换   操作方法   UI   sof   操作   mil   兼容   

原文地址:http://www.cnblogs.com/kilen/p/7172083.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!