该函数删除了一部分字符串内容,从而缩短了字符串的长度。受影响的字符取决于所使用的成员函数版本:
返回值: Erase()返回* this。
- 语法1:删除字符串的所有字符
string& string ::erase ()
CPP
// CPP code to illustrate
// erase() function
#include
#include
using namespace std;
// Function to demo erase()
void eraseDemo(string str)
{
// Deletes all characters
str.erase();
cout << "After erase() : ";
cout << str;
}
// Driver code
int main()
{
string str("Hello World!");
cout << "Before erase() : ";
cout << str << endl;
eraseDemo(str);
return 0;
}
CPP
// CPP code to illustrate working of
// erase(idx)
#include
#include
using namespace std;
// Function to demo erase
void eraseDemo(string str)
{
// Deletes all characters except first one
str.erase(1);
cout << "After erase(idx) : ";
cout << str;
}
// Driver code
int main()
{
string str("Hello World!");
cout << "Before erase(idx) : ";
cout << str << endl;
eraseDemo(str);
return 0;
}
CPP
// CPP code to illustrate
// erase(size_type idx, size_type len )
#include
#include
using namespace std;
// Function to demo erase
void eraseDemo(string str)
{
// Deletes 4 characters from index number 1
str.erase(1, 4);
cout << "After erase : ";
cout << str;
}
// Driver code
int main()
{
string str("Hello World!");
cout << "Before erase : ";
cout << str << endl;
eraseDemo(str);
return 0;
}
CPP
// CPP code to illustrate
// erase(iterator pos)
#include
#include
using namespace std;
// Function to demo erase
void eraseDemo(string str)
{
// Deletes character at position 4
str.erase(str.begin() + 4);
cout << "After erase : ";
cout << str;
}
// Driver code
int main()
{
string str("Hello World!");
cout << "Before erase : ";
cout << str << endl;
eraseDemo(str);
return 0;
}
CPP
// CPP code to illustrate
// erase(iterator pos, iterator end)
#include
#include
using namespace std;
// Function to demo erase
void eraseDemo(string str)
{
// Deletes all characters between 0th index and
// str.end() - 6
str.erase(str.begin() + 0, str.end() - 6);
cout << "After erase : ";
cout << str;
}
// Driver code
int main()
{
string str("Hello World!");
cout << "Before erase : ";
cout << str << endl;
eraseDemo(str);
return 0;
}
输出:
Before erase() : Hello World!
After erase() :
2.语法2:删除位置“ pos”之后的所有字符
string& string ::erase (size_type pos)
- Throw out_of_range if idx > size().
CPP
// CPP code to illustrate working of
// erase(idx)
#include
#include
using namespace std;
// Function to demo erase
void eraseDemo(string str)
{
// Deletes all characters except first one
str.erase(1);
cout << "After erase(idx) : ";
cout << str;
}
// Driver code
int main()
{
string str("Hello World!");
cout << "Before erase(idx) : ";
cout << str << endl;
eraseDemo(str);
return 0;
}
输出:
Before erase(idx) : Hello World!
After erase(idx) : H
3.语法3:从索引idx开始,最多擦除* this的len个字符。
string& string ::erase (size_type idx, size_type len )
- If len is missing, all remaining characters are removed.
- Throw out_of_range if idx > size().
CPP
// CPP code to illustrate
// erase(size_type idx, size_type len )
#include
#include
using namespace std;
// Function to demo erase
void eraseDemo(string str)
{
// Deletes 4 characters from index number 1
str.erase(1, 4);
cout << "After erase : ";
cout << str;
}
// Driver code
int main()
{
string str("Hello World!");
cout << "Before erase : ";
cout << str << endl;
eraseDemo(str);
return 0;
}
输出:
Before erase : Hello World!
After erase : H World!
4.语法4:擦除迭代器位置pos处的单个字符。
string& string ::erase (iterator pos)
- Return the first character after the last character removed
- If no such character is remaining then, returns
string::end() i.e. position after the last character.
CPP
// CPP code to illustrate
// erase(iterator pos)
#include
#include
using namespace std;
// Function to demo erase
void eraseDemo(string str)
{
// Deletes character at position 4
str.erase(str.begin() + 4);
cout << "After erase : ";
cout << str;
}
// Driver code
int main()
{
string str("Hello World!");
cout << "Before erase : ";
cout << str << endl;
eraseDemo(str);
return 0;
}
输出:
Before erase : Hello World!
After erase : Hell World!
5.语法5:删除迭代器pos中的字符。到另一个迭代器pos。
string& string ::erase (iterator beg, iterator end )
- Erases all characters of the range [ beg, end)
- Returns end i.e. the first character after the
last character removed.
- If no such character is remaining then, returns
string::end() i.e. position after the last character
CPP
// CPP code to illustrate
// erase(iterator pos, iterator end)
#include
#include
using namespace std;
// Function to demo erase
void eraseDemo(string str)
{
// Deletes all characters between 0th index and
// str.end() - 6
str.erase(str.begin() + 0, str.end() - 6);
cout << "After erase : ";
cout << str;
}
// Driver code
int main()
{
string str("Hello World!");
cout << "Before erase : ";
cout << str << endl;
eraseDemo(str);
return 0;
}
输出:
Before erase : Hello World!
After erase : World!
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。