Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lint prefers erase() method on std:string rather than clear()

Tags:

c++

string

std

Using Lint it repeatedly objects to using clear on a std string and wants to suggest using the erase method with no parameters.

Looking at the documentation both with do what I desire and that is to resize the string to zero elements so that the string is empty with no elements. I do not want to delete the string at this point simply remove all members from it.

My question is what is the difference between the two methods and does anyone know the reason I SHOULD be using erase over the clear method ?

like image 388
JamesD Avatar asked Mar 03 '15 13:03

JamesD


People also ask

Can we clear a string in C++?

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

How do I remove a single character from a string in C++?

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.

What does erase function do in C++?

All elements are destroyed one by one. erase() function is used to remove elements from a container from the specified position or range. Syntax : 1.

What is the difference between string erase () and end ()?

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. string str ("Hello World!"); Before erase : Hello World! After erase : Hell World! 5.

What is the use of erasing a string in Python?

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 string str ("Hello World!"); Before erase : Hello World!

How do you erase a string in C++?

std::string::erase in C++. The function erases a part of the string content, shortening the length of the string. The characters affected depend on the member function version used: Return value : erase() returns *this.

How do I stop lint from spitting out errors?

If you want to still show these messages and leave side effects (not recommended), remember to use the ‘no-console’ comment to avoid lint from spitting out an error; The lint message: error Missing an explicit type attribute for button react/button-has-type.


1 Answers

I cannot see a single reason for this. Quite the opposite: I actually had to look into the reference to see whether erase() can be called without arguments. erase() is used to remove some specified characters from the string. clear() is used to remove the entire contents of the string (but not its capacity).

The general programming rule is "write what you mean." If you want to clear the entire string, use the function for it: clear().

Furthermore, in the general case, erase() has more work to do—it must check that the index is in range, and throw an exception if not. clear() is guaranteed non-throwing. Clever optimisers might make them identical, but I would consider using erase() instead of clear() "premature" pessimisation.

like image 53
Angew is no longer proud of SO Avatar answered Sep 18 '22 05:09

Angew is no longer proud of SO