关于栈Stack的基本操作
//栈的定义及入栈出栈操作
#include<stack>
//定义
stack <int>stk;
//入栈
stk.push();
//打印并出栈
while(!stk.empty())
{
cout<<stk.top()<<endl;
stk.pop();
}
关于C++字符串的一些常见操作
1.字符串与int的相互转换
字符串→int
//引入头文件
#include<string>
string s = to_string(int n)
int→字符串
//利用标准库中的atoi函数(可以过滤掉前缀0)
string s = 12345;
int m = atoi(s.c_str())
字符串反转
//利用algorithm中的reverse函数
#include<algorithm>
reverse(s.begin(),s.end());
C++控制小数点精度
//引入头文件
#include<iomanip>
cout<<fixed<<setprecision(2)<<n<<endl;
这里setprecision(2)表示保留小数点后2位,如果不加fixed那么只会输出有效数字。
n = 0.5000000;
cout<<setprecision(2)<<n<<endl;
cout<<fixed<<setprecision(2)<<n<<endl;
第一次结果是0.5,第二次结果是0.50