string int的相互转换

xiaoxiao2021-02-28  133

                  

 1.int 转 string 

头文件  #include<sstream>

                      int x;

                     stringstream sd;

                       sd << x;                     string str = sd.str();

头文件  #include<strstream>

                   strstream ss;                     int sum = 0;

                   string  c;

                    ss << sum;                     ss >> c;

2.string 转 int

  int temp ;

  string a="asd";

   temp = atoi(a.c_str());//将string 转化为char* 转化成十进制数字

//在gcc编译器下 itoa  函数可能会报错  因为不是标准功能函数

//附一个判断string 是不是  int 的函数

bool isNum(string str) {     string num = "1234567890";     if (str.find_first_not_of(num) == string::npos)         return true;     return false; }//返回在字符串中首次出现的不匹配str中的任何一个字符的首字符索引, 从index开始搜索, 如果全部匹配则返回string::npos。

转载请注明原文地址: https://www.6miu.com/read-59344.html

最新回复(0)