So the C++ string function
string& erase ( size_t pos = 0, size_t n = npos )
returns *this
. What does that mean? Why do I need it to return anything?
Example
string name = "jimmy";
name.erase(0,1);
will erase j
and become immy
, but why do I need it to return anything at all?
For method chaining. For example, after you erase, you can call ==
on it to check something:
string name = "jimmy";
bool b = name.erase(0,1) == "immy";
It is only for convenience, for example you can chain calls like this:
name.erase(0,1).erase(3,1);
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