BOOST string SEARCH example

Source: Internet
Author: User

BOOST string SEARCH example
[Python]
# Include <string>
# Include <iostream>
# Include <algorithm>
# Include <functional>
# Include <boost/algorithm/string/case_conv.hpp>
# Include <boost/algorithm/string/find. hpp>
 
Using namespace std;
Using namespace boost;
 
Int main () www.2cto.com
{
Cout <"* Find Example *" <endl;
 
String str1 ("abc ___ cde ___ efg ");
String str2 ("abc ");
 
// Find "cde" substring
Iterator_range <string: iterator> range = find_first (str1, string ("cde "));
 
// Convert a substring to upper case
// Note that iterator range can be directly passed to the algorithm
To_upper (range );
 
Cout <"str1 with upper-cased part matching cde:" <str1 <endl;
 
// Get a head of the string
Iterator_range <string: iterator> head = find_head (str1, 3 );
Cout <"head (3) of the str1:" <string (head. begin (), head. end () <endl;
 
// Get the tail
Head = find_tail (str2, 5 );
Cout <"tail (5) of the str2:" <string (head. begin (), head. end () <endl;
 
// Char processing
Char text [] = "hello dolly! ";
Iterator_range <char *> crange = find_last (text, "ll ");
 
// Transform the range (add 1)
Transform (crange. begin (), crange. end (), crange. begin (), bind2nd (plus <char> (), 1 ));
// Uppercase the range
To_upper (crange );
 
Cout <text <endl;
 
Cout <endl;
 
Return 0;
}


Use of boost judgment Functions
[Python]
# Include <string>
# Include <iostream>
# Include <functional>
# Include <boost/algorithm/string/predicate. hpp>
# Include <boost/algorithm/string/classification. hpp>
# Include <boost/bind. hpp>
 
 
Using namespace std;
Using namespace boost;
 
Int main ()
{
Cout <"* Predicate Example *" <endl;
 
String str1 ("123xxx321 ");
String str2 ("abc ");
 
// Check if str1 starts with '123'
Cout <"str1 starts with/" 123 \ ":" <
(Starts_with (str1, string ("123 "))? "True": "false") <endl;

// Check if str1 ends with '123'
Cout <"str1 ends with \" 123 \ ":" <
(Ends_with (str1, string ("123 "))? "True": "false") <endl;
 
// Check if str1 containes 'xxx'
Cout <"str1 contains \" xxx \ ":" <
(Contains (str1, string ("xxx "))? "True": "false") <endl;
 
 
// Check if str2 equals to 'abc'
Cout <"str2 equals \" abc \ ":" <
(Equals (str2, string ("abc "))? "True": "false") <endl;
 
 
// Classification functors and all predicate
If (all (";.,", is_punct ()))
{
Cout <"\";., \ "are all punctuation characters" <endl;
}
 
// Classification predicates can be combined
If (all ("abcxxx", is_any_of ("xabc ")&&! Is_space ()))
{
Cout <"true" <endl;
}
 
Cout <endl;
 
Return 0;
}

Boost replacement example
[Python]
# Include <string>
# Include <iostream>
# Include <iterator>
// # Include <boost/algorithm/string/replace. hpp>
// # Include <boost/algorithm/string/erase. hpp>
// # Include <boost/algorithm/string/case_conv.hpp>
# Include <boost/algorithm/string. hpp>
 
// Following two implements des contain second-layer function.
// They are already encoded ded by first-layer header
 
// # Include <boost/algorithm/string/replace2.hpp>
// # Include <boost/algorithm/string/find2.hpp>
 
Using namespace std;
Using namespace boost;
 
// Uppercase formatter
/*
Convert an input to upper case.
Note, that this formatter can be used only on std: string inputs.
*/
Inline string upcase_formatter (
Const iterator_range <string: const_iterator> & Replace)
{
String Temp (Replace. begin (), Replace. end ());
To_upper (Temp );
Return Temp;
}
 
Int main ()
{
Cout <"* Replace Example *" <endl;
 
String str1 ("abc ___ cde ___ efg ");
 
// Erase 6-9th characters from the string
Cout <"str1 without 6th to 9th character:" <
Erase_range_copy (str1, make_iterator_range (str1.begin () + 6, str1.begin () + 9) <endl;
 
// Replace 6-9th character with '++'
Cout <"str1 with 6th to 9th character replaced with '++':" <
Replace_range_copy (
Str1, make_iterator_range (str1.begin () + 6, str1.begin () + 9), "++") <endl;
 
Cout <"str1 with 'cde' replaced with 'xyz ':";

// Replace first 'cde' with 'xyz'. Modify the input
Replace_first_copy (ostream_iterator <char> (cout), str1, "cde", "XYZ ");
Cout <endl;

// Replace all '___'
Cout <"str1 with all' ___ 'replace with '---':" <
Replace_all_copy (str1, "___", "---") <endl;
 
// Erase all '___'
Cout <"str1 without all '___':" <
Erase_all_copy (str1, "___") <endl;
 
// Replace third and 5th occurrence of _ in str1
// Note that nth argument is 0-based
Replace_nth (str1, "_", 4, "+ ");
Replace_nth (str1, "_", 2, "+ ");
 
Cout <"str1 with third and 5th occurrence of _ replace:" <str1 <endl;
 
// Custom formatter examples
String str2 ("abC-xxxx-AbC-xxxx-abc ");
 
// Find string 'abc' ignoring the case and convert it to upper case
Cout <"Upcase all 'abc' (s) in the str2:" <
Find_format_all_copy (
Str2,
First_finder ("abc", is_iequal ()),
Upcase_formatter );

Cout <endl;
 
Return 0;
}

Related Article

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.