文章目錄
1 刪除字符串最後一個字符的方法
代碼實現:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str;
str = "123456";
cout << str << endl;
//方法一:使用substr()
str = str.substr(0, str.length() - 1);
cout << str << endl;
//方法二:使用erase()
str.erase(str.end() - 1);
cout << str << endl;
//方法三:使用pop_back()
str.pop_back();
cout << str << endl;
return 0;
}
運行結果:
以上內容來自: