Boost: algorithm Overview
Boost: algorithm provides many character string algorithms, including case-insensitive conversion, removing invalid characters, predicates, search, delete/replace, cutting, and connection; we use examples to understand what boost: What algorithm can do for us.
Boost: algorithm Learning
# Include <boost/algorithm/string. HPP>
Using namespace STD;
Using namespace boost;
I. Case sensitivity Conversion
1To_upper ()Convert string to uppercase
Example:
String str1 ("Hello world! ");
To_upper (str1); // str1 = "Hello world! "
2To_upper_copy ()Converts a string to uppercase and assigns a value to another string.
Example:
String str1 ("Hello world! ");
String str2;
Str2 = to_upper_copy (str1); // str2 = "Hello world! "
3To_lower ()Convert string to lowercase
Example: see to_upper ()
4To_lower_copy ()Converts a string to lowercase and assigns a value to another string.
Example: see to_upper_copy ()
Ii. trimming)
1Trim_left ()Remove spaces starting with a string
Example:
String str1 ("Hello world! ");
Trim_left (str1); // str1 = "Hello world! "
2Trim_left_if ()Remove the specific characters starting with the string that match the "predicate" we provide.
Example:
Bool noth (const char & Ch)
{
If (CH = ''| CH = 'H ')
Return true;
Else
Return false;
}
....
String str1 ("Hello world! ");
Trim_left_if (str1, noth); // str1 = "ello world! "
3Trim_left_copy ()Removes spaces starting with a string and assigns a value to another string.
Example:
String str1 ("Hello world! ");
String str2;
Str2 = trim_left_copy (str1); // str2 = "Hello world! "
4Trim_left_copy_if ()Remove the specific characters starting with the string that match the provided "predicate" and assign the value to another string.
Example:
String str1 ("Hello world! ");
String str2;
Str2 = trim_left_copy_if (str1, noth); // str2 = "ello world! "
// Remove the spaces at the end of the string. For example, see the preceding section.
5Trim_right_copy_if ()
6Trim_right_if ()
7Trim_right_copy ()
8Trim_right ()
// Remove the spaces at the beginning and end of the string. For example, see the preceding section.
9Trim_copy_if ()
10Trim_if ()
11Trim_copy ()
12Trim ()
Iii. predicates
1Starts_with ()Determines whether a string is the start string of another string.
Example:
String str1 ("Hello world! ");
String str2 ("hello ");
Bool result = starts_with (str1, str2); // result = true
2Istarts_with ()Determine whether a string is the start string of another string (case-insensitive)
Example:
String str1 ("Hello world! ");
String str2 ("hello ");
Bool result = istarts_with (str1, str2); // result = true
3Ends_with ()Judge whether a string is the end string of another string
4Iends_with ()Judge whether a string is the end string of another string (case-insensitive)
5Contains ()Determines whether a string contains another string.
Example:
String str1 ("Hello world! ");
String str2 ("llo ");
Bool result = contains (str1, str2); // result = true
6Icontains ()Determines whether a string contains another string (case-insensitive)
7Equals ()Determine whether two strings are equal
8Iequals ()Determine whether two strings are equal (Case Insensitive)
9Lexicographical_compare ()Sort by dictionary. If the first string is smaller than the second string, true is returned (My boost1.33 is not implemented ?)
10Ilexicographical_compare ()Sort by dictionary. If the first string is smaller than the second string, true (case-insensitive) is returned (My boost1.33 is not implemented?
)
11All ()Determines whether all characters in the string satisfy the predicate.
Example:
Bool is_123digit (const char & Ch)
{
If (CH = '1' | CH = '2' | CH = '3 ')
Return true;
Else
Return false;
}
...
String str1 ("12332211 ");
Bool result = All (str1, is_123digit); // result = true
Str1 = "412332211 ";
Result = All (str1, is_123digit); // result = false
4. Search
1Find_first ()Returns the iterator_range iterator in the original string.
Example:
Char toupper (char & Ch)
Char toupper (char & Ch)
{
If (CH <= 'Z' & ch> = 'A ')
Return CH + 'a'-'A ';
Else
Return ch;
}
...
String str1 ("Hello Dolly! ");
Iterator_range <string: iterator> result = find_first (str1, "ll ");
Transform (result. Begin (), result. End (), result. Begin (), toupper); // str1 = "Hello Dolly! "
2Ifind_first ()Search for the substring in the string from the beginning and return the iterator_range iterator (case-insensitive) in the original substring)
3Find_last ()Returns the iterator_range iterator in the original string.
4Ifind_last ()Find the substring in the string from the end, and return the iterator_range iterator (case-insensitive) of the substring in the original string)
5Find_nth ()Locate the nth matched substring (starts from 0)
Example:
String str1 ("Hello Dolly! ");
Iterator_range <string: iterator> result = find_nth (str1, "ll", 1 );
Transform (result. Begin (), result. End (), result. Begin (), toupper); // str1 = "Hello Dolly! "
6Ifind_nth ()Locate the nth matched substring (starts from 0) (Case Insensitive)
7Find_head ()Locate the first n Bytes of the string
Example:
String str1 ("Hello Dolly! ");
Iterator_range <string: iterator> result = find_head (str1, 5 );
Transform (result. Begin (), result. End (), result. Begin (), toupper); // str1 = "Hello Dolly! "
8Find_tail ()Find the last n Bytes of the string
9Find_token ()Locate the predicate string
Example:
Char Add1 (const char & Ch)
{
Return CH + 1;
}
...
String str1 ("Hello 1 world! ");
Iterator_range <string: iterator> result = find_token (str1, is_123digit );
Transform (result. Begin (), result. End (), result. Begin (), Add1); // str1 = "Hello 2 World! ");
10Find_regex ()Match Regular Expression
Example: (wait a moment before learning about the boost regular expression)
11Find ()Use a self-written search function
Example:
Iterator_range <string: iterator>
Myfinder1 (STD: String: iterator begin, STD: String: iterator end)
{
STD: String: iterator itr;
For (itr = begin; itr! = End; itr ++)
{
If (* itr) = '1 ')
{
STD: String: iterator preitr = itr;
Iterator_range <string: iterator> RET (preitr, ++ itr );
Return ret;
}
}
Return iterator_range <string: iterator> ();
} // Boost also provides a lot of Finder
...
String str1 ("Hello 1 world! ");
Iterator_range <string: iterator> result = find (str1, myfinder1 );
Transform (result. Begin (), result. End (), result. Begin (), Add1); // str1 = "Hello 2 World! ");
5. delete/replace
1Replace_first ()Find the first matched string from the beginning and replace it with another given string.
Example:
String str1 ("Hello world! ");
Replace_first (str1, "hello", "hello"); // str1 = "Hello world! "
2Replace_first_copy ()Find the first matched string from the beginning, replace it with another given string, and assign
Value to another string
Example:
String str1 ("Hello world! ");
String str2;
Str2 = replace_first_copy (str1, "hello", "hello"); // str2 = "Hello world! "
3Ireplace_first ()Find the first matched string from the beginning and replace it with another given string (case-insensitive
)
4Ireplace_first_copy ()Find the first matched string from the beginning, replace it with another given string, and assign
Value to another string (Case Insensitive)
5Erase_first ()Find the first matched string from the beginning and delete it
Example:
String str1 ("Hello world! ");
Erase_first (str1, "llo"); // str1 = "he world! "
6Erase_first_copy ()Find the first matched string from the beginning, delete it, and assign it to another string
Example:
String str1 ("Hello world! ");
String str2;
Str2 = erase_first_copy (str1, "llo"); // str2 = "he world! "
7Ierase_first ()Locate the first matched string from the beginning and delete it (case-insensitive)
8 ierase_first_copy () locate the first matched string from the beginning, delete it, and assign the value to another string (not big
Lowercase)
// Similar to the above, but it is replaced from the end of the string
9Replace_last ()
10Replace_last_copy ()
11Ireplace_last ()
12Ireplace_last_copy ()
13Erase_last ()
14Erase_last_copy ()
15Ierase_last ()
16Ierase_last_copy ()
// Similar to the above, but it is replaced by N matching strings.
17Replace_nth ()
Example:
String str1 ("Hello world! ");
Replace_nth (str1, "O", 1, "O"); // str1 = "Hello world! "
18Replace_nth_copy ()
19Ireplace_nth ()
20Ireplace_nth_copy ()
21Erase_nth ()
22Erase_nth_copy ()
23Ierase_nth ()
24Ierase_nth_copy ()
// Similar to the above, but replaces all matching strings
25Replace_all ()
26Replace_all_copy ()
27Ireplace_all ()
28Ireplace_all_copy ()
29Erase_all ()
30Erase_all_copy ()
31Ierase_all ()
32Ierase_all_copy ()
33Replace_head ()Replace the first n characters
Example:
String str1 ("Hello world! ");
Replace_head (str1, 5, "hello"); // str1 = "Hello world! "
34Replace_head_copy ()Replace the first n characters and assign them to another string.
Example:
String str1 ("Hello world! ");
String str2;
Str2 = replace_head_copy (str1, 5, "hello"); // str2 = "Hello world! "
35Erase_head ()Delete the first n characters
Example:
String str1 ("Hello world! ");
Erase_head (str1, 5); // str1 = "world! "
36Erase_head_copy ()Delete the first n characters and assign them to another string.
Example:
String str1 ("Hello world! ");
String str2;
Str2 = erase_head_copy (str1, 5); // str2 = "world! "
// Similar to the above (replace/Delete the last n characters)
37Replace_tail ()
38Replace_tail_copy ()
39Erase_tail ()
40Erase_tail_copy ()
// Related to the regular expression. Learn more later.
41Replace_regex ()
42Replace_regex_copy ()
43Erase_regex ()
44Erase_regex_copy ()
45Replace_all_regex ()
46Replace_all_regex_copy ()
47Erase_all_regex ()
48Erase_all_regex_copy ()
// Not very clear. Learn more later
49Find_format ()
50Find_format_copy ()
51Find_format_all ()
52Find_format_all_copy ()
Vi. Cutting
1Find_all ()Search for all matched values and place these values in the specified container.
Example:
String str1 ("Hello world! ");
STD: vector <STD: String> result;
Find_all (result, str1, "L"); // result = [3] ("L", "L", "L ")
2Ifind_all ()Search for all matching values and place these values in the given container (case-insensitive)
3Find_all_regex ()Related to regular expressions. Learn more later
4Split ()Cut the string according to the given predicate and put the cut value into the given container.
Example:
Class splitnotthischar
{
Public:
Splitnotthischar (const char ch): m_char (CH ){}
Bool operator () (const char & Ch) const
{
If (CH = m_char)
Return true;
Else
Return false;
}
PRIVATE:
Char m_char;
};
String str1 ("Hello world! ");
String str2;
STD: vector <STD: String> result;
Split (result, str1, splitnotthischar ('l'); // result = [4] ("he", "", "O wor", "d! ")
5Split_regex ()Related to regular expressions. Learn more later
6Iter_find ()Search for strings Based on the given finder and put the searched values into the given container.
Example:
String str1 ("Hello world! ");
STD: vector <STD: String> result;
// First_finder is the built-in boost finder.
Iter_find (result, str1, first_finder ("L"); // result = [3] ("L", "L", "L ")
7Iter_split ()Cut the string according to the given finder and put the cut value into the given container.
Example:
String str1 ("Hello world! ");
STD: vector <STD: String> result;
Iter_split (result, str1, first_finder ("L"); // result = [4] ("he", "", "O wor", "d! ")