string::erase

sequence (1)
 string& erase (size_t pos = 0, size_t len = npos);两个参数都有默认值,传递的唯一参数匹配第一个
character (2)
iterator erase (const_iterator p);//擦除迭代器指定的一个字符
range (3)
iterator erase (const_iterator first, const_iterator last);

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main()
{
string s1("i love lyy who is lovely beautiful quiet");
//s1.erase(10);//
//cout << s1 << endl;
string str;
char *pp = new char[s1.length() + 1];
strcpy(pp, s1.c_str());
char *p = std::strtok(pp, " ");
//char *p = std::strtok(s1.c_str(), " ");//要把const char * 转化为char *
while(p)
{
cout << p << '\n';
if(!strcmp(p, "lovely")) break;
p = strtok(NULL, " ");
}
string::iterator it = s1.end() - 6;
s1.erase(it);
cout << s1 << endl;
s1.erase(s1.begin() + 18, s1.begin() + 24);
cout << s1 << endl;
return 0;
}

posted @ 2019-12-24 09:37  MoonXu  阅读(241)  评论(0编辑  收藏  举报