This article illustrates the method of boost string lookup in C + + and share it for everyone's reference. The specific methods are as follows:
BOOST String Lookup Example
Copy Code code as follows:
#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 ()
{
cout << "* Find Example *" << Endl << 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 This 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 decision function
Copy Code code as follows:
#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 << 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 Substitution example
Copy Code code as follows:
#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 includes contain Second-layer function.
They are already included 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 is input to upper case.
Note, which is formatter can be used only on std::string inputs.
*/
Inline String Upcase_formatter (
Const iterator_range<string::const_iterator>& Replace)
{
The String Temp (Replace.begin (), Replace.end ());
To_upper (Temp);
return Temp;
}
int main ()
{
cout << "* Replace Example *" << Endl << 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 the ' cde ' with ' XYZ '. Modify the input
Replace_first_copy (ostream_iterator<char> (cout), str1, "CDE", "XYZ");
cout << Endl;
Replace All ' ___ '
cout << "str1 with the" ___ ' replaced 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 this nth argument is 0-based
Replace_nth (STR1, "_", 4, "+");
Replace_nth (STR1, "_", 2, "+");
cout << "str1 with third 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;
}
I hope this article will help you with the C + + program design.