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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM