云起工作室 15711107967
06-boost
2022-11-25 22:03:25

www.boost.org

1、c++类型转换

atof() 将字符串转换为双精度浮点型值

atoi() 将字符串转换成整型

atol() 将字符串转换成长整型

itoa() 将字符串转为整形

gcvt() 将浮点数转换成字符串 取四舍五入


2、boost 库

include<boost/lexical_cast.cpp>

using boost::lexical_cast;

int a = lexical_cast<int>("123"); 字符串转正行

float b = lexical_cast<loat>("1.2345");

string c = lexical_cast<string>(666)


3、format

include<boost/format.hpp>

using boost::format


format fmt("d %2% d %1% aaaaaa %2% !!! ");

fmt% 123;

fmt% abc;

等价于printf("dd %d aaaaaa%s", 123,"abc");


4、string_algo 库

include<boost/algorithm/string.hpp>

to_upper("abc"); 转换成大写 改变原字符串

string str2 = to_upper_copy("abc") 转换成大写 不改变原字符串


to_lower("ABC"); 转换成小写 改变原字符串


starts_width("hello word","he") 检测是否是另一字符串的前缀

ends_width 检测是否是另一字符串的后缀

contains 检测是否包含另一字符串

equals 检测两字符串是否相等

lexicographical_compare 根据字典顺序检测一字符串是否小于另一个


all 检测一个字符串中的所有元素是否满足指定判断式


istart_width 忽略大小写, 检测是否是另一字符串的前缀



cout << is_equal()("abc","abc") << endl;

is_less()("abc","abc")

is_not_greater()("abc","abc")


is_space 字符是否为空

is_alnum 字符是否为字母数字字符

is_appha 字符是否为字母

is_cntrl 字符是否为控制字符

is_digit 字符是否为十进制数字

is_lower 字符是否为小写字符

is_punct 字符是否为标点符号字符

is_upper 字符是否为大写字符

is_xdigit 字符是否为十六进制数字

is_any_of 字符是否为参数字符序列中的任意字符


修剪

trim

trim_right

trim_left

trim_copy

trim_if

trim_copy_if

查找

find_first(str,"hello") 查找字符串第一次在被查找串中出现的位置

ifind_first 忽略大小写

find_last 查找子串最后一次在查找串中的位置

find_nth 查找子串第n此出现在被查找串中的位置

find_head 返回字符串头部n个字符串的位置

find_tail 返回字符串尾部n个字符串的位置


string str="Hello,hello,hello,Boost!!!"

iterator_range<string::iterator> rge;

rge= find_first(str,"hello") 查找字符串第一次在被查找串中出现的位置

rge.begin() - str.begin() 返回起始索引值

rge.end() - str.begin() 返回查找字符串在被查找字符传中的结束位置


删除替换

replace/erase_first 替换/删除第一次出现在被查串中的子串

replace/erase_last 替换/删除走后一次出现在被查串中的子串

replace/erase_nth 替换/删除第n次出现在被查串中的子串

replace/erase_all 替换/删除所有子串

replace/erase_head 替换/删除头部几个字符串 没有i

replace/erase_tial 替换/删除尾部几个字符 没有i


切割字符串:split

list<string> lst;

split(lst,str,if_any_of("*")) ; 以*分割 并赋给l


for(auto e:lst){

cout << e << endl;

}

循环等价于

for(list<string>::iterator it = lst.begin();it!=lst.end();it++){

cout << *it <<endl;

}


连接字符串

string str = join(v,"="); 用等号连接容器中的元素 返回字符串

join_if(v,"=",fun); 容器元素符合方法 fun的元素用等号连接成字符串