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














































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



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 check whether each character of the given string satisfies the given predicate or not. It starts from the first character of the string and checks using the user defined predicate function.

It is the responsibility of the user to explicitly define the predicate function which returns a boolean value.

header file:

boost/algorithm/string.hpp

syntax:

     all(input,predicate);

parameters:

    input : an input string

    predicate : the predicate function which is checked for each character.

The function all() returns 1 if all the characters satisfy the predicate. Else returns 0.

Example:

The predicate function returns 1 if the the given input character is either 'a' and 'b' .

Then all('aaabbab',func) returns 1.

Then all('aabcabba',func) returns 0 (because of the presence of character 'c' in the input string.

SAMPLE CODE:

#include<iostream> #include<boost/algorithm/string.hpp> using namespace std; using namespace boost::algorithm; //predicate function used to check if the given character is 'a' or not bool checka(char c) { return c == 'a'; } //predicate function used to check whether the given character is lexicographically less than 'w' or not bool check(char c) { return c < 'w'; } int main() { string s1 = "apple"; string s2 = "wxyz"; string s3 = "abcdef"; string s4 = "aaaaa"; cout<<"applying check on "<<s1<<" : "<<all(s1,check)<<endl; cout<<"applying check on "<<s2<<" : "<<all(s2,check)<<endl; cout<<"applying check on "<<s3<<" : "<<all(s3,check)<<endl; cout<<"applying checka on "<<s4<<" : "<<all(s4,checka)<<endl; }

OUTPUT:

EXPLANATION:

As we can observe from the above output that the function all(input,check) returns 1 for strings s1 and s2 as all the character are lexicographically less than 'w' . Whereas s3 returns 0 as it has characters greater than 'w' .

Similarly all(input,checka) returns 1 for s4 as all the characters are 'a'.


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() 225 1
C++ boost::algorithm::string::lexicographical_compare() 214 0
C++ boost::algorithm::string::equals() 378 0
C++ boost::algorithm::string::contains() 1825 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() 385 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