Boost 第五章 字符串与文本处理

本文章所有内容源于《BOOST程序库完全开发指南:深入C++“准”标准库(第3版)》第五章


1. lexical_cast

1.1 功能:

字面值转换,字符串与整数/浮点数之间的字面转换

1.2 头文件:

#include <boost/lexical_cast.hpp>
using namespace boost;

1.3 用法:

using namespace std;
#include <boost/lexical_cast.hpp>
using namespace boost;
#include <iostream>

int main()
{
    int  x      = lexical_cast<int>("100");
    long y      = lexical_cast<long>("2000");
    float pai   = lexical_cast<float>("3.14159e5");
    double e    = lexical_cast<double>("2.71828");
    double r    = lexical_cast<double>("1.414,xyz", 5);

    cout << x << y << pai << e << r << endl;

    string str = lexical_cast<string>(456);
    cout << str << endl;

    cout << lexical_cast<string>(0.618) << endl;
    cout << lexical_cast<string>(0x10) << endl;

    cout << lexical_cast<bool>("1") << endl;

    //lexical_cast<int>("0x100");
    //lexical_cast<int>("123L");
}

2. format

2.1 功能:

实现类似于printf()的格式化对象,可以把参数格式化到一个字符串,而且是完全类型安全的。

2.2 头文件:

#include <boost/fromat.hpp>
using namespace boost;

2.3 用法:

#include <iostream>
using namespace std;

#include <boost/format.hpp>
using namespace boost;

//

void case1()
{
    cout << format("%s:%d+%d=%d\n") %"sum" % 1 % 2 % (1+2);

    format fmt("(%1% + %2%) * %2% = %3%\n");
    fmt % 2 % 5 ;
    fmt % ((2+5)*5);
    cout << fmt.str();
}

//

void case2()
{
    format fmt("%05d\t%-8.3f\t% 10s\t%05X\n");
    cout << fmt %62 % 2.236 % "123456789" % 48;

    format fmt2("%|05d|\t%|-8.3f|\t%| 10s|\t%|05X|\n");
    cout << fmt2 %62 % 2.236 % "123456789" % 48;

    const format fmt3("%10d %020.8f %010X %10.5e\n");
    cout << format(fmt3) %62 % 2.236 % 255 % 0.618;
}

//
#include <iomanip>
using namespace boost;
using boost::io::group;

void case3()
{
    format fmt("%1% %2% %3% %2% %1% \n");
    cout << fmt %1 % 2 % 3;

    fmt.bind_arg(2, 10);
    cout << fmt %1 %3;

    fmt.clear();

    cout << fmt % group(showbase,oct,  111) % 333;

    fmt.clear_binds();


    fmt.modify_item(1, group(hex, right, showbase,setw(8), setfill('*')));
    cout << fmt % 49 % 20 % 100;
}

//

int main()
{
    case1();
    case2();
    case3();
}

3. string_ref

3.1 功能:

轻量化后的string字符串,它只持有字符串的引用,没有内存拷贝,运行效率很高。

3.2 头文件:

#include <boost/utility/string_ref.hpp>
using namespace boost;

3.3 用法:

const char* ch = "asdfdsfsadfsadf";           //字符数组
string str(ch);														//标准字符串,有拷贝成本

string_ref s1(ch);													//字符数组构造,零拷贝
string_ref s2(str);													//标准字符串构造,零拷贝

4. string_algo

4.1 功能:

提供大量字符串操作函数,如大小写无关比较、修剪、特定模式的子串查找、替换和删除、分割、合并等

4.2 头文件:

#include <boost/algorithm/string.hpp>
using namespace boost;

4.3 用法:

5. tokenizer

5.1 功能:

专门用于分词(token)的字符串处理库,可以简单的把一个字符串分解成若干个单词。

5.2 头文件:

#include <boost/tokenizer.hpp>
using namespace boost;

5.3 用法:

#include <cstring>
#include <iostream>
using namespace std;

#include <boost/algorithm/string.hpp>
#include <boost/tokenizer.hpp>
using namespace boost;

template<typename T>
void print(T &tok)
{
    for (auto& x : tok)
    {   cout << "[" << x << "]";    }
    cout << endl;
}

//
void case1()
{
    string str("Link raise the master-sword.");

    tokenizer<> tok(str);

    print(tok);
    //for(auto& x: tok)
    //{   cout << "["<< x << "]";}
}

//
void case2()
{
    char str[] = "Link ;; <master-sword> zelda";

    char_separator<char> sep;


    tokenizer<char_separator<char>, char*> tok(str, str +strlen(str), sep);
    print(tok);

    tok.assign(str, str +strlen(str),
        char_separator<char>(" ;-", "<>"));
    print(tok);

    tok.assign(str, str +strlen(str),
        char_separator<char>(" ;-<>", "", keep_empty_tokens));
    print(tok);
}

//
void case3()
{
    string str = "id,100,name,\"mario\"";

    escaped_list_separator<char> sep;
    tokenizer<escaped_list_separator<char> > tok(str, sep);
    print(tok);
}

//
void case4()
{
    string str = "2233344445";
    int offsets[] = {2,3,4};

    offset_separator sep(offsets, offsets + 3, true, false);
    tokenizer<offset_separator> tok(str, sep);
    print(tok);

    tok.assign(str, offset_separator(offsets, offsets + 3, false));
    print(tok);

    str += "56667";
    tok.assign(str, offset_separator(offsets, offsets + 3, true, false));
    print(tok);

}

//
template<typename Func, typename  String = std::string>
struct tokenizer_wrapper
{
    //typedef typename Func::string_type String;
    typedef tokenizer<Func, typename String::const_iterator, String > type;
};

void case5()
{
    wstring str(L"Link mario samus");

    auto p = make_split_iterator(str, first_finder(L" ", is_iequal()));
    decltype(p) endp;

    for (; p != endp;  ++p)
    {       wcout << L"[" << *p << L"]" ;   }
    cout << endl;

    char_separator<wchar_t> sep(L" ");
    //tokenizer<char_separator<wchar_t>,wstring::const_iterator,
    //    wstring > tok(str, sep);
    tokenizer_wrapper<
        char_separator<wchar_t>,wstring>::type      tok(str, sep);


    for(auto& x : tok)
    {      wcout << L"["<< x << L"]";  }
    cout << endl;

}

//

int main()
{
    case1();
    case2();
    case3();
    case4();
    case5();
}

6. xpressive

6.1 功能:

正则表达式处理文本,解决文本处理领域绝大多数问题,诸如验证、匹配、查找、替换等等

6.2 头文件:

混用:
#include <boost/xpressive/xpressive.hpp>
静态:
#include <boost/xpressive/xpressive_static.hpp>
动态:
#include <boost/xpressive/xpressive_dynamic.hpp>

6.3 用法:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值