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














































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



find_all:

     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 find all the occurences of a substring in the given input string. It stores the found substrings in a container provided by the user. Each part of the string is copied as a new element in the container. Popularly used output containers are std::vector<std::string> or std::list<boost::iterator_range<std::string::iterator>>

header file:

boost/algorithm/string.hpp

syntax:

     find_all(output_container,input_string,substring);

parameters:

    input_string : an input string

output_container : the container to hold the copies or references to the substrings

substring : the string to be searched

find_all() searches the substring case-sensitively. Another variation is present ifind_all() which searches the substring case-insensitively. Another variation is present find_all_regex() which searches the presence of substrings based on the given regular expression.

SAMPLE CODE :

#include<iostream> #include<boost/algorithm/string.hpp> #include<vector> using namespace std; using namespace boost::algorithm; int main() { //declaring the input strings string str1 = "Hi I am Hi Boost Hi Library"; string str2 = "I am very interesting"; string str3 = "I am very efficient"; //declaring the containers used for storing the resulting substrings found vector<string> v1; vector<string> v2; vector<string> v3; find_all(v1,str1,"Hi");//searching for "Hi" in str1 and storing the resultant substrings in v1 find_all(v2,str2,"interesting");//searching for "interesting" in str2 and storing the resultant substrings in v2 find_all(v3,str3,"e");//searching for "e" in str3 and storing the resultant substrings in v3 //displaying the contents of v1 for(int i=0;i<v1.size();i++) cout<<v1[i]<<" "; cout<<endl; //displaying the contents of v2 for(int i=0;i<v2.size();i++) cout<<v2[i]<<" "; cout<<endl; //number of 'e' present in str3 cout<<v3.size()<<endl; }

OUTPUT :


EXPLANATION :

First we are searching the substring "Hi" in str1. There are 3 occurences of "Hi" in str1 which are stored in v1 and are displayed. In str2 we are searching for "interesting" which occurs only once. Similarly in str3 we are searching for 'e'. We are displaying the number of times the letter 'e' occurs in str3 i.e., 3.



More Articles of Md Safi Ur Rahman Khan:

Name Views Likes
C++ boost::algorithm::string::join() 577 0
C++ boost::algorithm::string::split() 607 0
C++ boost::algorithm::string::find_all() 647 0
C++ boost::algorithm::string::erase_tail() 249 0
C++ boost::algorithm::string::replace_tail() 212 0
C++ boost::algorithm::string::erase_head() 277 0
C++ boost::algorithm::string::replace_head() 210 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() 1824 0
C++ boost::algorithm::string::ends_with() 1283 0
C++ boost::algorithm::string::starts_with() 2058 0
C++ boost::algorithm::string::trim_if() 712 1
C++ boost::algorithm::string::trim() 3797 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() 364 1
C++ boost::algorithm::string::to_lower() 737 1
C++ boost::algorithm::string::to_upper() 297 1
C++ Program to Implement Dequeue 1327 5
C++ Program to Implement Dequeue 241 3

Comments