C++学习心得:字符串转换(to_string,stoi,stol,stoul)
生活随笔
收集整理的這篇文章主要介紹了
C++学习心得:字符串转换(to_string,stoi,stol,stoul)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一、結論
使用C++11,并且導入頭文件?#include<string>
| other ---> string | to_string (int / long / float /...) | ? |
| string --->other | stoi | string 轉 int |
| stol | string 轉long | |
| stoll | string 轉 long long | |
| stoul | string 轉?unsigned long | |
| stoull | string 轉 unsigned long long | |
| stof | string 轉 float | |
| stod | string 轉 double |
?
?
?
?
?
?
?
?
注:在日常使用中, 最常用的是stoll和stod這兩個函數, stoll可以兼容stoi,stol; ?而stod可以兼容stof。
?
?
二、舉例
1、stoll()
#include<iostream> #include<string> using namespace std; int main() {string s = "11111";int a = stoll(s);cout << a; return 0; }輸出: 11111?
2、stod()
#include<iostream> #include<string> using namespace std; int main() {string s = "11.11";double a = stod(s);cout << a; return 0; }輸出:11.11?
?
三、參考資料
[【最貼心】C++字符串轉換(stoi;stol;stoul;stoll;stoull;stof;stod;stold)_來老鐵干了這碗代碼的博客-CSDN博客_stoul](https://blog.csdn.net/weixin_43899069/article/details/110290292?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-4&spm=1001.2101.3001.4242)
?
?
?
總結
以上是生活随笔為你收集整理的C++学习心得:字符串转换(to_string,stoi,stol,stoul)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 精准营销,我们这样做
- 下一篇: Unity加密(Sha256方式加密)