网游平台开发中需要使用C++编程,为了有效的利用现有的,成熟的类库,准备对boost库大致浏览一遍,同时将一些心得,记录在blog上。
String and text processing:
1. conversion/lexical_cast
lexical_cast类用于字面值的转换,其原型为:
template<typename Target, typename Source>
Target lexical_cast(Source arg)
lexical_cast要求Target支持>>操作,Source支持<<操作,依赖于对stringstream的<<与>>操作完成转换。相对于CRT (C Run-Time Library)中的atoi, atol, atof, lexical_cast支持的数据类型转换更灵活,但运行效率没有进行测试,需要测试后确定是否保留对atoi等的使用。
无法完成转换时,lexical_cast抛出bad_lexical_cast异常,bad_lexical_cast派生自std::bad_cast
2. format
format类用于C++中替代printf like的格式化输出,并且format类是类型安全的。c++中,通过manipulator也可以实现一部分简单的格式化,但是十分有限。
format类在构造时需要指定一个format string,如下:
format f("%1 + %2 = %3")
%1代表第一个格式化参数,如果想显示%,那需要用%%来代替。格式化参数并不需要按顺序添加,而且同一个格式化参数也不限于只出现在一处,比如format string可以是"%3 %1 %2 %1"
format类重载了operator %,比如前面构造的f,可以使用 f % 10 % 20 % 30; 或者 f %10; f % 20; f % 30; 接下来可以使用 << 操作符将 f 输出,或者是 string s = f.str();
format支持更为复杂的格式化需求,格式为: %[N$][flags][width][.precision]type-char,比如: %3$0#6x
format类提供了两个方法用于异常处理,分别是:
unsigned char exceptions(unsigned char newexcept); // query and set exceptions you care
unsigned char exceptions() const; // just query
exception中可以指定以下的位:
boost::io::bad_format_string_bit, boost::io::too_few_args_bit, boost::io::too_many_args_bit, boost::io::out_of_range_bit
另外, boost::io::all_error_bits和boost::io::no_error_bits为别为两个方便全设或清空所有error bit的值。
3. regex
regex类就不多记了,需要的时候用就是了,关键是regular expression的语法要记熟。
4. spirit
spirit类是 EBNF语法的LL解析器,估计这次不会用到。
5. tokenizer
tokenizer是代替CRT中的strtok方法的,并且功能更加强大。tokenizer还提供了interator,方便与其他库的配合应用。
tokenizer的原型为:
template <
class TokenizerFunc = char_delimiters_separator<char>,
class Iterator = std::string::const_iterator,
class Type = std::string
>
boost库中提供了四个预定义的separator: char_separator, escaped_list_separator, offset_separator和char_delimiters_separator,其中最后一个最boost的文档标注为 deprecated
6. string_algo
与前面的几个类不同,string_algo是一组类的总称,相当于一个sub library了。其位置在boost/algorithm/string目录下,包含的主要内容如下:
- case_conv.hpp: 大小写转换,包含 to_lower_copy, to_lower, to_upper_copy, to_upper
- classification.hpp: 字符类别,包含 is_classified(std::ctype_base::mask, locale), is_space, is_alnum, is_upper, is_xdigit等方法
- cllection_traits.hpp: 一大堆traits类,用于统一char的各种集合类,比如 STL collections, c-style array, null-terminated c-strings等的编程接口。
- compare.hpp: 字符串字符的functor,提供了is_equal, is_iequal两个predicates.
- concept.hpp: concept定义,包含FinderConcept和FormatterConcept
- constants.hpp: 定义了一个enum token_compress_mode_type
- erase.hpp: 提供了一组从string中移除字符和子串的方法,什么样的都有。
- find.hpp: 提供了一组从string中寻找子串的方法,允许指定各种寻找子串的条作。
- finder.hpp: 定义了一组生成string finder的方法
- formatter.hpp: 定义了一组生成string formatter的方法
- predicate.hpp: 提供了一组predictate, 包含starts_with, ends_withs, contains等
- replace.hpp: 提供了一组从string中替换子串的方法
- split:hpp: 提供了一组分割子串的方法。
- trim.hpp: trim算法。
分享到:
相关推荐
Boost Graph Library: The User Guide and Reference Manual,英文版。C++ In-Depth系列丛书之一,介绍C++图形编程利器的一本好书。包含解密的pdf,可以复制、修改。
boost c++ 图论 库 Boost+Graph+Library+-+The+User+Guide+and+Reference+Manual.pdf
Boost Graph Library:User Guide and Reference Manual[Jeremy_G.Siek,Lie-Quan_Lee,Andrew_Lumsdaine]
通过boost::asio::serialport类实现串口通信的例子
使用VS2017编译的boost库最新版1.68的动态库和静态库,多线程参数,经过测试可用
boost::asio::serial下6个工程演示多种串口读取写入方式方法,包含simple,with_timeout,async,callback,qt_integration,stream 等多个工程演示多种方式读取,写入串口,char,string ,buffer[]等多种数据格式。
详细讲述了boost::thread的用法
Beyond the C++ Standard Library: An Introduction to Boost
boost::lexical_cast用法示例,包含数值转字串,字串转数值以及相应的异常处理代码
Beyond the C++ Standard Library : An Introduction to Boost (Chinese)
使用c++实现boost::any类 any类可以存放任意类型数据,如: void test_any() { any any_a1(123); int a2 = any_cast(any_a1); int* p_a2 = any_cast(&any_a1); std::cout *p_a2="*p_a2<<std::endl; any any_b1...
boost library 安装程序 , 这个版本是1-47的 ,软件本身不大 , 但是需要在线下载文档,里面有许多有趣的东东,例如智能指针, 正则表达式等等!!
boost::asio完成了通讯模块的编写,界面用MFC简单做了一下。 局域网的测试结果: 传输速度在6-7m/s 并发到500,服务器CPU和网络应用均出现使用99%的情况出现硬件瓶颈,新连接无法建立(测试服务器比较差,CPU:...
相信不少人听过 boost 委员会提过 boost 里的对象池设计存在缺陷。我花了一些时间研究它的实现,发现其在效率上确实存在严重问题。这里给出一套解决方案。在效率上有了较大提高。可以完全替换你项目里的 object_pool...
CMake Error at cmake/boost.cmake:81 (MESSAGE): You can download it with -DDOWNLOAD_BOOST=1 -DWITH_BOOST= This CMake script will look for boost in <directory>. If it is not there, it will download ...
代码如下:#include <string> #include #include <boost> #include using namespace std; int main() { boost::array<int> array_temp = {{12, 8, 45, 23, 9}}; sort(array_temp.begin(), ...
这是一个由 Boost.Asio 提供支持的简单 D-Bus 绑定。 我尽可能地遵循 Asio 的习惯用法。 代码示例 # include # include < boost> # include using namespace std ; using namespace boost ::asio ; using boost::...
Boost库是一个经过千锤百炼、可移植、提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一;
此压缩包包含12个项目,每个项目都着重讲解Boost::Serialization一个方面的使用技巧,有详细的代码介绍具体使用方法。
Boost.URL是一个用于操作统一资源标识符(URI)和定位器(URL)的库。