This function is included in the "boost/algorithm/string" library. This library contains some brilliant methods which help in accomplishing string manipulations that are lacking in STL library.
This function is used to erase the last instance of the specified pattern in the input string. It first performs a search for the specified pattern to find its last occurence and then removes it.
input_string : an input string
seacrh_string : the pattern that has to be erased from the input_string;
erase_last() performs an inplace removal. There is another variation present erase_last_copy() which instead of doing an inplace removal, returns a copy of the modified string.
erase_last() performs a case-sensistive search. There is another variation present ierase_last() which performs a case-insensitive search. Similarly ireplace_last_copy() returns a copy of modified string instead of doing an inplace removal.
using namespace std;
using namespace boost::algorithm;
string str1 = "Hello and Welcome Hello and Welcome";
string str2 = "Apple Banana Mango Banana Apple";
string str3 = erase_last_copy(str1,"and ");
As we can observe from the output we are first performing an inplace removal where we are removing the last occurence of "Hello " . Next we are performing removal on the original string while performing the search case-insensitively. In str3, we are storing the string obtained after performing removal while searching case-sensitively.