The erase function is prototyped as follows:
(1) string& erase (size_t pos = 0, size_t n = NPOs);
(2) Iterator erase (iterator position);
(3) Iterator erase (iterator first, iterator last);
That means there are three ways to use:
(1) Erase (pos,n); Deleting n characters starting from POS, such as erase (0,1), deletes the first character
(2) Erase (position); Deletes a character at position (position is a string type of iterator)
(3) Erase (first,last); Delete the characters from first to last (both first and last are iterators)
Let me give you an example:
#include <iostream>#include<string>using namespacestd;intMain () {stringSTR ("This was an example phrase."); string:: Iterator it; //section (1) UsageStr.erase (Ten,8); cout<< str << Endl;//"This was an phrase." //section (2) UsageIt=str.begin () +9; Str.erase (IT); cout<< str << Endl;//"This is a phrase." //section (3) UsageStr.erase (Str.begin () +5, Str.end ()-7); cout<< str << Endl;//"this phrase." return 0;}
Use of string erase functions in C + +