boost字符串处理(上)

来源:互联网 发布:javascript模块化开发 编辑:程序博客网 时间:2024/05/17 00:38
一、概述

    最近工作又开始忙了,额外学习boost的机会也变少了很多,再加上在使用Boost时出现了很多编译错误的问题,让写文章的过程变得不可预测了。但我还是很期待这一部分,这是在平时应用中最常见的,也是boost的看家本领了,将会着重介绍。在标准 C++ 中,用于处理字符串的是std::string 类,它提供很多字符串操作,包括查找指定字符或子串的函数。尽管 std::string囊括了百余函数,是标准C++中最为臃肿的类之一,但却仍不能满足很多开发者在日常工作中的需要。例如, Java中提供的可以将字符串转换到大写字母的函数,std::string就没有相应的功能。Boost C++ 库试图弥补这一缺憾。

二、区域设置

    进入正题之前,需要先看一下区域设置的问题,本章中提到的很多函数都需要一个附加的区域设置参数。区域设置在标准 C++ 中封装了文化习俗相关的内容,包括货币符号、日期时间格式、分隔整数部分与分数部分的符号(基数符)以及多于三个数字时的分隔符(千位符)。

    在字符串处理方面,区域设置和特定文化中对字符次序以及特殊字符的描述有关。例如,字母表中是否含有变异元音字母以及其在字母表中的位置都由语言文化决定。如果一个函数用于将字符串转换为大写形式,那么其实施步骤取决于具体的区域设置。在德语中,字母'?' 显然要转换为'?',然而在其他语言中并不一定。

    使用类std::string时区域设置可以忽略, 因为它的函数均不依赖于特定语言。 然而在本章中为了使用 Boost C++ 库, 区域设置的知识是必不可少的。C++标准中在 locale 文件中定义了类 std::locale 。每个 C++ 程序自动拥有一个此类的实例,即不能直接访问的全局区域设置。如果要访问它,需要使用默认构造函数构造类std::locale的对象,并使用与全局区域设置相同的属性初始化。如下:

  1. #include <locale>    
  2. #include <iostream>    
  3.   
  4. int main()   
  5. {   
  6.   std::locale loc;   
  7.   std::cout << loc.name() << std::endl;   
  8. }  

    以上程序在iostream中输出C,这就是基本区域设置的名称,它包括了 C 语言编写的程序中默认使用的描述。这也是每个 C++ 应用的默认全局区域设置,它包括了美式文化中使用的描述。如货币符号使用美元符号,基字符为英文句号,日期中的月份用英语书写。全局区域设置可以使用类std::locale中的静态函数global()改变。

  1. #include <locale>    
  2. #include <iostream>    
  3.   
  4. int main()   
  5. {   
  6.   std::locale::global(std::locale("German"));   
  7.   std::locale loc;   
  8.   std::cout << loc.name() << std::endl;   
  9. }  

    静态函数global接收类型为std::locale的对象作为唯一的参数,此类的另一个版本的构造函数接受类型为const char*的字符串,可以为一个特别的文化创建区域设置对象。然而,除了C区域设置相应地命名为 "C" 之外,其他区域设置的名字并没有标准化,这就依赖于接受区域设置名字的C++标准库。VS 2008的语言字符串文档指出,可以使用语言字符串 "German" 选择定义为德国文化。

    上面程序的输出是German_Germany.1252。指定语言字符串为 "German" 等于选择了德国文化作为主要语言和子语言,这里选择了字符映射1252。以此类推,如果想指定与德国文化不同的子语言设置,例如瑞士语,需要使用不同的语言字符串。

  1. #include <locale>    
  2. #include <iostream>    
  3.   
  4. int main()   
  5. {   
  6.   std::locale::global(std::locale("German_Switzerland"));   
  7.   std::locale loc;   
  8.   std::cout << loc.name() << std::endl;   
  9. }   

现在程序会输出 German_Switzerland.1252 。

    在初步理解了区域设置以及如何更改全局设置后,下面的例子说明了区域设置如何影响字符串操作。

  1. #include <locale>    
  2. #include <iostream>    
  3. #include <cstring>    
  4.   
  5. int main()   
  6. {   
  7.   std::cout << std::strcoll("?""z") << std::endl;   
  8.   std::locale::global(std::locale("German"));   
  9.   std::cout << std::strcoll("?""z") << std::endl;   
  10. }   

    本例使用了定义在文件cstring中的函数 std::strcoll() ,该函数用于按照字典顺序比较第一个字符串是否小于第二个。也就是两个字符串中哪一个在字典中靠前(郁闷了,VC中居然不让输入?,自动变成了’?’)。执行程序,得到结果为1和-1。虽然函数的参数是一样的, 却得到了不同的结果。 原因很简单,在第一次调用函数 std::strcoll() 时,使用了全局 C 区域设置; 而在第二次调用时,全局区域设置更改为德国文化。 从输出中可以看出,在这两种区域设置中,字符'?'和'z'的次序是不同的。

    很多C 函数以及 C++ 流都与区域设置有关。尽管类 std::string 中的函数是与区域设置独立工作的, 但是以下各节中提到的函数并不是这样。 所以,在本章中还会多次提到区域设置的相关内容。

三、字符串算法库 Boost.StringAlgorithms

    Boost C++字符串算法库提供了很多字符操作函数,操作的字符串类型可以为std:;string、std::wstring或任何其他模板类std::basic_string的实例。使用时需包含头文件boost/algorithm/string.hpp,这个库中很多函数都可以接受类型为std::local的对象作为附加的可选参数,若未设置会使用默认的全局区域设置。先看下这个德国区的:

 
  1. #include <boost/algorithm/string.hpp>    
  2. #include <locale>    
  3. #include <iostream>    
  4. #include <clocale>    
  5.   
  6. int main()   
  7. {   
  8.   std::setlocale(LC_ALL, "German");   
  9.   std::string s = "Boris Sch?ling";   
  10.   std::cout << boost::algorithm::to_upper_copy(s) << std::endl;   
  11.   std::cout << boost::algorithm::to_upper_copy(s, std::locale("German")) << std::endl;   
  12. }  

    函数to_upper_copy用于转换一个字符串为大写,它返回转换后的字符串。上面代码第一次调用时使用的是默认全局区域设置, 第二次调用时则明确将区域设置为德国文化。显然后者的转换是正确的, 因为小写字母 '?' 对应的大写形式 '?' 是存在的。而在C区域设置中, ?' 是一个未知字符所以不能转换。为了能得到正确结果,必须明确传递正确的区域设置参数或者在调用 boost::algorithm::to_upper_copy() 之前改变全局区域设置。可以注意到,程序使用了定义在头文件 clocale 中的函数 std::setlocale() 为 C 函数进行区域设置, 因为 std::cout 使用 C 函数在屏幕上显示信息。 在设置了正确的区域后,才可以正确显示 '?' 和 '?' 等元音字母。另外,程序中的setlocale函数可以用std::locale::global代替,同为全局区域设置操作。

    Boost.StringAlgorithms 库还提供了几个从字符串中删除单独字母的函数, 可以明确指定在哪里删除,如何删除。例如,可以使用函数boost::algorithm::erase_all_copy()从整个字符串中删除特定的某个字符,若想只在此字符首次出现时删除,可以使用函数 boost::algorithm::erase_first_copy()。如果要在字符串头部或尾部删除若干字符,可以使用函数boost::algorithm::erase_head_copy()和boost::algorithm::erase_tail_copy():

  1. #include <boost/algorithm/string.hpp>    
  2. #include <locale>    
  3. #include <iostream>    
  4.   
  5. int main()   
  6. {   
  7.   std::locale::global(std::locale("German"));   
  8.   std::string s = "Boris Sch?ling";   
  9.   boost::iterator_range<std::string::iterator> r = boost::algorithm::find_first(s, "Boris");   
  10.   std::cout << r << std::endl;   
  11.   r = boost::algorithm::find_first(s, "xyz");   
  12.   std::cout << r << std::endl;   
  13. }   

    以下各个不同函数boost::algorithm::find_first()、boost::algorithm::find_last()、 boost::algorithm::find_nth()、boost::algorithm::find_head()以及boost::algorithm::find_tail()可以用于在字符串中查找子串。

    上面的程序还用到了一个boost::iterator_range,这个迭代器是所有这些函数的返回类型。此类起源于Boost C++的Boost.Range库,它在迭代器的概念上定义了“范围”。因为操作符<<由boost::iterator_range类重载而来,单个搜索算法的结果可以直接写入标准输出流。以上程序将Boris作为第一个结果输出而第二个结果为空字符串。

  1. #include <boost/algorithm/string.hpp>    
  2. #include <locale>    
  3. #include <iostream>    
  4. #include <vector>    
  5.   
  6. int main()   
  7. {   
  8.   std::locale::global(std::locale("German"));   
  9.   std::vector<std::string> v;   
  10.   v.push_back("Boris");   
  11.   v.push_back("Sch?ling");   
  12.   std::cout << boost::algorithm::join(v, " ") << std::endl;   
  13. }   

    函数boost::algorithm::join()接受一个字符串的容器作为第一个参数,根据第二个参数将这些字符串连接起来。相应地这个例子会输出Boris Sch?ling。

  1. #include <boost/algorithm/string.hpp>    
  2. #include <locale>    
  3. #include <iostream>    
  4.   
  5. int main()   
  6. {   
  7.   std::locale::global(std::locale("German"));   
  8.   std::string s = "Boris Sch?ling";   
  9.   std::cout << boost::algorithm::replace_first_copy(s, "B""D") << std::endl;   
  10.   std::cout << boost::algorithm::replace_nth_copy(s, "B", 0, "D") << std::endl;   
  11.   std::cout << boost::algorithm::replace_last_copy(s, "B""D") << std::endl;   
  12.   std::cout << boost::algorithm::replace_all_copy(s, "B""D") << std::endl;   
  13.   std::cout << boost::algorithm::replace_head_copy(s, 5, "Doris") << std::endl;   
  14.   std::cout << boost::algorithm::replace_tail_copy(s, 8, "Becker") << std::endl;   
  15. }   

    Boost.StringAlgorithms 库不但提供了查找子串或删除字母的函数, 而且提供了使用字符串替代子串的函数,包括 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() 等等。 它们的使用方法同查找和删除函数是差不多一样的,所不同的是还需要一个替代字符串作为附加参数。

  1. #include <boost/algorithm/string.hpp>    
  2. #include <locale>    
  3. #include <iostream>    
  4.   
  5. int main()   
  6. {   
  7.   std::locale::global(std::locale("German"));   
  8.   std::string s = "\t Boris Sch?ling \t";   
  9.   std::cout << "." << boost::algorithm::trim_left_copy(s) << "." << std::endl;   
  10.   std::cout << "." <<boost::algorithm::trim_right_copy(s) << "." << std::endl;   
  11.   std::cout << "." <<boost::algorithm::trim_copy(s) << "." << std::endl;   
  12. }   

    可以使用修剪函数 boost::algorithm::trim_left_copy(), boost::algorithm::trim_right_copy() 以及 boost::algorithm::trim_copy() 等自动去除字符串中的空格或者字符串的结束符。什么字符是空格取决于全局区域设置。

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

  1. #include <boost/algorithm/string.hpp>    
  2. #include <locale>    
  3. #include <iostream>    
  4.   
  5. int main()   
  6. {   
  7.   std::locale::global(std::locale("German"));   
  8.   std::string s = "--Boris Sch?ling--";   
  9.   std::cout << "." << boost::algorithm::trim_left_copy_if(s, boost::algorithm::is_any_of("-")) << "." << std::endl;   
  10.   std::cout << "." <<boost::algorithm::trim_right_copy_if(s, boost::algorithm::is_any_of("-")) << "." << std::endl;   
  11.   std::cout << "." <<boost::algorithm::trim_copy_if(s, boost::algorithm::is_any_of("-")) << "." << std::endl;   
  12. }   

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

  1. #include <boost/algorithm/string.hpp>    
  2. #include <locale>    
  3. #include <iostream>    
  4.   
  5. int main()   
  6. {   
  7.   std::locale::global(std::locale("German"));   
  8.   std::string s = "123456789Boris Sch?ling123456789";   
  9.   std::cout << "." << boost::algorithm::trim_left_copy_if(s, boost::algorithm::is_digit()) << "." << std::endl;   
  10.   std::cout << "." <<boost::algorithm::trim_right_copy_if(s, boost::algorithm::is_digit()) << "." << std::endl;   
  11.   std::cout << "." <<boost::algorithm::trim_copy_if(s, boost::algorithm::is_digit()) << "." << std::endl;   
  12. }   

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

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

  1. #include <boost/algorithm/string.hpp>    
  2. #include <locale>    
  3. #include <iostream>    
  4.   
  5. int main()   
  6. {   
  7.   std::locale::global(std::locale("German"));   
  8.   std::string s = "Boris Sch?ling";   
  9.   std::cout << boost::algorithm::starts_with(s, "Boris") << std::endl;   
  10.   std::cout << boost::algorithm::ends_with(s, "Sch?ling") << std::endl;   
  11.   std::cout << boost::algorithm::contains(s, "is") << std::endl;   
  12.   std::cout << boost::algorithm::lexicographical_compare(s, "Boris") << std::endl;   
  13. }   

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

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

  1. #include <boost/algorithm/string.hpp>    
  2. #include <locale>    
  3. #include <iostream>    
  4. #include <vector>    
  5.   
  6. int main()   
  7. {   
  8.   std::locale::global(std::locale("German"));   
  9.   std::string s = "Boris Sch?ling";   
  10.   std::vector<std::string> v;   
  11.   boost::algorithm::split(v, s, boost::algorithm::is_space());   
  12.   std::cout << v.size() << std::endl;   
  13. }   

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

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

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

  1. #include <boost/algorithm/string.hpp>    
  2. #include <boost/algorithm/string/regex.hpp>    
  3. #include <locale>    
  4. #include <iostream>    
  5.   
  6. int main()   
  7. {   
  8.   std::locale::global(std::locale("German"));   
  9.   std::string s = "Boris Sch?ling";   
  10.   boost::iterator_range<std::string::iterator> r = boost::algorithm::find_regex(s, boost::regex("\\w\\s\\w"));   
  11.   std::cout << r << std::endl;   
  12. }   

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