C++删除string最后一个字符的几种方法

#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;
}

 

 

 

转自:http://blog.csdn.net/cainv89/article/details/48102991

http://www.cnblogs.com/ylwn817/articles/1967689.html

 

posted @ 2021-09-05 20:13  Yavn  阅读(5085)  评论(0)    收藏  举报