【整理】C++ string转int,string转double,string转long,int转string,double转string…

C++开发中,经常遇到各种基本类型与string的转换,掌握本博文,便可以轻松应对C++各基本类型与string的转换(比如:

int转string,double转string,long转string,string转int,string转double,string转long

...)。

第一种:基于C++ 11标准:

基于C++11标准

如果你用的编译器是基于最新的C++11标准,那么这个问题就变的很简单,因为<string>中已经封装好了对应的转换方法:

标准库中定义了to_string(val);可以将其它类型转换为string。还定义了一组stoi(s,p,b)、stol(s,p,b)、stod(s,p,b)等转换函数,可以函数,可以分别转化成int、long、double等.

   stoi(s,p,b);stol(s,p,b);stoul(s,p,b);stoll(s,p,b);stoull(s,p,b); 返回s的起始子串(表示整数内容的字符串)的数值,返回值的类型分别为:int、long、unsigned long、long long、unsigned long long.其中b表示转换所用的基数,默认为10(表示十进制).p是size_t的指针,用来保存s中第一个非数值字符的下标,p默认为0,即函数不返 回下标.

   stof(s, p); stod(s, p); stold(s, p); 返回s的起始子串(表示浮点数内容)的数值,返回值的类型分别是float、double、long double.参数p的作用与整数转换函数中的一样。

voidtestTypeConvert()
{
    //int --> string
    inti = 5;
    string s = to_string(i);
    cout << s << endl;
    //double --> string
    doubled = 3.14;
    cout << to_string(d) << endl;
    //long --> string
    longl = 123234567;
    cout << to_string(l) << endl;
    //char --> string
    charc = 'a';
    cout << to_string(c) << endl;   //自动转换成int类型的参数
    //char --> string
    string cStr; cStr += c;
    cout << cStr << endl;
 
 
    s = "123.257";
    //string --> int;
    cout << stoi(s) << endl;
    //string --> int
    cout << stol(s) << endl;
    //string --> float
    cout << stof(s) << endl;
    //string --> doubel
    cout << stod(s) << endl;
}


第一种方法记得编译的时候加上支持C++ 11的参数:-std=c++0x

输出结果:

5
3.140000
123234567
97
a
123
123
123.257
123.257


第二种:C++ 11标准之前:

C++11标准之前

C++11标准之前没有提供相应的方法可以调用,就得自己写转换方法了,代码如下:

从其它类型转换为string,定义一个模板类的方法。

从string转换为其它类型,定义多个重载函数。

#include <strstream>
template<classT>
string convertToString(constT val)
{
    string s;
    std::strstream ss;
    ss << val;
    ss >> s;
    returns;
}
 
 
intconvertStringToInt(conststring &s)
{
    intval;
    std::strstream ss;
    ss << s;
    ss >> val;
    returnval;
}
 
doubleconvertStringToDouble(conststring &s)
{
    doubleval;
    std::strstream ss;
    ss << s;
    ss >> val;
    returnval;
}
 
longconvertStringToLong(conststring &s)
{
    longval;
    std::strstream ss;
    ss << s;
    ss >> val;
    returnval;
}
 
voidtestConvert()
{
    //convert other type to string
    cout << "convert other type to string:" << endl;
    string s = convertToString(44.5);
    cout << s << endl;
    intii = 125;
    cout << convertToString(ii) << endl;
    doubledd = 3.1415926;
    cout << convertToString(dd) << endl;
 
    //convert from string to other type
    cout << "convert from string to other type:" << endl;
    inti = convertStringToInt("12.5");
    cout << i << endl;
    doubled = convertStringToDouble("12.5");
    cout << d << endl;
    longl = convertStringToLong("1234567");
    cout << l << endl;
}


结果如下:

convert other type to string:
44.5
125
3.14159
convert from string to other type:
12
12.5
1234567


  • 13
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明 YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明YOLO高分设计资源源码,详情请查看资源内容中使用说明

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值