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 replace the tail of the input string with a new pattern. It first identifies the tail by taking last N characters , where N is given by the user. It then proceeds to replace the last N characters with the new format string given by the user.
input_string : an input string
N : An integer which specifies the number of characters to be considered as tail. i.e.,
format_string : the pattern that replaces the head.
(For N>=0, at most N characters are extracted. For N<0, size(Input)-|N| characters are extracted.)
replace_tail() performs an inplace replacement. There is another variation present replace_tail_copy() which instead of doing an inplace replacement, returns a copy of the modified string.
using namespace std;
using namespace boost::algorithm;
string str1 = "Hello and Welcome to STL";
string str2 = "Apple Banana Mango Apple Banana Apple";
string str4 = "Hi Hi Hi Hi Hi Hi";
string str3 = replace_tail_copy(str4,2,"Guys ");
As we can observe from the output we are first performing an inplace replacement where we are replacing the last 3 characters of the input string i.e., "STL" with "Boost ". In str3, we are storing the copy of the modified string i.e., inplace replacement is not preformed.