【无标题】

@ C++中字符串string与int, float, double相互转换

C++中字符串string与int, float, double相互转换

在平常的项目代码书写中经常会用到数据类型相互转换的情况,这里就将string类型转换为int, float, double类型做一下记录。他们之间的数据类型转换主要通过以下几种方式:

  1. 使用stringstream
  2. 使用atoi()、 atil() 、atof()函数

一、string转换int、float、double

方法一: 使用stringstream

利用模板函数:将string类型变量转换为常用的数值类型(此方法具有普遍适用性)
代码如下:

#include <iostream>  
#include <sstream>    //使用stringstream需要引入这个头文件  
using namespace std;  
  
//模板函数:将string类型变量转换为常用的数值类型
template <class Type>  
Type stringToNumber(const string& str)  
{  
    istringstream iss(str);  
    Type num;  
    iss >> num;  
    return num;      
}  
  
int main(int argc, char* argv[])  
{  
    string str("00801");  
    cout << stringToNumber<int>(str) << endl;  
    system("pause");  
    return 0;
}

其最终的输出结果为 801

方法二:使用atoi()、 atil() 、atof()函数 -----char类型向数值类型的转换

注意:使用 atoi 的话,如果 string s 为空,返回值为0.则无法判断s是0还是空

1 int atoi ( const char * str )

参数:str 是char* 类型的字符串
返回值:

1. 成功转换显示一个Int类型的值.  
2. 不可转换的字符串返回0.  
3. 如果转换后缓冲区溢出,返回 INT_MAX orINT_MIN

代码演示:

#include <iostream>  
using namespace std;  
int main ()  
{  
    int i;  
    char input_ [256];  
    cout<<"Enter a number: "<<endl;  
    fgets ( input_, 256, stdin );  
    i = atoi (input_);  
    cout<<"The input value is :"<<szInput<<endl;  
    cout<<" The convert number is:"<<i<<endl;  
    return 0;  
}

输出

Enter a number: 110
The input value is : 110
The convert number  is: 110
2. long int atol ( const char * str )

用法和atoi函数类似,返回值为long int

3. double atof ( const char * str );

返回值:

1. 转换成功返回doublel类型的值
2. 不能转换,返回0.0。  
3. 越界,返回HUGE_VAL

代码如下:

#include <stdio.h>  
#include <stdlib.h>  
#include <math.h>  
int main ()  
{  
  double n,m;  
  double pi=3.1415926535;  
  char szInput [256];  
  printf ( "Enter degrees: " );  
  gets ( szInput );  
  //char类型转换为double类型   
  n = atof ( szInput );  
  m = sin (n*pi/180);  
  printf ( "The sine of %f degrees is %f\n" , n, m );  
    
  return 0;  
}  

二、int、float、double转换string

方法一: char * itoa ( int value, char * str, int base )

代码演示

#include <iostream> 
using namespace std; 
int main(int argc, char* argv[])  
{    
    int n = 30; 
    char c[10];   
    //二进制转换
    itoa(n, c, 2);  
    cout << "2-> " << c << endl; 
    //十进制转换
    itoa(n, c, 10);    
    cout << "10-> " <<  c << endl;
    //十六进制转换
    itoa(n, c, 16);     
    cout << "16-> " <<  c << endl; 

    system("pause");     
    return 0;  
}

方法二: int sprintf ( char * str, const char * format, … )

参数说明:

% 印出百分比符号,不转换。
b 整数转成二进位。
c 整数转成对应的 ASCII 字元。
d 整数转成十进位。
f 倍精确度数字转成浮点数。
o 整数转成八进位。
s 整数转成字符串。
x 整数转成小写十六进位。
X 整数转成大写十六进位。

代码演示

#include <iostream>
#include <string>
using namespace std;  
int main() 
{     
    int n = 30; 
    char c[20];     //char *c;
    //%d十进制
    sprintf(c, "%d", n);   
    cout << c << endl;    
    //%o八进制
    sprintf(c, "%o", n);   
    cout << c << endl;   
    //%X大写十六进制
    sprintf(c, "%X", n);   
    cout << c << endl;  
    //%cACSII字元
    sprintf(c, "%c", n);   
    cout << c << endl;  
    
    //%f浮点数转换
    float f = 24.678;    
    sprintf(c, "%f", f); 
    cout << c << endl;   
    //%.2f"保留小数点后面两位
    sprintf(c, "%.2f", f);    
    cout << c << endl;  
    //转换两个数
    sprintf(c, "%d-%.2f", n, f);  
    cout << c << endl;  

    system("pause");    
    return 0;  
}
输出:
30  
36  
1E  
//注:这里是个特殊符号  
24.677999  
24.68  
30-24.68  

方法三:使用stringstream

代码演示:

#include <iostream> 
#include <string>
#include <sstream>    //引入stringstream头文件
using namespace std; 
int main() 
{     
    stringstream strStream;  
    int a = 100;  
    float f = 23.5566; 
    //int、float类型都可以塞到stringstream中
    strStream << a << "----"<< f ; 
    string s = strStream.str();  
    cout << s << endl;  

    system("pause");    
    return 0;  
}
输出:100----23.5566  

四、其他

1.sprintf可能引起缓冲区溢出,可以考虑使用 snprintf 或者非标准的 asprintf

2.如果是mfc程序,可以使用 CString::Format

3.如果使用boost,则可以直接使用: string s = boost::lexical_cast (a);

4.atoi 也是不可移植的。

参考

[1] http://blog.csdn.net/candadition/article/details/7342092
[2] https://www.cnblogs.com/lyggqm/p/4562727.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值