Boost库教程
1.Lexical Cast
用于高效文本格式转化
using boost::lexical_cast
例:
将A转换为string类型
lexical_cast(‘A’);
错误消息使用try…catch…捕捉
异常名为bad_lexical_cast
lexical_cast(“1234ab”, 4); //这种情况下就不会报错,因为只取前四个数字
2.Variant
C++中很少使用Union
Variant就像是信号传递的并集
例:
boost::variant<int, string> u1, u2;
//这里u1, u2分别代表int和string类型
boost::variant<int, string> u3;
//这里u3既可以是int类型,又可以是string类型
//u1 = u1 * 2
u1 = boost::get(u1)*2;
variant类型可以任意赋值
例:
U1 = “hello”;
boost::apply_visitor(DoubleVisitor(), u1); //u1 equals to “hellohello”
3.Any
boost::any x,y,z;
//不限定变量的类型
boost::any_cast(y)
//返回变量的拷贝,而get返回变量的引用
z = vector(); //使用动态存储
boost::any_cast<vector>(z).push_back(23);
int i = boost::any_cast<vector>(z).back(); //crash
int i; boost::any p = &i; //返回一个指针
struct property {
string name;
boost::any value;
}
vector properties; //用于存放任何类型
4.Optional
deque queue;
Boost::
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)