The Boost.StringAlgorithms library provides many free-standing functions for string manipulation.

1. converting strings to uppercase

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream> using namespace boost::algorithm int main() {
std::string s = "Boost C++ Libraries";
std::cout << to_upper_copy(s) << std::endl;
return ;
}

The function boost::algorithm::to_upper_copy() converts a string to uppercase, and boost::algorithm::to_lower_copy() converts a string to lowecase. Both functions return a copy of the input string, converted to the specified case. To convert the string in place, use the functions boost::algorithm::to_upper() or boost::algorithm::to_lower().

2. Algorithms to remove characters from a string

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream> using namespace boost::algorithm int main() {
std::string s = "Boost C++ Libraries";
std::cout << erase_first_copy(s, "s") << std::endl; 删除第一个出现的s字符
std::cout << erase_nth_copy(s, "s", ) << std::endl;
std::cout << erase_last_copy(s, "s") << std::endl;
std::cout << erase_all_copy(s, "s") << std::endl;
std::cout << erase_head_copy(s, ) << std::endl; 删除从头部开始后的5个字符
std::cout << erase_tail_copy(s, ) << std::endl; 删除从尾部开始的9个字符
return ;
}

输出结果为:

Boot C++ Libraries

Boot C++ Libraries

Boost C++ Librarie

Boot C++ Librarie

C++ Libraries

Boost C++

3. Searching for substrings

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream> using namespace boost::algorithm; int main() {
std::string s = "Boost C++ Libraries";
boost::iterator_range<std::string::iterator> r = find_first(s, "C++");
std::cout << r << std::endl;
r = find_first(s, "xyz");
std::cout << r << std::endl;
return ;
}

输出为:

C++

functions such as boost::algorithm::find_first(), boost::algorithm::find_last(), boost::algorithm::find_nth(), boost::algorithm::find_heand() and boost::algorithm::find_tail(), these functions return a pair of iterators of type boost::iterator_range. Because the operator operator<< is overloaded for boost::iterator_range, the result of the individual search algorithm can be written directly to standard output.

4. Concatenatin strings with boost::algorithm::join() 连接字符串

#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
#include <iostream> using namespace boost::algorithm; int main() {
std::vector<std::string> v{"Boost", "C++", "Libraries"};
join(v, " ");
return ;
}

输出为:Boost C++ Libraries

5. replace characters in a string

functions: boost::algorithm::replace_first_copy(), boost::algorithm::replace_nth_copy(), boost::algorithm::replace_last_copy(), boost::algorithm::replace_all_copy(), boost::algorithm::replace_head_copy(), boost::algorithm::replace_tail_copy()

6. trim strings

functions: boost::algorithm::trim_left_copy(), boost::algorithm::trim_right_copy(), boost::algorithm::trim_copy() to remove spaces on either end of a string.

Boost.StringAlgorithms lets you provide a predicate as an additional parameter for different functions to determine which characters of the string the function is applied to. The versions with predicate are: boost::algorithm::trim_right_copy_if(), boost::algorithm::trim_left_copy_if(), boost::algorithm::trim_copy_if()

7. creating predicates with boost::algorithm::is_any_of()

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream> using namespace boost::algorithm; int main() {
std::string s = "--Boost C++ Libraries--";
std::cout << trim_left_copy_if(s, is_any_of("-")) << std::endl;
std::cout << trim_right_copy_if(s, is_any_of("-")) << std::endl;
std::cout << trim_copy_if(s, is_any_of("-")) << std::endl;
return ;
}

输出为:

Boost C++ Libraries--

--Boost C++ Libraries

Boost C++ Libraries

function called boost::algorithm::is_any_of(), which is a helper function to create a predicate that checks whether a certain character--passed as parameter to is_any_of() exists in a string. With boost::algorithm::is_any_of(), the characters for trimming a string can be specified.

many helper functions that return commonly used predicates: boost::algorithm::is_digit(), boost::algorithm::is_upper(), boost::algorithm::is_lower().

8. Algorithms to compare strings with others

#include <boost/algorithm/string.hpp>
#include <string>
#include <iostream> using namespace boost::algorithm; int main() {
std::string s = "Boost C++ Libraries";
std::cout.setf(std::ios::boolalpha);
std::cout < starts_with(s, "Boost") << std::endl;
std::cout << ends_with(s, "Libraries") << std::endl;
std::cout << contains(s, "C++") << std::endl;
std::cout << lexicographical_compare(s, "Boost") << std::endl;
return ;
}

输出为:

true

true

true

false

9. Splitting strings with boost::algorithm::split()

#include <boost/algorithm/string.hpp>
#include <string>
#include <vector>
#include <iostream> using namespace boost::algorithm; int main() {
std::string s = "Boost C++ Libraries";
std::vector<std::string> v;
split(v, s, is_space());
std::cout << v.size() << std::endl;
return ;
}

a given string can be split based on a delimiter. The substrings are stored in a container. The function requires as its third parameter a predicate that tests each character an checks whether the string should be split at the given position.

10. Searching strings with boost::algorithm::find_regex()

#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <string>
#include <iostream> using namespace boost::algorithm; int main() {
std::string s = "Boost C++ Libraries";
boost::iterator_range<std::string::iterator> r = find_regex(s, boost::regex{"\\w\\+\\+"});
std::cout << r << std::endl;
return ;
}

输出为:C++

boost string algorithm的更多相关文章

  1. boost::string or boost::regex

    有时候写代码时会遇到下面问题 如果有一个文本文件,其包括内容类似于C语言,当中有一行例如以下格式的语句: layout (local_size_x = a,local_size_y = b, loca ...

  2. Right in the Center (js string algorithm)

    Right in the Center (js string algorithm) codewars https://www.codewars.com/kata/5f5da7a415fbdc0001a ...

  3. boost之algorithm/string

    头文件: #include<iostream>#include <boost/algorithm/string.hpp>using namespace std;using na ...

  4. boost::string 例题1

    如果有一个语法正确的shader源文件,其包括若干关于uniform变量的定义.请写一个程序从某个shader源文件里提取其全部定义的uniform变量.要求记录其名称.数据类型和初始值(如果有定义) ...

  5. String Algorithm Summary - 1

    目录 Suffix Array Summay 单个字符串问题 两个字符串问题 多个字符串问题 AC-Automaton Summary 求长度为n(2e9)不包含给定字符串的合法串个数 包含至少一个词 ...

  6. (四)boost库之正则表达式regex

    (四)boost库之正则表达式regex 正则表达式可以为我们带来极大的方便,有了它,再也不用为此烦恼 头文件: #include <boost/regex.hpp> 1.完全匹配 std ...

  7. boost replace_if replace_all_regex_copy用法

    #include <boost/algorithm/string.hpp> // for is_any_of #include <boost/range/algorithm/repl ...

  8. boost range

    1.Algorithms Boost.Range is library that, on the first sight, provides algorithms similar to those p ...

  9. C++ TR1、TR2与boost的关系

    C++ Technical Report 1 (TR1)是ISO/IEC TR 19768, C++ Library Extensions(函式库扩充)的一般名称.TR1是一份文件,内容提出了对C++ ...

随机推荐

  1. 【leetcode】1020. Number of Enclaves

    题目如下: Given a 2D array A, each cell is 0 (representing sea) or 1 (representing land) A move consists ...

  2. service-web

    <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://w ...

  3. CSS中的flex布局

    1.flex 布局的概念 Flex是Flexible Box的缩写,意为"弹性布局",用来为盒状模型提供最大的灵活性.任何一个容器都可以指定为 Flex 布局,行内元素也可以通过 ...

  4. scipy与sklearn下载与安装

    一.scipy下载与安装 scipy下载地址:http://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv提供各种包whl文件 下载之后放到Scripts文件中 ...

  5. flutte页面布局四

    AspectRatio 组件 AspectRatio 的作用是根据设置调整子元素 child 的宽高比. AspectRatio 首先会在布局限制条件允许的范围内尽可能的扩展,widget 的高度是由 ...

  6. 如何为网站启用HTTPS加密传输协议

    前言 当今时代对上网的安全性要求比以前更高,chrome和firefox也都大力支持网站使用HTTPS,苹果也从2017年开始在iOS 10系统中强制app使用HTTPS来传输数据,微信小程序也是要求 ...

  7. flask-路转换器

    from flask import Flask, render_template from werkzeug.routing import BaseConverter # 配置regex路由转换器 # ...

  8. 2019-05-16 Ubuntu使用

    Ubuntu的基本操作 查看操作系统版本 https://www.hostingadvice.com/how-to/ubuntu-show-version/ clu@sha01vmdev08:~/so ...

  9. ACM中java的使用 (转)

    ACM中java的使用 这里指的java速成,只限于java语法,包括输入输出,运算处理,字符串和高精度的处理,进制之间的转换等,能解决OJ上的一些高精度题目. 1. 输入: 格式为:Scanner ...

  10. (二)linux内核准备及编译

    1. 内核下载地址 linux内核网站,可以拿到最新的和最近的稳定版本内核: https://www.kernel.org/ 通过网站下载压缩包后解压或者使用git下载到本地: git clone h ...