优秀的boost字符串处理
2015-03-16 23:12
302 查看
我将把我认为非常有用,C++标准库又不是特别完备的做下重新学习做下笔记:字符串处理,文件系统等
在C++03中字符串的处理相当不灵活
对比MFC的CString的实现,我们可以发现string不仅缺少了分词的tokenizer, 替换字符串的replace也没有实现根据输入子串的替换,不仅没有format还缺少了trim操作,可以说在最常用的地方少了许多便利性。虽然C++11增加了 to_string,stoi等转换函数,但依旧显得单薄。
而boost针对这点增加了许多实用的工具:
下面记得是format,使用起来相当方面只需要包含相关头文件即可使用
format可以提供格式化字符串的操作,同时该类提供了类型检查功能,使用起来则相当方便
format本身重载了operator<<操作符,可以直接输出到想要的流中;
如:
在指定数据时使用有点变化,以%输入所需数据
上面可表示为
此外还可以有另一种像C#的位置的表示
format(” %1%,%2% “) %5 %”Sef"
还有点非常有用如下,包括str函数
cout << format("_%1$4d_ is : _%1$#4x_, _%1$#4o_, and _%1$s_ by default\n") % 18;
// prints "_ 18_ is : _0x12_, _ 022_, and _18_ by default\n"
// Taking the string value :
std::string s;
s= str( format(" %d %d ") % 11 % 22 );
由于format效率较低可以
使用copy来减少字符串解释时间
如
效率可以有个如下直观认识:
其中clockTime
测试代码:
运行结果如下:
可以看到虽然sprintf使用起来不方便,但性能还是要好很多,如果使用format就尽量使用提前构造好,然后拷贝方式使用,从使用来说还是非常方便的
在C++03中字符串的处理相当不灵活
对比MFC的CString的实现,我们可以发现string不仅缺少了分词的tokenizer, 替换字符串的replace也没有实现根据输入子串的替换,不仅没有format还缺少了trim操作,可以说在最常用的地方少了许多便利性。虽然C++11增加了 to_string,stoi等转换函数,但依旧显得单薄。
而boost针对这点增加了许多实用的工具:
下面记得是format,使用起来相当方面只需要包含相关头文件即可使用
format可以提供格式化字符串的操作,同时该类提供了类型检查功能,使用起来则相当方便
format本身重载了operator<<操作符,可以直接输出到想要的流中;
如:
format("%d,%s")
在指定数据时使用有点变化,以%输入所需数据
上面可表示为
format(”%d,%s“)%5%"Sef";
此外还可以有另一种像C#的位置的表示
format(” %1%,%2% “) %5 %”Sef"
还有点非常有用如下,包括str函数
cout << format("_%1$4d_ is : _%1$#4x_, _%1$#4o_, and _%1$s_ by default\n") % 18;
// prints "_ 18_ is : _0x12_, _ 022_, and _18_ by default\n"
// Taking the string value :
std::string s;
s= str( format(" %d %d ") % 11 % 22 );
由于format效率较低可以
使用copy来减少字符串解释时间
如
const format fmt(”%,%s”); format(fmt)%5%"Sef";
效率可以有个如下直观认识:
其中clockTime
#include <iostream> #include <ctime> using namespace std; class clockTime{ public: clockTime(){ clt_start = clock(); } ~clockTime(){ cout << "time cost:\t"<<clock() - clt_start << endl; } private: clock_t clt_start; };
测试代码:
void testProfile() { { cout << "normal" << endl; clockTime te; for (int i = 0; i < 20000; ++i) { format("%d,%s") % 5 % "Sef"; } } { cout << "copy" << endl; clockTime te; const format fmt("%d,%s"); for (int i = 0; i < 20000; ++i) { format(fmt) % 5 % "Sef"; } } { cout << "bind" << endl; clockTime te; const format fmt("%1%,%2%"); for (int i = 0; i < 20000; ++i) { format(fmt) % 5 % "Sef"; } } { char buf[10] = { 0 }; cout << "sprintf" << endl; clockTime te; for (int i = 0; i < 20000; ++i) { sprintf_s(buf, 10, "%d,%s", 5, "Sef"); string a = buf; } } { cout << "to_string" << endl; clockTime te; for (int i = 0; i < 20000; ++i) { string a = to_string(5); a += "Sef"; } } }
运行结果如下:
可以看到虽然sprintf使用起来不方便,但性能还是要好很多,如果使用format就尽量使用提前构造好,然后拷贝方式使用,从使用来说还是非常方便的
相关文章推荐
- boost的字符串处理函数——format
- Boost字符串处理
- boost之字符串与文本处理
- boost常用字符串处理方法学习
- boost 处理字符串
- <Boost> 字符串处理和格式化输出
- boost的字符串处理函数——format
- boost 字符串处理
- boost的字符串处理函数——string algorithm
- boost的字符串处理函数——string algorithm
- boost 字符串处理
- C++ boost 组件简介:字符串及文本处理
- boost 字符串和文本处理库概述
- boost——字符串与文本处理string_algo(一)
- [Boost基础]字符串和文本处理——lexical_cast词汇转换
- boost 05 字符串和文本的处理
- Boost下字符串处理:大小写转换,判断和剪切
- boost字符串处理(上)
- Boost学习系列5-字符串处理
- boost 字符串处理(1)