PAT C++常用函数(持续更新)
生活随笔
收集整理的這篇文章主要介紹了
PAT C++常用函数(持续更新)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
字符串處理
stoi(string) 字符轉數字
string s = "0123456789"; cout << s << endl; cout << stoi(s) - 1;Output:
0123456789 123456788substr(start, step) 提取子串
string s = "0123456789"; cout << s << endl; s = s.substr(1, 3); cout << s;Output:
0123456789 123保留有效數字
頭文件 #include<iomanip>setprecision(n) 保留n位有效數字
double t = 1.23456; cout << setprecision(3) << t;Output:
1.23setiosflags(ios::fixed) + setprecision(3) 保留n位小數
double t = 1.23456; cout << setiosflags(ios::fixed) << setprecision(3) << t;Output:
1.235Int+long+long long范圍
int -2147483648~2147483647 long -2147483648~2147483647 long long -9223372036854775808 ~9223372036854775807輸入輸出
sscanf()+sprintf()
sscanf() 按指定格式從一個字符串讀取數據到另一個變量中 sprintf() 把格式化好的數據寫入到字符串中 char a[MAX]; char b[MAX]; double t; cin >> a; sscanf(a, "%lf", &t); sprintf(b, "%.2f", t); cout << a << endl << b << endl;Input:
123.456Output:
123.456 123.46總結
以上是生活随笔為你收集整理的PAT C++常用函数(持续更新)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Pat乙级 1034 有理数四则运算
- 下一篇: Pat乙级 1045 快速排序