您的位置:首页 > 其它

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

2018-03-09 11:10 513 查看
       有时我们需要将输入的一串字符改成它所含的意义的整数形式,浮点数形式。通常这种情况下我们会自己写一个函数然后遍历加判断再输出,太过麻烦。可是其实本来是有类似的函数存在的。
        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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐