string int 相互转换

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

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