您现在的位置:首页 > 教案模板 > 正文

boost string find_boost wstring string_lua string.find(2)

2019-09-22 00:02 网络整理 教案网

boost wstring string_boost string find_lua string.find

#include #include #include using namespace std。#include #include #include #include #include #include #include #include #include #include #include using namespace std。#include #include #include using namespace std。

查找任何一个不包含在strcharset串中的字符 (字符串结束符null除外) 在string串中首次出现的位置序号. 返回一个整数值, 指定在string中全部由characters中的字符组成的子串的长度. 如果string以一个不包含在strcharset中的字符开头, 函数将返回0值.。将源串strsource开始的count个字符添加到目标串strdest后. 源串strsource的字符会覆盖目标串strdestination后面的结束符null. 如果count大于源串长度, 则会用源串的长度值替换count值. 得到的新串后面会自动加上null结束符. 与strcat函数一样, 本函数不能处理源串与目标串重叠的情况. 函数返回strdestination值.。子界类型和集合类型7.1枚举类型7.1.1枚举类型的一般形式7.1.2说明7.2子界类型7.2.1子界类型的一般形式7.2.2说明7.3集合类型7.3.1集合类型的一般形式7.3.2说明7.3.3集合的运算7.3.4集合的关系运算练习题第8章 数组类型和串8.1一维数组8.1.1一维数组类型的一般形式8.1.2说明8.2数组在编程中的使用8.2.1排序8.2.2查找8.3多维数组8.3.1二维数组的一般形式8.3.2二维数组的结构8.4字符数组和字符串8.4.1字符数组8.4.2字符串8.4.3字符串的比较规则8.4.4字符串函数及过程练习题第9章 记录类型。

#include<boost/algorithm/string.hpp> #include<locale> #include<iostream> intmain(){std::locale::global(std::locale("German"));std::strings="\tBorisSch?ling\t";std::cout<<"."<<boost::algorithm::trim_left_copy(s)<<"."<<std::endl;std::cout<<"."<<boost::algorithm::trim_right_copy(s)<<"."<<std::endl;std::cout<<"."<<boost::algorithm::trim_copy(s)<<"."<<std::endl;}

函数输入字符串时存在一个问题,就是如果输入了空格会认为字符串结束,对于gets()函数:空格后(包括空格)的字符将作为下一个输入项处理,但gets()函数将接收输入的整个字符串直到遇到换行为止。函数输入字符串时存在一个问题,就是如果输入了空格会认为字符串结束,空格后的字符将作为下一个输入项处理,但gets()函数将接收输入的整个字符串直到遇到换行为止*/。函数输入字符串时存在一个问题,就是如果输入了空格会认为字符串结束,空格后的字符将作为下一个输入项处理,但gets()函数将接收输入的整个字符串直到遇到换行为止*/。

Boost.StringAlgorithms库的函数可以接受一个附加的谓词参数,以决定函数作用于字符串的哪些字符。谓词版本的修剪函数相应地被命名为boost::algorithm::trim_left_copy_if(), boost::algorithm::trim_right_copy_if()和boost::algorithm::trim_copy_if()。

#include<boost/algorithm/string.hpp> #include<locale> #include<iostream> intmain(){std::locale::global(std::locale("German"));std::strings="--BorisSch?ling--";std::cout<<"."<<boost::algorithm::trim_left_copy_if(s,boost::algorithm::is_any_of("-"))<<"."<<std::endl;std::cout<<"."<<boost::algorithm::trim_right_copy_if(s,boost::algorithm::is_any_of("-"))<<"."<<std::endl;std::cout<<"."<<boost::algorithm::trim_copy_if(s,boost::algorithm::is_any_of("-"))<<"."<<std::endl;}

以上程序调用了一个辅助函数boost::algorithm::is_any_of(),它用于生成谓词以验证作为参数传入的字符是否在给定的字符串中存在。使用函数boost::algorithm::is_any_of后,正如例子中做的那样,修剪字符串的字符被指定为连字符。Boost.StringAlgorithms类也提供了众多返回通用谓词的辅助函数。

#include<boost/algorithm/string.hpp> #include<locale> #include<iostream> intmain(){std::locale::global(std::locale("German"));std::strings="123456789BorisSch?ling123456789";std::cout<<"."<<boost::algorithm::trim_left_copy_if(s,boost::algorithm::is_digit())<<"."<<std::endl;std::cout<<"."<<boost::algorithm::trim_right_copy_if(s,boost::algorithm::is_digit())<<"."<<std::endl;std::cout<<"."<<boost::algorithm::trim_copy_if(s,boost::algorithm::is_digit())<<"."<<std::endl;}

函数boost::algorithm::is_digit()返回的谓词在字符为数字时返回布尔值true。检查字符是否为大写或小写的辅助函数分别是boost::algorithm::is_upper()和boost::algorithm::is_lower()。所有这些函数都默认使用全局区域设置,除非在参数中指定其他区域设置。

boost wstring string_lua string.find_boost string find

除了检验单独字符的谓词之外,Boost.StringAlgorithms库还提供了处理字符串的函数。

#include<boost/algorithm/string.hpp> #include<locale> #include<iostream> intmain(){std::locale::global(std::locale("German"));std::strings="BorisSch?ling";std::cout<<boost::algorithm::starts_with(s,"Boris")<<std::endl;std::cout<<boost::algorithm::ends_with(s,"Sch?ling")<<std::endl;std::cout<<boost::algorithm::contains(s,"is")<<std::endl;std::cout<<boost::algorithm::lexicographical_compare(s,"Boris")<<std::endl;}

函数boost::algorithm::starts_with()、boost::algorithm::ends_with、boost::algorithm::contains和boost::algorithm::lexicographical_compare()均可以比较两个字符串。

下面再介绍一个字符串切割函数。

#include<boost/algorithm/string.hpp> #include<locale> #include<iostream> #include<vector> intmain(){std::locale::global(std::locale("German"));std::strings="BorisSch?ling";std::vector<std::string>v;boost::algorithm::split(v,s,boost::algorithm::is_space());std::cout<<v.size()<<std::endl;}

在给定分界符后,使用函数 boost::algorithm::split() 可以将一个字符串拆分为一个字符串容器。 它需要给定一个谓词作为第三个参数以判断应该在字符串的哪个位置分割。 这个例子使用了辅助函数 boost::algorithm::is_space() 创建一个谓词,在每个空格字符处分割字符串。

本节中许多函数都有忽略字符串大小写的版本, 这些版本一般都有与原函数相似的名称,所相差的只是以'i'.开头。例如,与函数 boost::algorithm::erase_all_copy() 相对应的是函数 boost::algorithm::ierase_all_copy()。

最后,值得注意的是类Boost.StringAlgorithms中许多函数都支持正则表达式。以下程序使用函数boost::algorithm::find_regex()搜索正则表达式。

#include<boost/algorithm/string.hpp> #include<boost/algorithm/string/regex.hpp> #include<locale> #include<iostream> intmain(){std::locale::global(std::locale("German"));std::strings="BorisSch?ling";boost::iterator_range<std::string::iterator>r=boost::algorithm::find_regex(s,boost::regex("\\w\\s\\w"));std::cout<<r<<std::endl;}

为了使用正则表达式,此程序使用了Boost C++库中的boost::regex,这将在下一节介绍。