Table 11.4. Find Algorithms
Algorithm name |
Description |
Functions |
Find_first |
Find the first occurrence of a string in the input |
Find_first ()
Ifind_first () |
Find_last |
Find the last occurrence of a string in the input |
Find_last ()
Ifind_last () |
Find_nth |
Find the Nth (zero-indexed) occurrence of a string in the input |
Find_nth ()
Ifind_nth () |
Find_head |
Retrieve the head of a string |
Find_head () |
Find_tail |
Retrieve the tail of a string |
Find_tail () |
Find_token |
Find First Matching token in the string |
Find_token () |
Find_regex |
Use the regular expression to search the string |
Find_regex () |
Find |
Generic find Algorithm |
Find () |
Basic example:
String str1 ( "A1234_first_nth_first_nth _" ) ; Boost : : Iterator_range < String : : Iterator > IR; // Find_first // Ifind_first -- case-insensitive; Other IR = Find_first ( Str1 , "First" ) ; Assert ( String ( IR . Begin ( ) , IR . End ( ) ) = = "First" ) ; // Compare strings Assert ( IR . Begin ( ) - Str1 . Begin ( ) = = 6 & & IR . End ( ) - Str1 . Begin ( ) = = 6 + 5 ) ; // View the position of the searched string To_upper ( IR ) ; // Use iterator_range to process the searched string Assert ( Str1 = = "A1234_first_nth_first_nth _" ) ; To_lower( IR ) ; Assert ( Str1 = = "A1234_first_nth_first_nth _" ) ; IR = Find_first ( Str1 , "No" ) ; Assert( IR . Empty ( ) ) ; // Does not exist Assert ( String ( IR . Begin ( ) , IR . End ( ) ) . Empty ( ) ) ; // A string can still be constructed because it does not exist. Ostringstream osstr ; Osstr < < Find_first ( Str1 , "_ First _" ) ; Assert ( Osstr . Str ( ) = = "_ First _" ) ; // Find_last -- same as findfirst, only in the opposite direction // Ifind_last -- case-insensitive. Other values are the same as those in findlast. // Find_nth IR = Find_nth ( Str1 , "Nth" , 1 ) ; Assert ( IR . Begin ( ) - Str1 . Begin ( ) = = 22 ) ; IR = Find_nth ( Str1 , "Nth" , 0 ) ; Assert ( IR . Begin ( ) - Str1 . Begin( ) = = 12 ) ; // Find_head // Find_tail is in the opposite direction IR = Find_head ( Str1 , 5 ) ; Transform ( IR. Begin ( ) , IR . End ( ) , IR . Begin ( ) , Bind2nd ( Plus < Char > ( ) , 1 ) ) ; Assert ( Str1 = = "B2345_first_nth_first_nth _" ) ; // Find_token IR = Find_token ( Str1, Is_any_of ( "Irfst" ) ) ; Assert ( String ( IR . Begin ( ) , IR . End ( ) ) = = "F" ) ; IR = Find_token ( Str1 , Is_any_of ( "Xfirts" ) , Token_compress_off ) ; Assert( String ( IR . Begin ( ) , IR . End ( ) ) = = "F" ) ; IR = Find_token ( Str1 , Is_any_of ( "Irfst" ) , Token_compress_on ) ; Assert ( String ( IR . Begin ( ) , IR. End ( ) ) = = "First" ) ; IR = Find_token ( Str1 , Is_any_of ( "Fitr" ) , Token_compress_on ) ; Assert ( String ( IR . Begin ( ) , IR . End ( ) ) = = "Fir" ) ; // Find_regex Boost : : RegEx RX ( "B [0-9] + _" ) ; IR = Find_regex ( Str1 , RX ) ; Assert ( String( IR . Begin ( ) , IR . End ( ) ) = = "B2345 _" ) ; // Not listed in find_all_regex Test Table 11.4, which is in the boost_1_33_0 \ libs \ algorithm \ string \ test directory. String str2( "Bda-b22_b333_b4444" ) ; Vector < String > Tokens ; Find_all_regex ( Tokens , Str2 , RX ) ; Assert ( Tokens . Size( ) = = 3 ) ; Assert ( Tokens [ 0 ] = = "B1 _" ) ; Assert ( Tokens[ 1 ] = = "B22 _" ) ; Assert ( Tokens [ 2 ] = = "B333 _" ) ; // Find IR = Find ( Str1 , First_finder ( String ( "B2345 _" ) ) ) ; Assert ( String ( IR . Begin ( ) , IR . End ( ) ) = = "B2345 _" ) ; ;