C++ boost::algorithm::string::erase_head()














































C++ boost::algorithm::string::erase_head()



erase_head():

     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 remove the head of the input string. It first identifies the head by taking first N characters , where N is given by the user. It then proceeds to remove the head of the input string.

header file:

boost/algorithm/string.hpp

syntax:

     erase_head(input_string,N);

parameters:

    input_string : an input string

N : An integer which specifies the number of characters to be considered as head.

(For N>=0, at most N characters are extracted. For N<0, size(Input)-|N| characters are extracted.)

erase_head() performs an inplace removal. There is another variation present erase_head_copy() which instead of doing an inplace removal, returns a copy of the modified string.

SAMPLE CODE:

#include<iostream>
#include<boost/algorithm/string.hpp>

using namespace std;
using namespace boost::algorithm;

int main()
{
string str1 = "Hello and Welcome to Boost";
cout<<str1<<endl;
erase_head(str1,
5);//removing "Hello" inplace with "Hi"
cout<<str1<<endl<<endl;

string str2 = "Apple Banana Mango Apple Banana Apple";
cout<<str2<<endl;
erase_head(str2,
12);//removing "Apple Banana" inplace with "Orange"
cout<<str2<<endl<<endl;

string str4 = "Hi Hi Hi Hi Hi Hi";
cout<<str4<<endl;
string str3 = erase_head_copy(str4,3);//removing "Hi " by returning a copy
cout<<str3<<endl;
}

OUTPUT:

EXPLANATION:

As we can observe from the output we are first performing an inplace removal where we are removing the first 5 characters of the input string i.e., "Hello". In str3, we are storing the copy of the modified string i.e., inplace removal is not preformed.



More Articles of Md Safi Ur Rahman Khan:

Name Views Likes
C++ boost::algorithm::string::join() 576 0
C++ boost::algorithm::string::split() 606 0
C++ boost::algorithm::string::find_all() 646 0
C++ boost::algorithm::string::erase_tail() 248 0
C++ boost::algorithm::string::replace_tail() 211 0
C++ boost::algorithm::string::erase_head() 277 0
C++ boost::algorithm::string::replace_head() 209 0
C++ boost::algorithm::string::erase_all() 895 1
C++ boost::algorithm::replace_all() 2049 0
C++ boost::algorithm::string::erase_nth() 215 0
C++ boost::algorithm::string::replace_nth() 221 0
C++ boost::algorithm::string::replace_last() 246 0
C++ boost::algorithm::string::erase_last() 243 0
C++ boost::algorithm::string::erase_first() 215 1
C++ boost::algorithm::string::replace_first() 432 0
C++ boost::algorithm::string::find_token() 351 0
C++ boost::algorithm::string::find_tail() 200 1
C++ boost::algorithm::string::find_head() 212 0
C++ boost::algorithm::string::find_last() 300 1
C++ boost::algorithm::string::find_first() 749 1
C++ boost::algorithm::string::all() 224 1
C++ boost::algorithm::string::lexicographical_compare() 214 0
C++ boost::algorithm::string::equals() 378 0
C++ boost::algorithm::string::contains() 1821 0
C++ boost::algorithm::string::ends_with() 1282 0
C++ boost::algorithm::string::starts_with() 2058 0
C++ boost::algorithm::string::trim_if() 712 1
C++ boost::algorithm::string::trim() 3796 0
C++ boost::algorithm::string::trim_right_if() 522 1
C++ boost::algorithm::string::trim_left_if() 384 1
C++ boost::algorithm::string::trim_right() 282 3
C++ boost::algorithm::string::trim_left() 363 1
C++ boost::algorithm::string::to_lower() 736 1
C++ boost::algorithm::string::to_upper() 296 1
C++ Program to Implement Dequeue 1327 5
C++ Program to Implement Dequeue 241 3

Comments