博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
关于boost里面的string
阅读量:4467 次
发布时间:2019-06-08

本文共 2800 字,大约阅读时间需要 9 分钟。

#include <boost/algorithm/string.hpp>

首先是盘点子串是否是父串的一个子串。

如下所示:

std::string str("I Don't Know.\n");    std::cout << boost::to_upper_copy(str);    std::cout << str ;    boost::to_lower(str);    std::cout << str;    std::string str("Power Bomb");    if (boost::iends_with(str, "bomb"))    {        std::cout << "bomb\n";    }    if (!boost::ends_with(str, "bomb"))        std::cout << "no bomb\n" ;    if (boost::starts_with(str, "Pow"))        std::cout << "start Pow" << std::endl;    if (boost::contains(str, "er"))        std::cout << "contains er\n";    std::string str2 = boost::to_lower_copy(str);    if (boost::iequals(str, str2))        std::cout << "iequal" << std::endl;    std::string str3("power suit");    if (boost::lexicographical_compare(str, str3))    {        std::cout << "litter\n";    }    if (boost::all(str2.substr(0, 5), boost::is_lower()))        std::cout << "is low" << std::endl;
View Code

在这里有各种的查找和判断是否相等,如果前面加i说明是没有计较大小写。

还有一个很长的函数lexicographical_compare,该函数是判断左边的字符串是否小于右边的字符串。

boost::all(str2.substr(0, 5), boost::is_lower())

这个是判断all函数的第一个参数是否全部是小写的,如果是返回true

 

接下去是这个去除字符串头尾指定的字符的trim函数。如果不写if则默认是删除空格。如果有if责判断去除特定的字符。也可以删除多个特定的字符。如:boost::trim_copy_if(str2, boost::is_punct()||boost::is_digit())  去除符号和数字,并通过拷贝返回去,对str2不做改变。

 

对于erase和replace这两个函数也是差不多的道理,

std::cout << boost::ierase_all_copy(str, "samus");

std::cout << boost::replace_nth_copy(str, "l", 1, "L");
std::cout << boost::erase_tail_copy(str, 8);

 

接下来这个是关于分割的各种方式

加#include <boost/assign.hpp>

std::string str = "Samus,Link.Zelda::Mario-Luigi+zelda";    std::deque
d; boost::ifind_all(d, str, "zELDA"); for (BOOST_AUTO(pos, d.begin()); pos != d.end(); ++pos) { std::cout << "[" << *pos << "]" ; } std::cout << std::endl; std::list
> l; boost::split(l, str, boost::is_any_of(",.:-+")); for (BOOST_AUTO(pos, l.begin()); pos != l.end(); ++pos) { std::cout << "[" << *pos << "]" ; } std::cout << std::endl; l.clear(); boost::split(l, str, boost::is_any_of(".:-"), boost::token_compress_on); for (BOOST_AUTO(pos, l.begin()); pos != l.end(); ++pos) { std::cout << "[" << *pos << "]"; } std::cout << std::endl;
View Code

在分割的时候可以有多个字符来做个分隔符。并把分割后的字符串存储到一个容器中。如上面代码所示。

这里要提的一个是:boost::token_compress_on   使能压缩和另一个boost::token_compress_off的使能不压缩即在两个分隔符连在一起的时候如果使能压缩,则只看成是一个分隔符。

 

有分割就有合并

std::vector
v = boost::assign::list_of("Samus")("Link")("Zelda")("Mario"); std::cout << boost::join(v, "+") << std::endl; struct is_contains_a { bool operator()(const std::string &x) { return boost::contains(x, "a"); } }; std::cout << boost::join_if(v, "**", is_contains_a());
View Code

 

转载于:https://www.cnblogs.com/cxiaoln/p/3375304.html

你可能感兴趣的文章
[设计模式] .NET设计模式笔记 - 有多少种设计模式
查看>>
笔记52 Mybatis快速入门(三)
查看>>
Cracking The Coding Interview 1.2
查看>>
PL/SQL报错:无法解析指定的连接标识符
查看>>
LAMP安全加固
查看>>
力扣 5063 最后一块石头的重量 & II
查看>>
导航狗信息导航网站首页源代码(2017年11月03日版)
查看>>
Java中的Class.forName
查看>>
20165223 实验五 网络编程与安全
查看>>
java.math.BigDecimal cannot be cast to [Ljava.lang.Object 报错解决方法
查看>>
20145104张家明 《Java程序设计》第4周学习总结
查看>>
CS 1037 A - Assessment
查看>>
夜神安卓模拟器怎么清除数据
查看>>
解决IE6不支持position:fixed;的问题
查看>>
理解jquery的$.extend()、$.fn和$.fn.extend()的区别及用法
查看>>
NGUI基本事件
查看>>
工具下载地址
查看>>
mysqld诡异crash
查看>>
eclipse启动tomcat出现内存溢出错误 java.lang.OutOfMemoryError: PermGen space
查看>>
POJ 1564 经典dfs
查看>>