Is the behaviour of std::string::erase(0)
is well defined on an empty string. Because cppreference says:
Removes count characters starting at index.
But for an empty string, the character at index 0 does not exist.
Method #1: Using remove() This particular method is quite naive and not recommended use, but is indeed a method to perform this task. remove() generally removes the first occurrence of an empty string and we keep iterating this process until no empty string is found in list.
The C++ string library allows you to erase a part of a string using the erase() function.
std::string::empty Returns whether the string is empty (i.e. whether its length is 0). This function does not modify the value of the string in any way.
It seems to be OK, since the size of the string is 0
:
21.4.6.5 basic_string::erase [string::erase]
basic_string<charT,traits,Allocator>& erase(size_type pos = 0, size_type n = npos);
1 Requires:
pos <= size()
2 Throws: out_of_range if
pos > size()
.
On the same page of std::string::erase
I found these lines:
Exceptions
1) std::out_of_range ifindex > size()
.
2-3) (none)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With