- pop() 出栈操作(删除栈顶),只是出栈,没有返回值。注意这里如果需要返回值,可以调用this->pop();
- .replace() 用于替换
str=str.replace(str.find("a"),2,"#"); //从第一个a位置开始的两个字符替换成#;
S=S.replace("aa","").replace("bb","")
- substr(size_type _Off = 0,size_type _Count = npos)
一种构造string的方法
形式 : s.substr(pos, len)
返回值: string,包含s中从pos开始的len个字符的拷贝(pos的默认值是0,len的默认值是s.size() - pos,即不加参数会默认拷贝整个s) - 字符串直接作为栈:使用pop_back() / push_back();
- st.push(stoi(tokens[i])); stoi:string转换为int类型
- st.push(stoll(tokens[i])); string转换成long long类型
- #include :iomanip全称为IO Manipulators,意思是输入输出操纵器
使用例子:cout << setiosflags(ios::fixed) << setprecision(1) << cost << endl;
在遇到要计算浮点数且希望能控制其输出、精度、小数点后的位数等时,用setiosflags( ios::fixed )来控制。setprecision(n )表示小数点精度。setiosflags(ios::fixed|ios::showpoint)当setprecision( )的精度为0的时候,有showpoint,结果就会显示小数点,没showpoint,就不显示小数点。 - case 3 … 5: //连续的值,用…表示连续
cout << “春季” << endl; break; - 判断数组长度: int len = sizeof(arr) / sizeof(int)// sizeof(arr)返回整个数组大小, sizeof(int)返回一个整型的大小,可以用来得到数组的长度
10.reverse:
需要添加头文件:#include
1.reverse(str.begin(),str.end()) 反转字符串
2.reverse(vector.begin(),vector.end()) 反转向量
3.reverse(a,a+strlen(a)) 反转数组 - cin的用法:
输入一个字符或者一个数字。
#include
using namespace std;
int main()
{
int m,n;
cin>>m>>n;
cout<<m<<n;//如果输入带空格的字符串,空格后面的无法输出。
return 0;
}
二、getline()
用于接受字符串,并可以接受空格并输出。
#include iostream>
#include cstring>
//用getline()必须有此头文件
using namespace std;
int main()
{
string a;
getline(cin,a);
cout<<a<<endl;
return 0;
}
三、cin.getline()
也用于接受字符串,并可以接受空格并输出。
#include
#include
using namespace std;
int main()
{
char a[105];
cin.getline(a,105);
cout<<a<<endl;
return 0;
}