Brief Introduction to boost string algorithms library

Source: Internet
Author: User
Tags printable characters

Most of the time I wantCodeUse STD: string instead of the cstring of MS to ensure that myProgramIt will be easier to transplant in the future, but to be honest, cstring is much easier to use than STD: string, and is often tempted. Then, let's look at the string of C #. It feels better to use it. But with this library, I can basically resist the temptation...

 

# Include<Boost/Algorithm/String. HPP>

Many times I want to use STD: string instead of Ms cstring in my own code to ensure that my program will be easily transplanted in the future, but to be honest, cstring is better than STD :: string is much easier to use. It is tempting to use it. Check the string of C. But with this library, I can basically resist the temptation.

Let's take a look at some of the good things. The following directories are listed in the document (I changed the order and some basic information can be used to guess what it is, so I won't say much here)
1. trimming
2. Case Conversion
3. Replace Algorithms
4. Find iterator
5. Find Algorithms
6. predicates and Classification
7. Split
I am more interested in 6. predicates and classification and 7. Split. First, let's look at the list of functions (function names with prefix I are not sensitive to big and lowercase letters ).

6.1.predicates
Starts_with // 'starts with 'Predicate
Istarts_with // 'starts' predicate (Case Insensitive)
Ends_with
Iends_with
Contains
Icontains
Equals
Iequals
All
6.2.classification
Classifier assertions are added to the database mainly to facilitate the use of algorithms trim () and all () :-). In fact, almost all functions of STL are available, but all are for characters.
Is_space // Space
Is_alnum // letters and numbers
Is_alpha // letter
Is_cntrl // Control Character
Is_digit // number
Is_graph // printable characters (excluding spaces)
Is_lower // lower case
Is_print // printable characters (including spaces)
Is_punct // punctuation
Is_upper // uppercase
Is_xdigit // hexadecimal number
Is_any_of //
...

Example:

  String Str =   " Boost_1_32_0.rar " ;
Cout < Str
< (Boost: algorithm: ends_with (STR, " . Rar " ) ?   " Yes " : " No " ) <   " Rarfile "   < Endl;


Example:

  Char Text [] = " Hello world! " ;
Cout
< Text
< (ALL (text, is_lower ()) ?   " Is " : " Is not " )
<   " Written in the lower case "
< Endl;
// Prints "Hello world! Is not written in the lower case"


7. Split
Find_all
Ifind_all
Split

Split is a feature that I like very much. String in C # has this function. Below is a piece of C # code in msdn.

String Words =   " This is a list of words, with: A bit of punctuation. " ;
String [] Split = Words. Split ( New Char [] {' ',',','.',':'} );
Foreach ( String S In Split)
{
If(S. Trim ()! = "")
Console. writeline (s );
}

 
It feels good to use. Now we can use this library.

String Words =   " This is a list of words, with: A bit of punctuation. " ;
Vector < String > Splitvec;
Split (splitvec, words, is_any_of ( " ,.: " ));
For ( Int I = 0 ; I < ( Int ) Splitvec. Size (); I ++ )
{
If(Trim_copy (splitvec [I])! = "")//Note that the space is removed. Examples in this document are not described.
Cout<Splitvec [I]<Endl;
}

I think it's similar to the above. Oh, it's good to write like this.

String Words =   " This is a list of words, with: A bit of punctuation. " ;
Vector < String > Splitvec;
Boost: Array < Char , 4 > Separator =   {' ',',','.',':'} ;
// Char separator [4] = {'', '.', ':'};
Split (splitvec, words, is_any_of (separator ));
For (Vector < String > : Iterator ITER = Splitvec. Begin (); ITER ! = Splitvec. End (); ++ ITER)
{
If(!All (*ITER, is_space ()))
Cout< *ITER<Endl;
}

here, we can say that all the basic operations are available and easy to use. It's just because it's not put together with STD: string, it's a little troublesome to use, but with namespace, IDE can help you effectively now, if you use Vi, you can also talk about it.
we have seen many features before, but we have not seen an important string operation, that is, the commonly used format method boost in cstring also provides one, but it feels abnormal. The next article will introduce it.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.