优秀的boost字符串处理
来源:互联网 发布:斯特拉斯堡vs卡昂数据 编辑:程序博客网 时间:2024/06/05 02:24
我将把我认为非常有用,C++标准库又不是特别完备的做下重新学习做下笔记:字符串处理,文件系统等
在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就尽量使用提前构造好,然后拷贝方式使用,从使用来说还是非常方便的
0 0
- 优秀的boost字符串处理
- Boost库的字符串处理算法
- boost 处理字符串
- boost 字符串处理
- Boost字符串处理
- boost 字符串处理
- boost::algorithm 字符串处理
- boost 字符串处理(1)
- boost 字符串处理(2)
- boost字符串处理(上)
- boost字符串处理(下)
- Boost库之字符串处理
- boost的字符串处理函数——string algorithm
- boost的字符串处理函数——string algorithm
- boost的字符串处理函数——format
- boost的字符串算法
- boost的字符串算法
- boost库简介之:字符串处理一
- LeetCode - Pascal's Triangle I && II
- UVA - 10271 Chopsticks
- codeigniter配置oracle数据库连接错误
- Android 采用eclipse编辑UI,非常卡问题解决
- UVA - 10617 Again Palindrome
- 优秀的boost字符串处理
- BZOJ 3832 Poi2014 Rally 拓扑排序+堆
- reis学习dayONE,安装
- 标准C++中的string类的用法总结
- iOS设备中WiFi、蓝牙和飞行模式的开启与关闭
- UVA - 11137 Ingenuous Cubrency
- TextView内容过长加省略号
- [150316]电路学习笔记
- 高手路过--菜鸟版系统安装==(图文安装教程)+(最新win7+win8系统)+系统工具