首页 > 作文

C++ string与int的相互转换(使用C++11)

更新时间:2023-04-04 18:25:08 阅读: 评论:0

目录
一、int转string二、string转int

一、int转string

#include <iostream>#include <string> int mphtoshop教程ain() {    double f = 23.43;    double f2 = 1e-9;    double f3 = 1e40;    double f4 = 1e-40;    double f5 = 123个人简历空白表456789;    std::string f_str = std::to_string(f);    std::string f_str2 = std::to_string(f2); // 注意:返回 "0.000000"    std::string f_str3 = std::to_string(f3); // 注意:不返回 "1e+40".    std::string f_str4 = std::to_string(f4); // 注意:返回 "0.000000"    std::string 我们是一家人歌词f_str5 = std::to_string(f5);    std::cout << "std::cout: " << f << '\n'              << "to_string: " << f_str  << "\n\n"              << "std::cout: " << f2 << '\n'              << "to_string: " << f_str2 << "\n\n"              << "std::cout: " << f3 << '\n'              << "to_string: " << f_str3 << "\n\n"              << "std::cout: " << f4 << '\n'              << "to_string: " << f_str4 << "\n\n"              << "std::cout: " << f5 << '\n'              << "to_string: " << f_str5 << '\n';}

输出

std::cout: 23.43
to_string: 23.430000

std::cout: 1e-09
to_string: 0.000000

std::cout: 1e+40
to_string: 10000000000000000303786028427003666890752.000000

std::cout: 1e-40
to_string: 0.000000

std::cout: 1.23457e+08
to_string: 123456789.000000

二、string转int

#include <iostream>#include <string> int main(){    std::string str1 = "45";    std::string str2 = "3.14159";    std::string str3 = "31337 with words";    std::string str4 = "words and 2";     int myint1 = std::stoi(str1);    int myint2 = std::stoi(str2);    int myi黑脸娃娃能美白吗nt3 = std::stoi(str3);    // 错误: 'std::invalid_argument'    // int myint4 = std::stoi(str4);     std::cout << "std::stoi(\"" << str1 << "\") is " << myint1 << '\n';    std::cout << "std::stoi(\"" << str2 << "\") is " << myint2 << '\n';    std::cout << "std::stoi(\"" << str3 << "\") is " << my适合瓜子脸的发型int3 << '\n';    //std::cout << "std::stoi(\"" << str4 << "\") is " << myint4 << '\n'; }

结果:

std::stoi(“45”) is 45
std::stoi(“3.14159”) is 3
std::stoi(“31337 with words”) is 31337

到此这篇关于c++ string与int的相互转换(使用c++11)的文章就介绍到这了,更多相关c++ string与int的相互转换内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!

本文发布于:2023-04-04 18:25:07,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/37addf1aab176cde32350450d4b57bc2.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:C++ string与int的相互转换(使用C++11).doc

本文 PDF 下载地址:C++ string与int的相互转换(使用C++11).pdf

标签:瓜子脸   文章   黑脸   相关文章
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图