boost string algorithm
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的更多相关文章
- boost::string or boost::regex
有时候写代码时会遇到下面问题 如果有一个文本文件,其包括内容类似于C语言,当中有一行例如以下格式的语句: layout (local_size_x = a,local_size_y = b, loca ...
- Right in the Center (js string algorithm)
Right in the Center (js string algorithm) codewars https://www.codewars.com/kata/5f5da7a415fbdc0001a ...
- boost之algorithm/string
头文件: #include<iostream>#include <boost/algorithm/string.hpp>using namespace std;using na ...
- boost::string 例题1
如果有一个语法正确的shader源文件,其包括若干关于uniform变量的定义.请写一个程序从某个shader源文件里提取其全部定义的uniform变量.要求记录其名称.数据类型和初始值(如果有定义) ...
- String Algorithm Summary - 1
目录 Suffix Array Summay 单个字符串问题 两个字符串问题 多个字符串问题 AC-Automaton Summary 求长度为n(2e9)不包含给定字符串的合法串个数 包含至少一个词 ...
- (四)boost库之正则表达式regex
(四)boost库之正则表达式regex 正则表达式可以为我们带来极大的方便,有了它,再也不用为此烦恼 头文件: #include <boost/regex.hpp> 1.完全匹配 std ...
- boost replace_if replace_all_regex_copy用法
#include <boost/algorithm/string.hpp> // for is_any_of #include <boost/range/algorithm/repl ...
- boost range
1.Algorithms Boost.Range is library that, on the first sight, provides algorithms similar to those p ...
- C++ TR1、TR2与boost的关系
C++ Technical Report 1 (TR1)是ISO/IEC TR 19768, C++ Library Extensions(函式库扩充)的一般名称.TR1是一份文件,内容提出了对C++ ...
随机推荐
- firewall&iptables
一.firewall 查看firewall状态 firewall-cmd --state 如果firewall为关闭状态,先启动firewall systemctl start firewalld 添 ...
- git 配置(实用)
将公钥添加进入即可 码云是添加私钥(注意!!!!) 这个时候Git就配置好了 git clone ssh协议的仓库 就可以直接拉代码了
- 看图了解RocksDB
它是一个高性能的Key-Value数据库.设计了完善的持久化机制,同时保证性能和安全性.能够良好的支持范围查询,因为K-V记录就是按照Key来排序的. 下图为写入的流程: 可以看到主要的三个组成部 ...
- 重写NSString的setter方法
- (void)setName:(NSString *)name { _name = [name copy]; } 就可以了 不需要写成: 第一种: (void)setName:(NSString * ...
- kafka-producer.properties
# Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreement ...
- 【2019 Multi-University Training Contest 3】
01: 02:https://www.cnblogs.com/myx12345/p/11593829.html 03: 04:https://www.cnblogs.com/myx12345/p/11 ...
- 【CF1210B】Marcin and Training Camp(贪心)
题意:有n个人,60种技能点,如果第i个人会第j种技能a[i]的二进制表示的第j位就是1,第i个人的价值是b[i] 如果有若干种技能i会j不会,i就会鄙视j 求一种至少两个人的选人方案使得价值和最大, ...
- 7.zabbix常用item
zabbix常用item zabbix常用item vfs.file.md5sum[/etc/crontab] {basic:vfs.file.md5sum[/etc/crontab].diff()} ...
- USACO 5.5 章节
Picture 题目大意 IOI 1998 求n (<=5000)个矩形 覆盖的图形 的周长(包括洞), 坐标范围[-10000,10000] 题解 一眼离散化+2维线段树,但仔细一想 空间不太 ...
- 2019.7.26 NOIP 模拟赛
这次模拟赛真的,,卡常赛. The solution of T1: std是打表,,考场上sb想自己改进匈牙利然后wei了(好像匈牙利是错的. 大力剪枝搜索.代码不放了. 这是什么神仙D1T1,爆蛋T ...