Search code examples
c++stringerase

what does C++ string erase return *this mean?


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?


Solution

  • 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";