該函數將刪除一部分字符串內容,從而縮短字符串的長度。受影響的字符取決於所使用的成員函數版本:
返回值:erase()返回* this。
- 語法1:擦除字符串中的所有字符
string& string::erase ()
// CPP code to illustrate // erase() function #include <iostream> #include <string> 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; }
輸出:
Before erase():Hello World! After erase():
- 語法2:刪除位置“ pos”之後的所有字符
string& string::erase (size_type pos) - Throw out_of_range if idx > size().
// CPP code to illustrate working of // erase(idx) #include <iostream> #include <string> 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:從索引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 code to illustrate // erase(size_type idx, size_type len ) #include <iostream> #include <string> 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:刪除迭代器位置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 code to illustrate // erase(iterator pos) #include <iostream> #include <string> 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:刪除迭代器位置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 code to illustrate // erase(iterator pos, iterator end) #include <iostream> #include <string> 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!
相關用法
注:本文由純淨天空篩選整理自 std::string::erase in C++。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。