std::string::erase in C++ 之 erase()方法截取string的前几位、后几位
Syntax 1: Erases all characters in a string
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; }
output:
Before erase() : Hello World!
After erase() :
Syntax 2: Erases all characters after position ‘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; }
output:
Before erase(idx) : Hello World! After erase(idx) : H
Syntax 3: Erases at most, len characters of *this, starting at index idx.
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; }
output:
Before erase : Hello World! After erase : H World!
Syntax 4: Erase the single character at iterator position 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; }
output:
Before erase : Hello World! After erase : Hell World!
Syntax 5: Erase characters from iterator pos. to another iterator 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; }
output:
Before erase : Hello World! After erase : World!
from:https://www.geeksforgeeks.org/stdstringerase-in-cpp/
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥