• boost库:字符串处理


    使用boost库的字符串处理之前,需要进行区域设置。类:std::locale,每个C++程序自动拥有一个此类的实例,不能直接访问全局区域设置。

    全局区域设置可以使用类std::locale中的静态函数global()改变。

    #include <locale>
    #include <iostream>
    
    int main() {
      std::locale::global(std::locale("German"));
      std::locale loc;
      std::cout << loc.name() << std::endl;
      return 0;
    }

    静态函数global()接受一个类型为std::locale的对象作为其唯一的参数。

    正则表达式库 Boost.Regex

    boost::regex 用于定义一个正则表达式库

    boost::smatch可以保存搜索结果

    #include <boost/regex.hpp>
    #include <locale>
    #include <iostream>
    
    int main() {
      std::locale::global(std::locale("German"));
      std::string s = "Boris Schaling";
      boost::regex expr("\w+\s\w+");
     std::cout << boost::regex_match(s, expr) << std::endl;
    return 0;
    }

     boost::regex_match()用于字符串与正则表达式的比较,字符串匹配正则表达式时返回true。

    下面介绍一下boost string常用的算法接口:

    1. case conversiion
    to_upper()  将输入字符串转换成大写。
    to_upper_copy() 输入字符串不变,返回值为转换成大写的字符串。
    to_lower()  将输入字符串转换成小写。
    to_lower_copy()  输入字符串不变,返回值为转换成小写的字符串。
    2. trimming
    trim_left() 将输入字符串左边的空格删除。
    trim_left_copy() 输入字符串不变,返回去除左边空格的字符串。
    trim_left_if() 将输入字符串左边符合条件的字符去除。trim_left_if("12hello", is_digit()), 输出 hello
    trim_left_copy_if() 输入字符串不变,意思同上。
    
    trim_right() 意思同left差不多。删除右边的空格
    trim_right_if()
    trim_right_copy()
    trim_right_copy_if()
    
    trim() 删除首尾两端的空格。
    trim_if()
    trim_copy()
    trim_copy_if()
    3. predicates
    bool starts_with(input, test) 判断input是否以test为开端。
    bool istarts_with(input, test) 判断input是否以test为开端,大小写不敏感。
    
    bool ends_with(input, test) 判断input是否以test结尾。
    bool iends_with(input, test) 判断input是否以test结尾, 大小写不敏感。
    
    bool contains(input, test) 判断test是否在input里。
    bool icontains(input, test) 判断test是否在input里,大小写不敏感。
    
    bool equals(input, test) 判断input和test是否相等。
    bool iequals(input, test) 判断input和test是否相等, 大小写不敏感。
    
    bool lexicographical_compare() 按字典顺序检测input1是否小于input2。
    bool ilexicographical_compare() 按字典顺序检测input1是否小于input2, 大小写不敏感。
    
    bool all() 检测输入的每个字符是否符合给定的条件。
    4. find algorithms
    iterator_range<string::iterator> find_first(input, search) 从input中查找第一次出现search的位置。
    iterator_range<string::iterator> ifind_first(input, search) 从input中查找第一次出现search的位置,大小写不敏感。
    
    iterator_range<string::iterator> find_last(input, search) 从input中查找最后一次出现search的位置。
    iterator_range<string::iterator> ifind_last(input, search) 从input中查找最后一次出现search的位置,大小写不敏感。
    
    iterator_range<string::iterator> find_nth(input, search, n) 从input中查找第n次(n从0开始)出现search的位置。
    iterator_range<string::iterator> ifind_nth(input, search, n) 从input中查找第n次(n从0开始)出现search的位置,大小写不敏感。
    
    iterator_range<string::iterator> find_head(input, n) 获取input开头n个字符的字串。
    iterator_range<string::iterator> find_tail(input, n) 获取input尾部n个字符的字串。

    iterator_range<string::iterator> find_token() 
  • 相关阅读:
    随意给一组数,找出满足一下条件的a[i],a[i]左边的数小于等于a[i],a[i]右边的数大于等于a[i]
    SVN
    四种进程或线程同步互斥的控制方法
    二叉树转双向链表
    最大连续子序列和
    找出一个字符串中第一个只出现一次的字符
    清除浮动的那些事
    jQuery中ready与load事件的区别
    css 经典布局之圣杯布局(左右固定,中间自适应)
    window.name + iframe跨域实例
  • 原文地址:https://www.cnblogs.com/sssblog/p/10307671.html
Copyright © 2020-2023  润新知