boost编译这块不多说了,百度文库上有教程.版本之间变化挺大的,比如说timer库的实现,连接口都变了.
教材用的是<boost程序库完全开发指南>,国人写的,因为资料实在比较少,将就着看看.另外,boost自带的英文手册也比较给力.
timer库
计算程序运行时间(基本没用):
progress_timer t;
date_timer是和date计算相关的库,与java很相似,提供了很多便利的函数.
shared_ptr是引用计数型的智能指针,能自由的拷贝和赋值,可以放到标准容器里.可被多个线程安全读取,其他情况未知..
#include <boost/make_shared.hpp>
shared_ptr<string> spstr = new string();
shared_ptr<string> spstr = make_shared<string>("make_shared");
shared_ptr< vector<int> > spvec = make_shared< vector<int> >(3, 2);
int* p = new int[100];
shared_array<int> sa(p);
shared_array<int> sa2 = sa;
sa[0] = 10;
tribool库,好猎奇的一个库啊,除了true和false状态,增加了第三个状态未知状态,平时都是默认设为false状态的,突然整个世界观就被它颠覆了...
#include <boost/logic/tribool.hpp>
tribool tb(indeterminate);
optional库像它的名字一样,无用的...
字符串操作
验证字符串合法性, 与数字之间的相互转化
#include <boost/lexical_cast.hpp>
using namespace boost;
template<typename T>
bool num_valid(const char* str)
{
try
{
lexical_cast<T>(str);
return true;
}
catch(bad_lexical_cast&)
{
return false;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
try
{
int x = lexical_cast<int>("100");
float pai = lexical_cast<float>("3.14159");
string str = lexical_cast<string>(123);
cout << "x: " << x << " pai: " << pai << " str: " << str << endl;
}
catch(bad_lexical_cast& e)
{
cout << "error: " << e.what() << endl;
}
assert(num_valid<double>("3.14"));
return 0;
}
#include <boost/algorithm/string.hpp>
string_algo应该有的差不多都有了,比java还完善..
函数命名中:
前缀i:有表示大小写敏感的
后缀_copy:有表示不变动输入,返回处理结果的拷贝,否则输入即输出
后缀_if:表面这个算法需要一个作为判断式的谓词函数对象,否则使用默认的判断准则.
to_upper();to_lower();starts_with(); ends_with(); contains(); equals(); all(); is_less(); is_not_greater();
检测字符特性也有一套函数:
修剪算法可以删除字符串开头或结尾部分的空格
查找,替换,删除,分割,合并...
分词库(tokenizer),瞟了一眼,蛋都碎了..
xpressive 正则表达式库,boost.regex的替代品