关于将字符串形式的转换成整数,浮点数等;以及逆方向的方法;

       有时我们需要将输入的一串字符改成它所含的意义的整数形式,浮点数形式。通常这种情况下我们会自己写一个函数然后遍历加判断再输出,太过麻烦。可是其实本来是有类似的函数存在的。

        std::stoi;

        std::stol;

        std::stoll;

        相关的具体情况请进入这里:http://www.cplusplus.com/reference/string/stoll;

        这个的话3个函数分别是转化为int,long int ,long long int;

        具体用法如下;

#include<iostream>
#include<string>
#include<cstring>
int main()
{
	std::ios::sync_with_stdio(false);
	std::string m;
	std::cin >> m; 
	long long int t = std::stol(m);
	std::cout << t << std::endl;
	return 0;
}

        还有就是浮点数的;

        std::stof;

        std::stod;

        std::stold;

        分别代表着转化为float,double ,long double;


#include<iostream>
#include<string>
#include<cstring>
int main()
{
	std::ios::sync_with_stdio(false);
	std::string m;
	std::cin >> m; 
	double t = std::stod(m);
	std::cout << t << std::endl;
	return 0;
}

        最后就是将一个浮点型,整形转化为字符串形式;

        

#include<iostream>
#include<string>
#include<cstring>
int main()
{
	std::ios::sync_with_stdio(false);
	double m;
	std::cin >> m; 
	std::string t = std::to_string(m);
	std::cout << t << std::endl;
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值