Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

string::erase(0) on an empty string?

Tags:

c++

string

erase

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.

like image 501
Vincent Avatar asked Jul 20 '13 13:07

Vincent


People also ask

How do you remove Blank values from a string?

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.

How do you delete part of a string in C++?

The C++ string library allows you to erase a part of a string using the erase() function.

Can std::string be empty?

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.


2 Answers

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().

like image 166
juanchopanza Avatar answered Oct 17 '22 14:10

juanchopanza


On the same page of std::string::erase I found these lines:

Exceptions
1)     std::out_of_range if index > size().
2-3) (none)

like image 4
Anand Rathi Avatar answered Oct 17 '22 14:10

Anand Rathi