码迷,mamicode.com
首页 > 其他好文 > 详细

string.erase()

时间:2014-10-31 13:21:51      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   os   ar   使用   sp   div   

在代码中需要实现这样一个功能,需要将[00000001]这个存储在string中的字符串的中括号去掉,首先想到的就是string.erase()这个函数。结果。。。

代码:

 1 #include <iostream>
 2 #include <string>
 3 
 4 using namespace std;
 5 
 6 void strSize()
 7 {
 8     string strTest = "[0000-0001]";
 9     strTest.erase(strTest.size() - 1);
10     strTest.erase(0);
11 }
12 
13 void strLength()
14 {
15     string strTest = "[0000-0001]";
16     strTest.erase(strTest.length() - 1);
17     strTest.erase(0);
18 
19     cout << strTest << endl;
20 }
21 
22 void strIterator()
23 {
24     string strTest = "[0000-0001]";
25     strTest.erase(strTest.begin());
26     strTest.erase(strTest.end() - 1);
27 }
28 
29 int main(int argc, char *argv)
30 {
31     strIterator();
32 
33     return 0;
34 }

这段代码中一共有三个函数进行测试,却只有一个可用,strIterator

其他两个总是会提示段错误。而且,跟踪调试发现,使用过erase后,strTest的size就变为0了。

待以后自己知识足够了再来解决。

加油吧,少年!

 

string.erase()

标签:style   blog   io   color   os   ar   使用   sp   div   

原文地址:http://www.cnblogs.com/lit10050528/p/4064677.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!