Boost string Handler -- string algorithm

Source: Internet
Author: User
Tags types of functions

C ++ provides a string class in the stl library to replace the char * of c language to implement the string function. However, stl strings only provide a connection string and search function, almost none of other common functions are available. Even string replacement is implemented by itself, which is worse than string functions in c.

The boost library is in the header file <boost/algorithm/string. hpp> provides many string processing functions to help us implement basic string processing functions, greatly alleviating the problem of insufficient use of string functions.

String str1 ("hello abc-*-ABC-*-aBc goodbye ");
Vector <string> SplitVec; // #2: Search for tokens
Split (SplitVec, str1, is_any_of ("-*"), token_compress_on); // SplitVec =={ "hello abc", "ABC", "aBc goodbye "}

The above Code provides a split function, but it is not concise enough compared with the c # version. If it is designed to be better:

Auto SplitVec = split (str1, is_any_of ("-*"), token_compress_on );

Not designed in this way, it seems that if the return value is constructed in the split function, there will be an overhead of data copying. We can also see that the boost design focuses on performance. The. net program does not have the overhead brought about by the copy of the returned value, so it can adopt a more concise interface form.

PS: I think it can be like. net,Save results in heap VariablesAnd use auto_ptr as the returned value. There is a simple interface, and there is no overhead of data copying, heap variables can also be automatically released through auto_ptr.

Similarly, for the sake of performance, functions that generate new strings usually have two versions: xxx and xxx_copy. For example, tolower has two versions: to_lower and to_lower_copy: one is to directly change its own value, and the other is to construct a new string, which is selected based on the actual scenario.

In addition, string comparison functions often have case-sensitive and case-insensitive algorithms. In boost, It is not controlled by parameters, but directly provides the xxx and ixxx versions, where I indicates ignor case. Such as equals and iequals. Some other functions need to be used together with the conditional functions, and a version of xxx_if will be provided.

Because many string functions are provided, I only list several common functions and examples for each type. If the same algorithm has the xxx_copy version or the ixxx version, only the basic form is listed. For other information, see the boost documentation.

Case Conversion

Case-sensitivity conversion involves four functions: to_upper (), to_upper (), and xxx_copy versions. The basic usage is as follows:

Cout <to_upper_copy (string ("hello world") <endl;

Trimming

Trimming functions include trim (), trim_left (), trim_right (), and their xxx_copy and xxx_if versions. Removes spaces at the top of a string:

Cout <trim_copy (std: string ("hello world") <endl;

Of course, it is not limited to removing only white spaces:

Cout <trim_copy_if (std: string (", hello world"), is_any_of (",.:") <endl;

Predicates

Predicates functions include starts_with (), ends_with (), contains (), equals (), lexicographical_compare (), all (), and their ixxx versions. Basically, we can see from the name how to use it:

Cout <(ends_with ("hello world", "world ")? "True": "false") <endl;

Find algorithms

There are several search algorithms: find (), first (), find_last (), find_nth (), find_head (), find_tail (), find_token (), and find_regex (). Common usage:

Auto result = find_first ("hello world", "world ");
If (result. empty ())
Cout <"can't find result" <endl;
Else
Cout <result <endl;

The result is a boost: iterator_range object, which can be used to construct a substring.

String s ("hello world ");
Cout <s. substr (find_first (s, "wo"). begin ()-s. begin () <endl;

Or a more complex point:

String str1 ("abc-*-ABC-*-aBc ");
For (auto it = make_find_iterator (str1, first_finder ("abc", is_iequal ()));! It. eof (); ++ it)
{
Cout <copy_range <std: string> (* it) <endl;
}

Erase/Replace

Boost separates erase and replace functions. The advantage of this is that the names are clear, but when there are too many functions in poor cases, it is better to remember the form of overload.

Replace functions include replace_all (), replace_first (), replace_last (), and their variants. In addition to erase, there are more than 20 types of functions, which are not listed here.

Cout <replace_all_copy (string ("hello world"), "l", "-") <endl;

Split

The usage of the split function is described earlier:

String str1 ("hello abc-*-ABC-*-aBc goodbye ");
Vector <string> SplitVec;

Split (SplitVec, str1, is_any_of ("-*"), token_compress_on );

Note that the token_compress_on parameter is used here. It can be used as one of multiple consecutive delimiters. It is not enabled by default and is usually enabled when used.

In addition, boost divides the find_all function into the split class, and their usage is similar.

String str1 ("hello abc-*-ABC-*-aBc goodbye ");
Typedef
Vector <iterator_range <string: iterator> find_vector_type;
Find_vector_type FindVec;
Ifind_all (FindVec, str1, "abc ");

Join

The Join function is opposite to split and is used to concatenate multiple strings.

Std: array <string, 3> k = {"hello", "world", "123 "};
Cout <join (k, "-"); // The output result is hello-world-123.

It requires that the parameter be put in the container First, unlike. net can directly pass in dynamic parameters as easy to use, it would be better if you can write such a heavy load form (of course, it is not difficult to block one by yourself ):

Join ("-", "hello", "world", "123 ",...)

Others

Other conditional functions are mainly used in combination with algorithms. For example, is_any_of (), is_upper (), is_lower (), is_digit (), and is_space () that have been seen earlier are not described here.

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.