Learning boost 3
String Algorithm 1
Introduction
String Algorithm is a class library that provides a string algorithm in Boost. Here, the string does not necessarily be std :: Basic_String, or other STL container or zero tail string char * in C .
example:
Std :: string Str ("Hello");
Std :: vector
Char * cstr = new char (Str.size ());
Std :: Copy (Str.Begin (), Str.End (), CSTR);
Boost :: TO_UPPER (STR); // String
Boost :: TO_UPPER (VSTR); // Vector
Boost :: TO_UPPER (CSTR); // Char *
In addition, many algorithms in String Algorithm are divided into Mutable and Copy, the Mutable algorithm directly modify the input string, and the Copy algorithm returns a new string or outputs it through an iterator.
In String Algorithm, ITORRRANGE concept, this is a class in Boost :: Range, and I will not use it, but you can use make_iterator_range (iterator, itemarator) to construct. Iterator_Range represents a range of ranges through two start and end iterators, generally obtained by the return algorithm.
example:
Std :: string str1 ("hello");
Std :: string str2 ("hello");
Boost :: to_upper (str1); // mutable algorithm, now str1 == "Hello"
Std :: string str3 = boost :: to_upper_copy (str2); // Copy Algorithm, str3 == "Hello"
/ *
Char * p = boost :: to_upper_copy ("Hello");
This is wrong. Because To_upper_copy will not apply for a copy of the memory to save the "Hello" copy.
Char str [] = {"Hello"};
TO_UPPER (STR); this is fine.
* /
Std :: string str4;
Boost :: TO_UPPER_COPY (std :: back_inserter (STR4),
Std :: make_pair (str2.begin (), str2.end ())
); // use iterators Copy Algorithm, str4 == "Hello"
/ / Hereby here, here and the STL is the output iterator in front of the input iterator
Boost :: TO_UPPER_COPY (std :: back_inserter (str4), "hello"); str4 == "Hellohello"
// This is also possible, I have said iteratorRange is equivalent to a container.
// STL standard container, std :: basic_string and char * can be used as containers in String_Algorithm
Case Conversion Algorithm Size Change Algorithm
Void to_Lower (MutableCollection &, Std :: local & = std :: local ());
Void to_upper (MutableCollection &, Std :: local & = std:: local ()); sequence to_lower_copy (const sequence &, std :: local);
Sequence to_upper_copy (const sequence &, std :: local & = std :: local ());
Outputiterator to_lower_copy (OutputIterator, Const Collection &, Std :: local & = std :: local ());
Outputiterator to_lower_copy (OutputIterator, Const Collection &, Std :: local & = std :: local ());
In String Algorithm, Collection refers to:
l STL standard type
l Built in the built-in (eg int [])
l zero tail string (char * or wchar *)
l Pair
MutableCollection, of course, refers to non-constings.
Sequence seems to be a standard container.
Trim Algorithm Trim Algorithm
Void Trim (Sequence &, Std :: Local & = std :: local ());
Void Trim_left (Sequence &, Std :: Local & = std :: local ());
Void Trim_right (Sequence &, Std :: local & = std :: local ());
Sequence Trim_Copy (const sequence &, std :: local & = std :: local ());
Sequence Trim_LEFT_COPY (Const Sequence &, Std :: Local & = std :: local ());
SEQUENCE TRIM_RIGHT_COPY (const sequence &, std :: local & = std :: local ());
The above algorithm deletes spaces of left and right (or one of them) in the string.
Void TRIM_IF (SEQUENCE &, PREDICATE);
Void Trim_LEFT_IF (SEQUENCE &, PREDICATE);
Void TRIM_RIGHT_IF (SEQUENCE &, PREDICATE);
SEQUENCE TRIM_COPY_IF (Const Sequence &, Predicate);
SEQUENCE TRIM_LEFT_COPY_IF (Const Sequence &, Predicate);
SEQUENCE TRIM_RIGHT_COPY_IF (Const Sequence &, Predicate);
Outputiterator Trim_copy_IF (OutputITerator, Const Collection &, Predicate);
Outputiterator Trim_LEFT_COPY_IF (Outputiterator, Const Collection &, Predicate);
Outputiterator Trim_right_copy_IF (OutputITerator, Const Collection &, Predicate); The above algorithm deletes the string (or one of them) satisfying the Predicate character.
Predicate is a one-dollar judgment.
example:
String Str ("--- Hello ---");
Boost :: Trim_if (Str, Bind2nd (Equal_to
PREDICATE judgment
Some judgment related to strings are provided in String Algorithm.
Bool Starts_with (Input, Test);
Bool Starts_with (Input, Test, Comp)
Bool iStarts_with (Input, Test, Local)
Decision INPUT starts with Test. CoMP is a binary judgment of comparison characters. Local is std :: local.
BOOL ENDS_WITH (Input, Test);
BOOL Ends_with (Input, Test, Comp)
BOOL IENDS_WITH (Input, Test, Local)
Decision INPUT is ending TEST. CoMP is a binary judgment of comparison characters. Local is std :: local.
Bool Contains (INPUT, TEST);
BOOL Contains (Input, Test, Comp)
Bool icontains (Input, Test, Local)
Judging whether the input contains TEST. CoMP is a binary judgment of comparison characters. Local is std :: local.
Bool Equals (INPUT, TEST);
Bool Equals (INPUT, TEST, COMP)
Bool Iequals (Input, Test, Local)
It is determined whether INPUT is the same as TEST. CoMP is a binary judgment of comparison characters. Local is std :: local.
Bool All (Input, PRED);
Determine whether each character of the INPUT meets the PRED judgment.
ClassFICATION Category judgment
ClassFICATION is also a judgment, which is a generic package such as std :: isspace and other functions.
Unspecified is_classified (std :: ctype_base :: mask, const st :: locale & = std :: locale ());
Unspecified is_space (const std :: locale & = std :: locale ();
Unspecified is_alnum (const st: locale & = std :: locale ());
Unspecified is_alpha (const st: locale & = std :: locale ();
Unspecified is_cntrl (const st: locale & = std :: locale ());
Unspecified is_digit (const std :: locale & = std :: locale ());
Unspecified is_graph (const std :: locale & = std :: locale ());
Unspecified is_lower (const st: locale & = std :: locale ());
Unspecified is_print (const st: locale & = std :: locale ()); unspect std :: locale & = std :: locale ();
Unspecified is_upper (const st: locale & = std :: locale ());
Unspecified is_xdigit (const st: locale & = std :: locale ();
The judgment is whether the character is a type, returns the judgment.
example:
Trim_copy_if (String ("123hello
890 "
, is_digit ())); // Return "Hello"
Unspecified is_any_of (set);
The judgment is whether the character is one of the set, and returns the judgment.
Unspecified is_from_range (from, to);
Decades whether the character is between from and TO (FROM <= CH <= TO).
Unspecified Operator && (PRED1, PRED2);
UNSPECified Operator || (PRED1, PRED2);
Unspecified Operator! (PRED);
ClassFICATION Judgment with or non-computational. Note: You can only use the above ClassFication to be used.
example:
Trim_copy_if (String ("123hello
890 "
), is_digit () || is_upper ()); // return "Ello"
Iterator_Range Class iterator range class
Itrator_range is a package for a set of iterators. The return value generally used for the Find algorithm.
Itrator_range has BeGin (), End (), End (), Size (), SWAP (), Bool Operator () member function similar to the STL container.
Make_iterator_range () is generally used to construct iTerator_rang, make_iterator_range uses two iterator or pair
You can also use the stream to output iTerator_Range.
example:
String Str ("Hello");
Cout << make_iterator_range (str.begin () 1, str.end () - 1); // Output "ELL"
The OSTREAM output for Iterator_Range is similar to copy (it.begin (), it.end (), ostream_iterator
No spaceless continuous output.
ERASE Algorithm Delete Algorithm
Outputiterator ERASE_RANGE_COPY (OUTPUTITERATOR, INPUT, SEARCHRANGE);
Sequence ERASE_RANGE_COPY (Input, SearchRange);
Void ERASE_RANGE (Input, SearchRange);
Delete the section of the SearchRange in the input string. SearchRange is an Iterator_Range object.
example:
Std :: string str1 ("hello");
Std :: string str2;
Boost :: ERASE_RANGE (str1, boost :: make_iterator_range (str1.begin () 1, str1.end () - 1));
COUT << str1 << endl; // output "ho" ERASE_RANGE_COPY (Back_Inserter (STR2),
STR1,
Boost :: make_iterator_range (str1.begin () 1, str1.end () - 1));
Cout << str2 << Endl; // Output "HO"
Outputiterator ERASE_FIRST_COPY (Outputiterator, INPUT, SECH);
Sequence ERASE_FIRST_COPY (Input, Search);
Void Erase_First (Input, Search);
Outputiterator inerase_first_copy (Outputiterator, Input, Search, Local);
Sequence Irase_First_copy (Input, Search, Local);
Void Ierase_First (Input, Search, Local);
Delete the first Search found from Input.
example:
Std :: string str1 ("hello");
Std :: string str2;
Boost :: Erase_first (STR1, "LL");
Str2 = boost :: ERASE_FIRST_COPY (String ("Hello"), "L");
COUT << str1 << endl; // output "heo"
Cout << str2 << Endl; // Output "Helo"
Outputiterator ERASE_LAST_COPY (Outputiterator, INPUT, SECH);
Sequence Erase_last_copy (Input, Search);
Void Erase_last (Input, Search);
Outputiterator inerase_last_copy (OutputITerator, Input, Search, Local);
Sequence Irase_Last_copy (Input, Search, Local);
Void Ierase_Last (Input, Search, Local);
Delete the last search from INPUT.
Outputiterator ERASE_NTH_COPY (Outputiterator, Input, Search, N);
Sequence ERASE_NTH_COPY (Input, Search, N);
Void Erase_Nth (Input, Search, N);
Outputiterator Ierase_NTH_COPY (Outputiterator, Input, Search, N, Local);
Sequence Irase_NTH_COPY (Input, Search, N, Local);
Void Irase_Nth (Input, Search, N, Local);
Delete the Nth Search found from Input. (N is 0-based)
Outputiterator ERASE_ALL_COPY (OUTPUTITERATOR, INPUT, SECH);
Sequence Erase_all_copy (Input, Search);
Void Erase_all (Input, Search); OutputItemrator Irase_all_copy (OutputItrator, Input, Search, local);
SEQUENCE IERASE_ALL_COPY (INPUT, Search, Local);
Void Ierase_all (Input, Search, Local);
Delete all Searches found from Input.
Outputiterator ERASE_HEAD_COPY (OutputItItor, INPUT, N);
Sequence ERASE_HEAD_COPY (INPUT, N);
Void Erase_Head (Input, N)
Delete the previous n characters of the INPUT. If n is greater than the input string length, the entire string will be deleted.
Outputiterator ERASE_TAIL_COPY (Outputiterator, INPUT, N);
Sequence ERASE_TAIL_COPY (INPUT, N);
Void ERASE_TAIL (Input, N);
Delete the last n characters of INPUT. If n is greater than the input string length, the entire string will be deleted.
Replace algorithm replacement algorithm
Outputiterator Replace_Range_copy (OutputITerator, INPUT, SearchRange, Format);
SEQUENCE REPLACE_RANGE_COPY (Input, SearchRange, Format);
Void Replace_Range (INPUT, SearchRange, Format);
Replace the SearchRange section in Input to Format.
Outputiterator Replace_First_Copy (OutputItItor, Input, Search, Format);
Sequence Replace_First_copy (Input, Search, Format);
Void Replace_First (Input, Search, Format);
Outputiterator Ireplace_First_copy (OutputItrator, Input, Search, Format, Local);
SEQUENCE IREPLACE_FIRST_COPY (Input, Search, Format, Local);
Void Ireplace_first (Input, Search, Format, Local);
Replace the first Search in INPUT to Format.
Outputiterator Replace_Last_copy (OutputItItor, Input, Search, Format);
SEQUENCE REPLACE_LAST_COPY (INPUT, Search, Format);
Void Replace_Last (Input, Search, Format);
Outputiterator Ireplace_last_copy (OutputIterator, Input, Search, Format, Local);
SEQUENCE IREPLACE_LAST_COPY (INPUT, Search, Format, Local);
Void IREPLACE_LAST (Input, Search, Format, Local); replaces the last Search searched in INPUT to Format.
Outputiterator Replace_NTH_COPY (OutputItrator, Input, Search, Format, N);
SEQUENCE REPLACE_NTH_COPY (Input, Search, Format, N);
Void Replace_ns (Input, Search, Format, N);
Outputiterator Ireplace_NTH_COPY (Outputiterator, Input, Search, Format, N, Local);
SEQUENCE IREPLACE_NTH_COPY (INPUT, Search, Format, N, Local);
Void Ireplace_ns (Input, Search, Format, N, Local);
Replace the nth Search in Input to Format.
Outputiterator Replace_all_copy (OutputItItor, INPUT, Search, Format);
SEQUENCE REPLACE_ALL_COPY (INPUT, Search, Format);
Void Replace_all (Input, Search, Format);
Outputiterator IREPLACE_ALL_COPY (Outputiterator, Input, Search, Format, Local);
SEQUENCE IREPLACE_ALL_COPY (INPUT, Search, Format, Local);
Void Ireplace_all (Input, Search, Format, Local);
Replace all Searches in Input to Format.
Outputiterator Replace_Head_copy (OutputIterator, Input, N, Format);
SEQUENCE REPLACE_HEAD_COPY (INPUT, N, FORMAT);
Void Replace_Head (Input, N, Format);
Replace the previous n characters in the Input to Format.
Outputiterator Replace_tail_copy (OutputItiTor, Input, N, Format);
SEQUENCE REPLACE_TAIL_COPY (INPUT, N, FORMAT);
Void Replace_tail (Input, N, Format);
Replace the last n characters in Input into Format.
Finder Object Finder imitation function
The Finder Object is an imitation function that is used to determine the matching strategy in the Find algorithm. Can be used in find_iterator.
Finder first_finder (Search);
Finder first_finder (Search, Comp);
Return to find the first Search's Finder object in the input.
Finder Last_finder (Search);
Finder Last_Finder (Search, Comp);
Return to find the last search for the last search.
Finder nth_finder (Search, N);
Finder nth_finder (Search, N, CoMP);
Return to find the first Search's Finder object in the input. Finder head_finder (n);
Finder tail_finder (n);
Returns the Finder object that looks for the first (post) n character in the input. (N is 0-based)
Finder token_finder (PRED, Compress = token_compress_off);
Returns the Finder object that satisfies the character of the PRED judge.
PRED is similar to BOOL PRED (CHAR C); such a function or imitation function.
Compress can take value:
Token_compress_on: combines adjacent characters that meet the PRED as a lookup result.
Token_compress_off: does not merge the adjacent character.
For example, for bind2nd (Equal_to
Finder Range_Finder (BEG, END);
Finder Range_Finder;
Return to find a specific range of finder objects.
Find algorithm
The Find algorithm is an algorithm for finding a substring in a string, and the algorithm returns the Iterator_Range type.
Iterator_Range Find (Input, Finder);
Iterator_Range Find_First (Input, Search);
Iterator_Range ifind_first (Input, Search, Local = std :: local ());
Iterator_Range Find_Last (Input, Search);
Iterator_Range ifind_last (Input, Search, Local = std :: local ());
Iterator_Range Find_NTH (Input, Search, N);
Iterator_Range ifind_ns (Input, Search, N, Local = std :: local ());
Iterator_Range Find_Head (Input, N);
Iterator_Range Find_Tail (Input, N);
Iterator_Range Find_Token (Input, PRED, Compress = token_compress_off);
The above algorithms are very simple, I will not say much.
Let me analyze the source code of Find_First, Find_First is this:
Template
Inline Iterator_Range <
Boost_string_typename result_iterator_of
Find_first (
Collection1T & INPUT,
Const Collection2T & Search)
{
Return first_finder (Search)
Begin (Input), End (Input);
}
Result_iterator_of
Begin (Input) is Input.Begin ()
End (input) is input.end ()
First_finder (Search) returns a first_finder object and then calls this Finder.operator () (BEG, END) and returns as the lookup results as the lookup result in iTerator_Range. In fact, for each Finder overloads the Operator () function, use this function to find the string directly. Find Iterator Find iterators
The Find algorithm can only find a result, and you can find all the results via Find_iterator.
Template
Find_iterator make_find_iterator (input, finder);
Find_iterator member function:
Find_iterator () default constructor
Find_iterator (BEG, End, Finder) Find between Beg and End
Find_iterator (Input, Finder) Find Input
Bool EOF () Find the end
example:
Std :: string str1 ("Ten 10 Nine 9 and Eight 8);
Boost :: Find_Iterator
For (it = make_find_iterator (str1,
Token_Finder (boost :: is_digit (),
Boost :: token_compress_on) / / Find Continued Digital Token_Finder
)
; it.eof (); IT) // Note is IT.eof (), it-> means (* it). And * IT is an Iterator_Range object
Cout << * IT << Endl; // * IT returns the results: an Iterator_Range object, and an item_Range is directly output.
// Output:
// 10
//9
//8
In the process of using the find_iterator, the length of the string cannot be modified, for example: ERASE_RANGE (str1, * it) if it is Replace.
In addition, the Find_iterator is the Forward iterator, cannot --IT.
Template
Find_iterator make_split_iterator (Input, Finder);
Split_iterator uses the strings that look up to separate the string, for example, for the above example, replace the find with Split, the result:
Ten
nine
And eleht
Splite_iterator is the same as FIND_ITERATOR.