avatar

目录
Boost string algorithm

Apis

boost/algorithm/string/case_conv.hpp

  • to_lower()
  • to_lower_copy()
  • to_upper()
  • to_upper_copy()

boost/algorithm/string/predicate.hpp

  • starts_with()
  • istarts_with()
  • ends_with()
  • iends_with()
  • contains()
  • icontains()
  • equals()
  • iequals()
  • lexicographical_compare()
  • ilexicographical_compare()
  • all()

boost/algorithm/string/classification.hpp

  • is_classified()
  • is_space()
  • is_alnum()
  • is_alpha()
  • is_cntrl()
  • is_digit()
  • is_graph()
  • is_lower()
  • is_print()
  • is_punct()
  • is_upper()
  • is_xdigit()

boost/algorithm/string/trim.hpp

  • trim_left_copy_if()
  • trim_left_copy()
  • trim_left_if()
  • trim_left()
  • trim_right_copy_if()
  • trim_right_copy()
  • trim_right_if()
  • trim_right()
  • trim_copy_if()
  • trim_copy()
  • trim_if()
  • trim()

boost/algorithm/string/find.hpp

  • find()
  • find_first()
  • ifind_first()
  • find_last()
  • ifind_last()
  • find_nth()
  • ifind_nth()
  • find_head()
  • find_tail()
  • find_token()

boost/algorithm/string/replace.hpp

  • replace_range_copy()
  • replace_range()
  • replace_first_copy()
  • replace_first()
  • ireplace_first_copy()
  • ireplace_first()
  • replace_last_copy()
  • replace_last()
  • ireplace_last_copy()
  • ireplace_last()
  • replace_nth_copy()
  • replace_nth()
  • ireplace_nth_copy()
  • ireplace_nth()
  • replace_all_copy()
  • replace_all()
  • ireplace_all_copy()
  • ireplace_all()
  • replace_head_copy()
  • replace_head()
  • replace_tail_copy()
  • replace_tail()

boost/algorithm/string/erase.hpp

  • erase_range_copy()
  • erase_range()
  • erase_first_copy()
  • erase_first()
  • ierase_first_copy()
  • ierase_first()
  • erase_last_copy()
  • erase_last()
  • ierase_last_copy()
  • ierase_last()
  • erase_nth_copy()
  • erase_nth()
  • ierase_nth_copy()
  • ierase_nth()
  • erase_all_copy()
  • erase_all()
  • ierase_all_copy()
  • ierase_all()
  • erase_head_copy()
  • erase_head()
  • erase_tail_copy()
  • erase_tail()

boost/algorithm/string/find_format.hpp

  • find_format_copy()
  • find_format()
  • find_format_all_copy()
  • find_format_all()

boost/algorithm/string/find_iterator.hpp

  • make_find_iterator()
  • make_split_iterator()

boost/algorithm/string/split.hpp

  • find_all()
  • ifind_all()
  • split()

Quick Reference

Case Conversion

Algorithm name Description Functions
to_upper Convert a string to upper case
to_lower Convert a string to lower case

Trimming

Algorithm name Description Functions
trim_left Remove leading spaces from a string
trim_right Remove trailing spaces from a string
trim Remove leading and trailing spaces from a string

Predicates

Algorithm name Description Functions
starts_with Check if a string is a prefix of the other one
ends_with Check if a string is a suffix of the other one
contains Check if a string is contained of the other one
equals Check if two strings are equal
lexicographical_compare Check if a string is lexicographically less then another one
all Check if all elements of a string satisfy the given predicate

Find algorithm

Algorithm name Description Functions
find_first Find the first occurrence of a string in the input
find_last Find the last occurrence of a string in the input
find_nth Find the nth (zero-indexed) occurrence of a string in the input
find_head Retrieve the head of a string
find_tail Retrieve the tail of a string
find_token Find first matching token in the string
find_regex Use the regular expression to search the string
find Generic find algorithm

Erase/Replace

Algorithm name Description Functions
replace/erase_first Replace/Erase the first occurrence of a string in the input
replace/erase_last Replace/Erase the last occurrence of a string in the input
replace/erase_nth Replace/Erase the nth (zero-indexed) occurrence of a string in the input
replace/erase_all Replace/Erase the all occurrences of a string in the input
replace/erase_head Replace/Erase the head of the input
replace/erase_tail Replace/Erase the tail of the input
replace/erase_regex Replace/Erase a substring matching the given regular expression
replace/erase_regex_all Replace/Erase all substrings matching the given regular expression
find_format Generic replace algorithm

Split

Algorithm name Description Functions
find_all Find/Extract all matching substrings in the input
split Split input into parts
iter_find Iteratively apply the finder to the input to find all matching substrings
iter_split Use the finder to find matching substrings in the input and use them as separators to split the input into parts

Join

Algorithm name Description Functions
join Join all elements in a container into a single string
join_if Join all elements in a container that satisfies the condition into a single string

Finders and Formatters

Finder Description Generators
first_finder Search for the first match of the string in an input first_finder()
last_finder Search for the last match of the string in an input last_finder()
nth_finder Search for the nth (zero-indexed) match of the string in an input nth_finder()
head_finder Retrieve the head of an input head_finder()
tail_finder Retrieve the tail of an input tail_finder()
token_finder Search for a matching token in an input token_finder()
range_finder Do no search, always returns the given range range_finder()
regex_finder Search for a substring matching the given regex regex_finder()
Formatter Description Generators
const_formatter Constant formatter. Always return the specified string const_formatter()
identity_formatter Identity formatter. Return unmodified input identity_formatter()
empty_formatter Null formatter. Always return an empty string empty_formatter()
regex_formatter Regex formatter. Format regex match using the specification in the format string regex_formatter()

Iterator

Iterator name Description Iterator class
find_iterator Iterates through matching substrings in the input find_iterator
split_iterator Iterates through gaps between matching substrings in the input split_iterator

Classification

Predicate name Description Generator
is_classified Generic ctype mask based classification is_classified()
is_space Recognize spaces is_space()
is_alnum Recognize alphanumeric characters is_alnum()
is_alpha Recognize letters is_alpha()
is_cntrl Recognize control characters is_cntrl()
is_digit Recognize decimal digits is_digit()
is_graph Recognize graphical characters is_graph()
is_lower Recognize lower case characters is_lower()
is_print Recognize printable characters is_print()
is_punct Recognize punctuation characters is_punct()
is_upper Recognize uppercase characters is_upper()
is_xdigit Recognize hexadecimal digits is_xdigit()
is_any_of Recognize any of a sequence of characters is_any_of()
is_from_range Recognize characters inside a min…max range is_from_range()

Examples

code repo: https://github.com/busyboxs/boost-learn

Convert example

cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <string>
#include <vector>
#include <iostream>
#include <iterator>
#include <boost/algorithm/string/case_conv.hpp>

int main()
{
std::cout << "* Case Conversion Example *" << std::endl << std::endl;

std::string str1("AbCdEfG");
std::vector<char> vec1(str1.begin(), str1.end());

std::cout << "lower-cased copy of vec1: ";
boost::to_lower_copy(std::ostream_iterator<char>(std::cout), vec1);
std::cout << std::endl;

std::cout << "upper-cased copy of str1:" << boost::to_upper_copy(str1) << std::endl;

boost::to_lower(str1);
std::cout << "lower-cased str1: " << str1 << std::endl;

boost::to_upper(str1);
std::cout << "then to upper case: " << str1 << std::endl;

std::cout << std::endl;

return 0;
}

trim example

cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <string>
#include <iostream>
#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/classification.hpp>

int main()
{
std::cout << "* Trim Example *" << std::endl << std::endl;

std::string str1(" 1x x x x1 ");
std::string str2("<>trim<>");
std::string str3("123abs123");

std::cout << "trim_left copy of str1: [" << boost::trim_left_copy(str1) << "]" << std::endl;
std::cout << "trim_left copy of str2 (space = '<>'): [" << boost::trim_left_copy_if(str2, boost::is_any_of("<>")) << "]" << std::endl;

std::cout << "trim_right copy of str1: [" << boost::trim_right_copy(str1) << "]" << std::endl;
std::cout << "trim_right copy of str2 (space = '<>'): [" << boost::trim_right_copy_if(str2, boost::is_any_of("<>")) << "]" << std::endl;

std::cout << "trimmed copy of str1: [" << boost::trim_copy(str1) << "]" << std::endl;
std::cout << "trimmed copy of str4 (space = '<>'): [" << boost::trim_copy_if(str2, boost::is_any_of("<>")) << "]" << std::endl;
std::cout << "trimmed copy of str5 (space = digit): [" << boost::trim_copy_if(str3, boost::is_digit()) << "]" << std::endl;

str1 = " 1x x x x1 ";
boost::trim_left(str1);
std::cout << "trim_left on str1: [" << str1 << "]" << std::endl;

str2 = "<>trim<>";
boost::trim_left_if(str2, boost::is_any_of("<>"));
std::cout << "trim_left of str2 (space = '<>'): [" << str2 << "]" << std::endl;

str1 = " 1x x x x1 ";
boost::trim_right(str1);
std::cout << "trim_right on str1: [" << str1 << "]" << std::endl;

str2 = "<>trim<>";
boost::trim_right_if(str2, boost::is_any_of("<>"));
std::cout << "trim_right of str2 (space = '<>'): [" << str2 << "]" << std::endl;

str1 = " 1x x x x1 ";
boost::trim(str1);
std::cout << "trim on str1: [" << str1 << "]" << std::endl;

str2 = "<>trim<>";
boost::trim_if(str2, boost::is_any_of("<>"));
std::cout << "trim of str2 (space = '<>'): [" << str2 << "]" << std::endl;

return 0;
}

find example

cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <string>
#include <iostream>
#include <algorithm>
#include <functional>
#include <boost/algorithm/string.hpp>

bool func(char c)
{
return c == 'o' || c == 'e' || c == 'l';
}

void findTokenExample()
{
char s1[] = "Hello and Welcome to Boost ";
char s2[] = "It is very interesting 7873";
char s3[] = "srtwyuzx 0043A";

boost::iterator_range<char*> it1 = boost::find_token(s1, func, boost::token_compress_on);
boost::iterator_range<char*> it2 = boost::find_token(s1, func);
boost::iterator_range<char*> it3 = boost::find_token(s2, boost::is_digit(), boost::token_compress_on);
boost::iterator_range<char*> it4 = boost::find_token(s2, boost::is_alpha(), boost::token_compress_on);
boost::iterator_range<char*> it5 = boost::find_token(s3, boost::is_xdigit(), boost::token_compress_on);

std::cout << "------------------------------------------------\n";
std::cout << "With token_compress_on in s1 : [" << it1 << "]" << std::endl;
std::cout << "Without token-compress_on in s1 : [" << it2 << "]" << std::endl << std::endl;
std::cout << "Identifying the first only digits token in s2 : [" << it3 << "]" << std::endl;
std::cout << "Identifying the first only alphabets token in s2 : [" << it4 << "]" << std::endl << std::endl;
std::cout << "Identifying the first hexadecimal token in s3: [" << it5 << "]" << std::endl;
}

int main()
{
std::cout << "* Find Example *" << std::endl << std::endl;

std::string str1("abc___cde__dfg");
std::string str2("abc");

boost::iterator_range<std::string::iterator> range = boost::find_first(str1, std::string("cde"));
boost::to_upper(range);
std::cout << "str1 with upper-cased part matching cde: " << str1 << std::endl;

char text[] = "hello dolly!";
boost::iterator_range<char*> crange = boost::find_last(text, "ll");
std::transform(crange.begin(), crange.end(), crange.begin(), std::bind2nd(std::plus<char>(), 1));
boost::to_upper(crange);
std::cout << text << std::endl;

std::string str3("this is an example of boost. It is useful. It is important.");
boost::iterator_range<std::string::iterator> second = boost::find_nth(str3, "is", 2);
boost::to_upper(second);
std::cout << str3 << std::endl;

boost::iterator_range<std::string::iterator> head = boost::find_head(str1, 3);
std::cout << "head(3) of the str1: " << std::string(head.begin(), head.end()) << std::endl;

head = boost::find_tail(str2, 5);
std::cout << "tail(5) of the str2: " << std::string(head.begin(), head.end()) << std::endl;

findTokenExample();

return 0;
}

predicate example

cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <string>
#include <iostream>
#include <functional>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/classification.hpp>

int main()
{
std::cout << "* Predicate Example *" << std::endl << std::endl;

std::string str1("123xxx321");
std::string str2("abc");

std::cout << std::boolalpha;
std::cout << R"(str1 starts with "123": )" << boost::starts_with(str1, std::string("123")) << std::endl;

std::cout << R"(str1 ends with "123": )" << boost::ends_with(str1, std::string("123")) << std::endl;

std::cout << R"(str1 contains "xxx": )" << boost::contains(str1, std::string("xxx")) << std::endl;

std::cout << R"(str2 equals "abc")" << boost::equal(str2, std::string("abc")) << std::endl;

std::cout << R"("abc" is lexicographically less than "abd": )" << boost::lexicographical_compare(std::string("abc"), std::string("abd")) << std::endl;

if (boost::all(";.,", boost::is_punct()))
{
std::cout << R"(";.," are all punctuation characters)" << std::endl;
}

if (boost::all("abcxxx", boost::is_any_of("xabc") && !boost::is_space()))
{
std::cout << true << std::endl;
}

return 0;
}
文章作者: Busyboxs
文章链接: http://yangshun.win/blogs/6054eac/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Busyboxs

评论