std::basic_string::erase(3)
std::basic_string::erase(3)
okay bestie, welcome to the C++ Standard Libary page for std::basic_string::erase β yes, thatβs the manual name, spelling and all π. This is the function that removes specified characters from the string, and we are going through every single overload, exception, and tiny detail together. π
π NAME
std::basic_string::erase β std::basic_string::erase
β¨ Synopsis
basic_string& erase( size_type index = 0, size_type count = npos ); (1) (constexpr since C++20)
iterator erase( iterator position ); (until C++11)
iterator erase( const_iterator position ); (since C++11) (2) (constexpr since C++20)
iterator erase( iterator first, iterator last ); (until C++11)
iterator erase( const_iterator first, const_iterator last ); (since C++11) (3) (constexpr since C++20)
Removes specified characters from the string.
- Removes
std::min(count, size() - index)characters starting atindex. - Removes the character at
position.
Ifpositionis not a dereferenceable iterator on*this, the behavior is undefined. π«£
- Removes the characters in the range
[first, last).
Iffirstorlastis not a valid iterator on*this, or[first, last)is not a valid range, the behavior is undefined. π
πΈ Parameters
indexβ first character to removecountβ number of characters to removepositionβ iterator to the character to removefirst, lastβ range of the characters to remove
π Return value
*this- Iterator pointing to the character immediately following the character erased, or
end()if no such character exists. - Iterator pointing to the character last pointed to before the erase, or
end()if no such character exists.
π¦ Exceptions
- 1)
std::out_of_rangeifindex > size(). - 2,3) Throws nothing.
If an exception is thrown for any reason, this function has no effect (strong exception safety guarantee). β¨
π Example
// Run this code
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
int main()
{
std::string s = "This Is An Example";
std::cout << "1) " << s << '\n';
s.erase(7, 3); // erases " An" using overload (1)
std::cout << "2) " << s << '\n';
s.erase(std::find(s.begin(), s.end(), ' ')); // erases first ' '; overload (2)
std::cout << "3) " << s << '\n';
s.erase(s.find(' ')); // trims from ' ' to the end of the string; overload (1)
std::cout << "4) " << s << '\n';
auto it = std::next(s.begin(), s.find('s')); // obtains iterator to the first 's'
s.erase(it, std::next(it, 2)); // erases "sI"; overload (3)
std::cout << "5) " << s << '\n';
}
π Output
1) This Is An Example
2) This Is Example
3) ThisIs Example
4) ThisIs
5) This
π§ Defect reports
The following behavior-changing defect reports were applied retroactively to previously published C++ standards. π
- LWG 27 β applied to C++98. Behavior as published: overload (3) did not erase the character last pointed to, but it returned the iterator pointing to the character immediately following that character. Correct behavior: returns an iterator pointing to that character.
- LWG 428 β applied to C++98. Behavior as published: overload (2) explicitly required
positionto be valid, butSequenceContainerrequires it to be dereferenceable (stricter). Correct behavior: removed the explicit requirement. - LWG 847 β applied to C++98. Behavior as published: there was no exception safety guarantee. Correct behavior: added strong exception safety guarantee.
π See also
clearβ clears the contents (public member function)
Thatβs the whole page, bestie β source/version: http://cppreference.com; date: 2024.06.10. Periodt. π
π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms