C++11的stoi & stod
【摘要】
#include <iostream> #include <string>using namespace std;int main(){ string str="123"; int a=stoi(str); //如果遇到非法输入,stoi会自动截取最前面的数字,知道遇到不是数字为止 //所以说如果是浮点数,会截取...
-
#include <iostream>
-
#include <string>
-
using namespace std;
-
int main(){
-
string str="123";
-
int a=stoi(str);
-
//如果遇到非法输入,stoi会自动截取最前面的数字,知道遇到不是数字为止
-
//所以说如果是浮点数,会截取前面的整数部分
-
cout << a << endl;
-
str = "123.4354";
-
double b=stod(str);
-
//如果是非法输入,stod会截取最前面的浮点数,知道遇到不满足浮点数为止
-
cout << b;
-
-
/*
-
string s1=to_string(123); //把123这个数字转成字符串
-
cout << s1 <<endl;
-
string s2=to_string(3.5); //把4.5这个数字转成字符串
-
cout << s2 <<endl;
-
cout << s1+s2 <<endl; //把s1和s2两个字符串拼接起来并输出
-
printf("%s\n",(s1+s2).c_str());
-
//如果想用printf输出string,得加一个c_str()
-
*/
-
system("pause");
-
return 0;
-
-
}
文章来源: andyguo.blog.csdn.net,作者:山顶夕景,版权归原作者所有,如需转载,请联系作者。
原文链接:andyguo.blog.csdn.net/article/details/99414018
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)