網(wǎng)站首頁 編程語言 正文
一、int轉(zhuǎn)string
#include <iostream> #include <string> int main() { double f = 23.43; double f2 = 1e-9; double f3 = 1e40; double f4 = 1e-40; double f5 = 123456789; 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轉(zhuǎn)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 myint3 = 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 " << myint3 << '\n'; //std::cout << "std::stoi(\"" << str4 << "\") is " << myint4 << '\n'; }
結(jié)果:
std::stoi("45") is 45
std::stoi("3.14159") is 3
std::stoi("31337 with words") is 31337
原文鏈接:https://blog.csdn.net/m0_37316917/article/details/82712017
相關(guān)推薦
- 2023-04-06 關(guān)于虛函數(shù)實現(xiàn)多態(tài)的原理及分析_C 語言
- 2022-05-03 windows?server?2019開啟iis服務(wù)器+tp5.1的完美配置運行流程_win服務(wù)器
- 2022-03-15 pycharm安裝opencv出現(xiàn)錯誤:Could not find a version that
- 2022-04-18 http通過StreamingHttpResponse完成連續(xù)的數(shù)據(jù)傳輸長鏈接方式_python
- 2022-07-11 jenkins數(shù)據(jù)遷移和備份
- 2022-12-10 jquery異常問題Uncaught?TypeError:?$(...).on?is?not?a?f
- 2021-12-11 由redux到react-redux再到rematch
- 2022-11-16 Android?十六進制狀態(tài)管理實例詳解_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支